[Tutor] Loop comparison

Christian Witts cwitts at compuscan.co.za
Fri Apr 16 10:49:05 CEST 2010

```Dave Angel wrote:
>
>
> Christian Witts wrote:
>> <div class="moz-text-flowed" style="font-family: -moz-fixed">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
>>>
>>> Thanks
>>> ark
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at 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
>>
> A simpler formula is simply
>    upper_bounds * (upper_bounds-1) / 2
>
> No check needed for even/odd.
>
> DaveA
>
Ah yes, that is true. :)
Sometimes I feel I overlook the simplest of solutions.

--
Kind Regards,
Christian Witts

```