Ark wrote:
Hi everyone.
A friend of mine suggested me to do the next experiment in python and Java.

It's a simple program to sum all the numbers from 0 to 1000000000.

result = i = 0
while i < 1000000000:
    result += i
    i += 1
print result

The time for this calculations was huge.  It took a long time to give
the result.  But, the corresponding program in Java takes less than 1
second to end.  And if in Java, we make a simple type check per cycle,
it does not take more than 10 seconds in the same machine.  I was not
expecting Python to be faster than Java, but it''s too slow.  Maybe
Java optimizes this case and Python doesn't.  Not sure about this.}

Thanks
ark
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Different methods and their relative benchmarks. The last two functions are shortcuts for what you are trying to do, the last function 't5' corrects the mis-calculation 't4' has with odd numbers. Remember, if you know a better way to do something you can always optimize yourself ;)

>>> def t1(upper_bounds):
...   start = time.time()
...   total = sum((x for x in xrange(upper_bounds)))
...   end = time.time()
...   print 'Time taken: %s' % (end - start)
...   print total
...
>>> t1(1000000000)
Time taken: 213.830082178
499999999500000000
>>> def t2(upper_bounds):
...   total = 0
...   start = time.time()
...   for x in xrange(upper_bounds):
...     total += x
...   end = time.time()
...   print 'Time taken: %s' % (end - start)
...   print total
...
>>> t2(1000000000)
Time taken: 171.760597944
499999999500000000
>>> def t3(upper_bounds):
...   start = time.time()
...   total = sum(xrange(upper_bounds))
...   end = time.time()
...   print 'Time taken: %s' % (end - start)
...   print total
...
>>> t3(1000000000)
Time taken: 133.12481904
499999999500000000
>>> def t4(upper_bounds):
...   start = time.time()
...   mid = upper_bounds / 2
...   total = mid * upper_bounds - mid
...   end = time.time()
...   print 'Time taken: %s' % (end - start)
...   print total
...
>>> t4(1000000000)
Time taken: 1.4066696167e-05
499999999500000000
>>> def t5(upper_bounds):
...   start = time.time()
...   mid = upper_bounds / 2
...   if upper_bounds % 2:
...     total = mid * upper_bounds
...   else:
...     total = mid * upper_bounds - mid
...   end = time.time()
...   print 'Time taken: %s' % (end - start)
...   print total
...
>>> t5(1000000000)
Time taken: 7.15255737305e-06
499999999500000000
>>> t3(1999)
Time taken: 0.0038161277771
1997001
>>> t4(1999)
Time taken: 3.09944152832e-06
1996002
>>> t5(1999)
Time taken: 3.09944152832e-06
1997001

--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: cwi...@compuscan.co.za

NOTE:  This e-mail (including attachments )is subject to the disclaimer 
published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to