Reputation: 379
I have 3 functions that do the same thing but with a different implementation and I want to check which of them is faster.
I know that there is a timer library, but my concern is that if one of them is linear and the other one is exponential I have to make a few tests with different inputs to find the answer. For instance, if the input is a list, maybe the function A will be faster for list with length 20, but function B for length 1000.
So, I was wondering if there is any way to do this automatically. I could create a script to do this and maybe with a few plots with the time in base of the inputs length, but I don't want to invent the wheel again, if there is a library that can do this for me.
Upvotes: 1
Views: 1171
Reputation: 11591
If the functions are simple, you can do something like this (with the timeit module):
>>> import timeit
>>> a = ('A()', 'def A():\n x = [i for i in range({list_length})]\n x * 3')
>>> b = ('B()', 'def B():\n x = []\n for i in range({list_length}) * 3:\n x.append(i)')
>>> c = ('C()', 'def C():\n x = []\n for i in range({list_length}):\n x.append(i)\n x * 3')
>>> for num in (100, 1000, 10000):
for func in (a, b, c):
print func[0], '=>', num,
timeit.timeit(func[0], setup=func[1].format(list_length=num), number=10000)
The result:
A() => 100
0.06415607679355162
B() => 100
0.29436154161112427
C() => 100
0.11945218201799435
A() => 1000
0.4792028142351228
B() => 1000
2.701182674568372
C() => 1000
1.0364000362492334
A() => 10000
4.432893686294392
B() => 10000
26.445674297814094
C() => 10000
10.198394832984377
If the functions are more complex, I would suggest using a Python Profiler.
Upvotes: 1
Reputation: 5083
Use timeit
:
import timeit
example = 'map(lambda x: x + 1, range(100000)'
t = timeit.Timer(example)
t.timeit()
If you want something more featureful, this question may be relevant.
Upvotes: 1