Question

Here is my code

import datetime
n = raw_input()
print "aaa"
a = datetime.datetime.now()
for i in xrange(1,100000):
    x = 0
    for i in n:
        x = ord(i)-48 + 10*x
b = datetime.datetime.now()
print x,b-a
n = raw_input()
print "aaa"
a = datetime.datetime.now()
for i in xrange(1,100000):
    x = int(n)
b = datetime.datetime.now()
print x,b-a

On my System I am getting this various times for same value 5 for both input, Why am I getting this different values and also that these values are not consistently in same order does it has anything to do with my input typing timing or branching in computers

case 1:

5
aaa
5 0:00:00.112000
5
aaa
5 0:00:00.104000

case 2:

5
aaa
5 0:00:00.108000
5
aaa
5 0:00:00.140000

case 3:

5
aaa
5 0:00:00.114000
5
aaa
5 0:00:00.107000

case 4:

5
aaa
5 0:00:00.110000
5
aaa
5 0:00:00.124000
Was it helpful?

Solution

You are using the wrong approach to time execution differences.

Use the timeit module instead; it uses the most optimal clock for your system, the most optimal loop implementation for repeated testing and disables the garbage collector to minimize system process fluctuations.

Using timeit you'll find that for a single-digit input, your method is faster:

>>> import timeit
>>> def manual(n):
...     x = 0
...     for i in n:
...         x = ord(i)-48 + 10*x
... 
>>> def using_int(n):
...     int(n)
... 
>>> timeit.timeit('manual("5")', 'from __main__ import manual')
0.7053060531616211
>>> timeit.timeit('using_int("5")', 'from __main__ import using_int')
0.9772920608520508

However, using a large inputstring slows it down to a crawl; I tried this with 1000 digits first but ran out of patience after 10 minutes. This is with just 50 digits:

>>> timeit.timeit('manual("5"*50)', 'from __main__ import manual')
15.68298888206482
>>> timeit.timeit('using_int("5"*50)', 'from __main__ import using_int')
1.5522758960723877

int() now beats the manual approach by a factor of 10.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top