One of the simplest way beside %%timeit cell magic,is just to put all in string.
import timeit np_test = '''\ import numpy as np NUM_ELEMS = 10 SCALE = 1000 s1 = set( (np.random.rand(NUM_ELEMS) * SCALE).astype('int') ) s2 = set( (np.random.rand(NUM_ELEMS) * SCALE).astype('int') ) s1.intersection(s2) ''' print(timeit.Timer(stmt=np_test).timeit(number=100000))
Output:3.0972112079055183