-3

I've got a script with several complex functions, which looks something like this

import time

def function1(a,b,c):
   print(a,b,c)

def function2(d,e,f):
   print(d,e,f)
...
...
def function7(g,h,i):
   print(g,h,i)

I am trying to record the timing for each function and then make a dataframe with all the timings for analysis. This is what I have attempted:

start1 = time.time()
function1(a,b,c)
end1 = time.time()
func1_time = end - start

start2 = time.time()
function2(d,e,f)
end2 = time.time()
func2_time = end - start
...
...
start7 = time.time()
function7(g,h,i)
end7 = time.time()
func7_time = end - start

Above is a basic method using time.time, however, is there a more elegant/convenient way to carry this out?

blackbishop
  • 30,945
  • 11
  • 55
  • 76
star_it8293
  • 399
  • 3
  • 12
  • 3
    Does this answer your question? [timeit versus timing decorator](https://stackoverflow.com/questions/1622943/timeit-versus-timing-decorator) – matszwecja Feb 07 '22 at 12:42
  • The code you provided misses `def` before the functions. – Jakob Stark Feb 07 '22 at 12:43
  • @KellyBundy please see updates – star_it8293 Feb 07 '22 at 13:26
  • @matszwecja not really, looks quite confusing, apologies – star_it8293 Feb 07 '22 at 13:26
  • @JakobStark please see updates – star_it8293 Feb 07 '22 at 13:30
  • What is wrong with `time.time`? "elegant/convenient way to do sth." is very opinion-based. Stackoverflow is not a platform to discuss the elegance or convenience of solutions. You should provide a concrete reason why your solution is not suitable for your problem and clear borderlines of what you expect from alternative solutions that you are asking for. – Jakob Stark Feb 07 '22 at 13:39

1 Answers1

1

When you see variable1, variable2, etc. and you want your code to look more elegant, the answer is in 90% cases either a list or a dictionary:

funs = [function1, function2, function3]
times = []
for fun in funs:
    start = time.time()
    fun(a,b,c)
    end = time.time()
    times.append(end - start)
matszwecja
  • 6,357
  • 2
  • 10
  • 17