Interesting speed benchmark

Mahesh Padmanabhan micronospampad at nospam.yahoo.com
Wed Jun 6 20:00:48 EDT 2001


As one poster suggested, Python's reference counting might be the 
culprit, so I modified the Java test as follows (probably not the 
correct way to do it):

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;
                 System.gc(); // <-----------Force garbage collection

             }
         }
     }
}

The program did not even complete in 7 minutes at which point I became 
impatient and killed it. Did I modify the program correctly ?

I then modified the program to move System.gc() to the outer loop.

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;

             }

             System.gc(); // <-------- Outer loop

         }
     }
}

This time the program did complete with the following time:


real    0m33.165s
user    0m32.980s
sys     0m0.250s

Approximately a 1500% increase.

Seems like that poster was correct in pointing out that the memory 
management scheme of Java vs Python makes a difference.

Mahesh

Mahesh Padmanabhan wrote:

> Hello,
> 
> To email me remove "nospam" from the email address.
> 
> I came across this web site: 
> http://www.twistedmatrix.com/~glyph/rant/python-vs-java.html
> 
> 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:
> 
> (I am using a Debian woody system with Python 2.1 and JDK 1.3 from 
> blackdown on an Intel system)
> 
> 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. In fact I tried this under 
> Windows NT 4 SP5 with Python 2.1 and jdk1.3 from SUN and got similar 
> results.
> 
> I am very curious to know why Python takes so much time.
> 
> Thanks
> 
> Mahesh
> 





More information about the Python-list mailing list