-1

I have to functions and need to compare them for efficiency purposes (who is faster), what is the best way to do it?

The
  • 17
  • 4
  • 1
    That depends _entirely_ on how you define "efficiency". – Chris Apr 25 '20 at 20:21
  • 1
    Please 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 Answers4

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
  • 607
  • 7
  • 15
1

What about using something like that?

import time

start = time.time()
print("hello")
end = time.time()
print(end - start)

Based on the solution provided here: Solution

Fanto
  • 339
  • 2
  • 11
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
  • 125
  • 1
  • 7
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,717
  • 1
  • 9
  • 17