2

I'm trying to compare the speed of my functions using timeit but saving the results of these and I can't achieve that. Let me show you a toy example, trying to compare diferent matrix multiplication functions:

from timeit import timeit
import numpy as np

def prod_tensorial (A,B)  : return np.tensordot(A,B,(-1,0))
def prod_punto (A,B)      : return np.dot(A,B)
def prod_sumeinsten (A,B) : return np.einsum('ij,jk->ik',A,B)

A = np.random.rand(1000,2000)
B = np.random.rand(2000,3000)

fs = ( prod_tensorial, prod_punto, prod_sumeinsten )
ts = [ ]
ps = [ ]

for f in fs:
    ts += [ timeit('ps += [f(A,B)]',number=1) ]     # error: ps & f didn't recognized
    #ts += [ timeit(lambda: f(A,B),number=1) ]      # it works but lossing results
    print( ts[-1] )

print(np.allclose(*ps))

How you can see, I can't save the results. Do you know how could I do that?

1 Answer 1

3

Function timeit does not have direct access to the global variables. You must provide the dictionary of the global variabes through the globals keyword. Since the variable ps is first read in the timed statement, it is by default considered local. Mark it as global:

# This part is only for testing
ps = []
def f(x,y): return x+y
A, B = 10, 11

timeit('global ps; ps += [f(A,B)]', number=1, globals=globals())
Sign up to request clarification or add additional context in comments.

7 Comments

I'm still getting the same error. Could you check it please? colab.research.google.com/drive/…
Thanks a lot @DYZ! Just to leave all solved here, I add the complete line for my example: ts += [ timeit('global ps,f; ps += [f(A,B)]', number=1, globals=globals()) ]
f is already global, there is no need to mark it. But whatever.
You are right, it's enough declaring ds as global. Thanks again! ts += [ timeit('global ps; ps += [f(A,B)]', number=1, globals=globals()) ]
I have just readed about that. It's because ps is being assiged inside the script, just like a function. Otherwise, it would be enough adding the globals=globals() parameter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.