0

I am having some trouble using the time or timeit functions to determine the runtime of two algorithms within python. So far I have this

def normal(sound):
  for s in getSamples(sound):
    largest=max(0,getSampleValue(s))
    amplification = 32767.0/largest
  for s in getSamples(sound):
    louder = amplification*getSampleValue(s)
    setSampleValue(s,louder)

def onlyMax(sound):
  for s in getSamples(sound):
    value=getSampleValue(s)
    if value>0:
      setSampleValue(s,32767)
    if value<=0:
      setSampleValue(s,-32768)

import time
def timetwoalgorithms(sound):
    print time.time("normal(sound)","onlyMax(sound)")

The program should measure the time it takes to run each of the functions then output/print what the runtime of each program was.

ddbg
  • 37
  • 1
  • 3
  • 12
  • Im not sure I fully understand the timeit or time function but some help would be appreciated, cant seem to find the answer within stack – ddbg Aug 28 '14 at 17:18
  • I wrote an answer to this [here](http://stackoverflow.com/a/19010935/1663352) – Noelkd Aug 28 '14 at 17:24

2 Answers2

1

The time.time() function does not take any arguments, because it is a function that simply returns the current time (in the form of seconds since a specific point in time, the UNIX epoch). You cannot use it to compare two function runtimes like that. You could use it to measure time passing by storing the value before running a function then comparing that to the time.time() value afterwards, but that's a poor method of measuring performance.

The timeit.timeit() function does let you measure how much a test function takes, by repeatedly executing it and by making sure other factors that may impede accurate measurement are at least minimized. You can only test one such function at a time however.

To test one function, pass in Python source to run the function, and another function to set up the test. That setup should include importing the function:

timeit.timeit("normal(sound)", 'from __main__ import normal, sound')

Do this again for the other function and compare the outcomes against one another.

Take into account that the function will be executed many times (you can adjust how many), so if the function changes global state you'd have to reset that state each time. That'll change how you can measure the performance as well.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

time.time gives the current time. You want timeit.timeit.

print(timeit.timeit("normal(sound)"))
print(timeit.timeit("onlyMax(sound)"))

However, you cannot pass local variables to timeit (how to pass parameters of a function when using timeit.Timer()), so you may have to simplify your function. You can either reimplement timeit:

def timeit(exec_s, globals={}, number=10000):
    t = time.time()
    for i in range(number):
        exec(exec_s, globals)
    return time.time() - t

and do

print(timeit("normal(sound)", locals()))
print(timeit("onlyMax(sound)", locals()))

or place the variables in the module namespace as per @Martijn Pieters answer. Or, incorporating the timing loop into the main function:

def time2algorithms(sound, number=10000):
    t = time.time()
    for i in range(number):
        normal(sound)
    print(time.time() - t)
    t = time.time()
    for i in range(number):
        onlyMax(sound)
    print(time.time() - t)
Community
  • 1
  • 1
matsjoyce
  • 5,744
  • 6
  • 31
  • 38
  • Ok, so as I implement timeit into the function, should I be replacing exec_s with the funtion name? The error I receive is expecting a variable. If not, I don't think I have a handle on it – ddbg Aug 28 '14 at 18:08
  • `exec_s` is a string to execute, just like for `timeit.timeit`. It can be any executable python, in the form of a string. So in your case, it is either `"normal(sound)"` or `"onlyMax(sound)"`. However, if you do not want to use a helper function, you can just replace `exec(exec_s, globals)` with `normal(sound)`. See edit. – matsjoyce Aug 28 '14 at 18:11
  • This seems to work now, however again I am receiving a thread death while it tries to execute the range program through the time loop – ddbg Aug 28 '14 at 18:46