Thursday, February 10, 2011

Counting time (another test)

Continuing from yesterday we were curious to see if java is faster than python. we know that java is slower than languages that create executable files. But python runs code by interpreting line by line. The program is the same as yesterday.
We also now that big programs may differ from the results, but we want to see just simple one's, because those that are complicated have several ways of writing.

We wrote in python:
#!/usr/bin/env python
import sys

c=a + b
print c

Save the file as
Make it executable:
chmod a+x

And in java:
Public class Addition
public static void main(String[] args)
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a + b;

Surely python looks shorter. We run the script we mentioned yesterday:
for python:
time for ((i=1;i<=10;i++)); do $i $i

for java:
time for ((i=1;i<=10;i++)); do
java Addition $i $i


real 0m0.156s
user 0m0.080s
sys 0m0.064s


real 0m0.784s
user 0m0.564s
sys 0m0.200s

On previous article we haven't explained time.
The sys time is the important thing since it is the time that the process kept cpu busy.
Python is faster. The difference is very well considered!!! Or was it the print out which is slow?
Let's see again:
We removed the print line of each code.

Here are the results:

real 0m0.164s
user 0m0.112s
sys 0m0.020s

real 0m0.827s
user 0m0.576s
sys 0m0.188s

Final conclusion:
Print out takes an amazing amount of time.
Python is amazingly faster than Java.

What are your experiments? Share them with us on:

No comments:

Post a Comment