0

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

The
  • 27
  • 5

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
  • 667
  • 8
  • 16
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
  • 377
  • 3
  • 14
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