I have to functions and need to compare them for efficiency purposes (who is faster), what is the best way to do it?
Asked
Active
Viewed 1,524 times
0
-
1That depends _entirely_ on how you define "efficiency". – ChrisGPT was on strike Apr 25 '20 at 20:21
-
1Please repeat [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). This is too broad a question for Stack Overflow. – Prune Apr 25 '20 at 20:27
4 Answers
1
Simplest way is that you can use time function from time library.
import time
start = time.time()
my_function() # This is the task which I have done
end = time.time()
print(end - start)

Deepak
- 667
- 8
- 16
0
Depends on how intense your function is. If it is something simple and you want to compare between some functions, you should run them a few times
import time
t0 = time.time()
for i in range(1,10000):
yourfunction()
t1 = time.time()
for i in range(1,10000):
yourotherfunction()
t2 = time.time()
print(t1-t0, t2-t1)

JLi
- 165
- 1
- 8
0
You want the timeit function. It will run your test case a number of times and give back the timings. You will often see people quoting the results from timeit when they are doing performance comparisons between different approaches.
You can find the docs on it here

Glenn Mackintosh
- 2,765
- 1
- 10
- 18