Interesting speed benchmark

scott smarsh at hotmail.com
Wed Jun 6 01:02:10 EDT 2001


Mahesh Padmanabhan wrote:
> 
(snip)
> Python program:
> ---------------
> 
> class ObjectTest:
>          pass
> 
> for i in xrange(1000):
>      root=ObjectTest()
>      for j in xrange(10000):
>          root.next=ObjectTest()
>          root=root.next
> 
> Time:
> 
> real    1m23.326s
> user    1m23.290s
> sys     0m0.060s
> 
> Java program:
> -------------
> 
> public class ObjectTest {
>      public ObjectTest next;
>      public static void main(String[] args) {
>          for (int i = 0; i < 1000; i++) {
>              ObjectTest root = new ObjectTest();
>              for (int j = 0; j < 10000; j++) {
>                  root.next=new ObjectTest();
>                  root=root.next;
>              }
>          }
>      }
> }
> 
> Time:
> 
> real    0m2.428s
> user    0m2.190s
> sys     0m0.220s
> 
> There is a huge difference in performance.
(snip)

That's certainly a massive difference.
Heres a *somewhat* related comparison :-}, specifically python vs.
jython. 
NT4, python 2.0, jython 2.0 with jdk1.3.0.

-As I expected, a simple loop generating random numbers ran about 3
times faster in python (6.5 secs) vs jython (19.0 secs):
------------------------
import random, time

start = time.time()
li = []
sum = 0
for i in range(100000):
    x = random.gauss(0, 1)
    li.append(x)
    sum = sum+x
end = time.time()
print end - start
print sum
-----------------------

-Unexpectedly, when I did a slight rewrite to take advantage of jythons
ability to use Java classes (specifically java.util.Random) it was just
as fast as the python version (6.5 secs)! :
-------------------
from java.util import Random  #changed this
import time

start = time.time()
li = []
r = Random()                  #added this
sum = 0
for i in range(100000):
    x = r.nextGaussian()      #changed this   
    li.append(x)
    sum = sum+x
end = time.time()
print end - start
print sum
------------------

Interesting (to me at least..).



More information about the Python-list mailing list