Interesting speed benchmark

Pete Shinners pete at visionart.com
Tue Jun 5 20:59:27 EDT 2001


"Mahesh Padmanabhan" <micronospampad at nospam.yahoo.com> wrote 
> in which the author does some (by his own admission) subjective 
> benchmarks between java and python. While most of it is usual 
> stuff, the one test that stood out was the following:
> 
> Python program:
> ---------------
> 
> class ObjectTest:
>          pass
> 
> for i in xrange(1000):
>      root=ObjectTest()
>      for j in xrange(10000):
>          root.next=ObjectTest()
>          root=root.next

i did some fiddling with your program. first i just put a "pass"
on the inner loop to see what the loop overhead was. on my
system it turned out the looping takes about 10% of the runtime.

then i did a little fiddling to see what we could do. i wrapped
the code into a small function so i could make local variables for
xrange and ObjectTest. this shaved about 10% off the runtime.
then i changed the inner loop to "root.next=root=ObjectTest()"
this dropped another 5% of the runtime out.

well thats the best i could do tuning the loop. it looks to me
like all the time is spend in the object instancing. to test it
ran it using a dictionary instead of an instance, that dropped
the total runtime to about 50% of the original.

so here's my final code that did run about 50% of your original
one. note that it's really the same thing anymore, but it may
help lock down where some of the bottlenecks are.

it's still a whole lot slower than the java version :[

def fulltest():
    X = xrange
    for i in xrange(1000):
         root = {}
         for j in X(500):
             root{'next'} = root = {}







More information about the Python-list mailing list