0

I defined a function here to calculate the time of procedure 'p' takes.

def timeit(p):
    a=time.clock()
    p
    b=time.clock()
    print(b-a)

Then I use another function to test it.

d=pd.DataFrame({'x':np.random.randint(1,100,10000)})
d['s']=None
def f1():
    for i in range(10000):
        if d.iloc[i,0]<60:
            d.iloc[i,1]='under'
        elif d.iloc[i,0]<80:
            d.iloc[i,1]='ok'
        else:
            d.iloc[i,1]='best'

Use my timeit:

timeit(f1())         
5.140552730154013e-07

The result is absolutely wrong. Why? Is there any problem in my grammar? The true time is:

a=time.clock()
f1()
b=time.clock()
b-a
Out[26]: 5.914697663515881
adafdwwf
  • 162
  • 3
  • 12

2 Answers2

1

timeit(f1()) calls f1 before passing its return value to timeit, so what you're actually measuring is just the overhead of calling time.clock twice.

You need to pass the function itself and call it in timeit, between the calls to time.clock():

def timeit(p):
    a=time.clock()
    p()              # call it here
    b=time.clock()
    print(b-a)

timeit(f1)           # NOT here

If you need to pass arguments later on you can either use *args / **kwargs or re-write timeit as a decorator.

def timeit(func, *args, **kwargs):
    a = time.clock()
    func(*args, **kwargs)
    b = time.clock()
    print(b - a)
meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
0

It's wrong because your code f1() does not actually get run between the two clock() commands. f1() gets evaluated before the call to timeit and not within the call. See here for more info.

Kyle Parsons
  • 1,475
  • 6
  • 14