-1

I've noticed that results change rapidly between running timeit with the regular arugments and timeit with the argument number=1. Here is the example that I found.

import sympy as sp
import numpy as np
var = (sp.Symbol("x"), sp.Symbol("y"), sp.Symbol("z"))
x,y,z = var[0], var[1], var[2]
monos = np.array([1, z, y, x, z**2, y*z, y**2, x*z, x*y, x**2, z**3, y*z**2, y**2*z, y**3, x*z**2, x*y*z, x*y**2, x**2*z, x**2*y, x**3, z**4, y*z**3, y**2*z**2, y**3*z, y**4, x*z**3, x*y*z**2, x*y**2*z, x*y**3, x**2*z**2, x**2*y*z, x**2*y**2, x**3*z, x**3*y, x**4])
f = sp.lambdify(var, monos)
import testit
timeit.timeit("(f(2,3,4))", setup="from __main__ import f") 
#returns 2.0760600566864014
timeit.timeit("(f(2,3,4))", setup="from __main__ import f", number=1)
#returns 1.5974044799804688e-05

I'm using python2 version 2.7.12 and sympy version 1.2. What is going on here?

Wraith1995
  • 107
  • 1
  • 2
  • 2
    Well did you expect this function to work exactly the same regardless of the arguments you pass to it? Have you looked at the [corresponding docs](https://docs.python.org/3.7/library/timeit.html#timeit.timeit)? What do they say about the number argument? – juanpa.arrivillaga Aug 21 '18 at 19:32

1 Answers1

0

Unlike the IPython %timeit, timeit.timeit returns the total time taken by all the runs. To get the time of an individual run, you have to divide by the number. You can also use timeit.Timer(stmt).autorange() (Python 3.6+ only), which picks a number of runs automatically.

I recommend using the IPython %timeit for timing. It only works interactively, but it's much easier to work with than the timeit module in the standard library.

asmeurer
  • 86,894
  • 26
  • 169
  • 240