Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pi modules and %timeit
#1
I'm trying to compare different ways to use pi and I wanted to do something like this to test out the speed of assigning pi to a variable or using in calculations. I don't know if different pi modules might be faster or slower depending on how they are used, so something like this would help me quickly see what's more efficient.

import math
import numpy as np

print("math.pi equals: ", math.pi)
print("The speed of math.pi is: ", %timeit (x = math.pi))
print("numpy.np equals: ", np.pi)
print("The speed of numpy.pi is: ", %timeit (x = np.pi))
I'm using timeit wrong in some manner. Is it possible to do what I'm trying here with timeit?

edit..

It seems to work if I do it like this, but if there is a better way to do it I like to learn.

import math
import numpy as np
import timeit

def math_dot_pi(a):
    x = math.pi
    return(x)

def numpy_dot_pi(a):
    x = np.pi
    return(x)

x = 0.0

print("math.pi equals: ", math.pi)
print("The speed of math.pi is: ")
%timeit math_dot_pi(x)
print("numpy.np equals: ", np.pi)
%timeit numpy_dot_pi(x)
Reply
#2
There is nothing special with pi. It is just a constant data in a module. There should be no significant difference between assigning math.pi versus numpy.pi or any other constant such as sys.version for example
>>> from timeit import timeit
>>> timeit('x = math.pi', setup='import math', number=10_000_000)
0.32607505199848674
>>> timeit('x = numpy.pi', setup='import numpy', number=10_000_000)
0.4240568519962835
>>> timeit('x = sys.version', setup='import sys', number=10_000_000)
0.3458355639959336
>>> 
Once again
>>> timeit('x = math.pi', setup='import math', number=10_000_000)
0.32594562199665233
>>> timeit('x = numpy.pi', setup='import numpy', number=10_000_000)
0.29548215300019365
>>> timeit('x = sys.version', setup='import sys', number=10_000_000)
0.31242522199318046
>>> 
RockBlok likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Read the documentation here: https://docs.python.org/3/library/timeit.html

Changing your timeit code to strings and using setup to prepare for timing:
from timeit import timeit
 
print("math.pi", timeit('x = math.pi', setup='import math'))
print("numpy.pi", timeit('x = np.pi', setup='import numpy as np'))
I don't know what you think "%" is doing? Is that the old formatting? If so, you forgot to indicate where in the format string you wanted to insert the measured time. In ancient times you would do this:
from timeit import timeit
 
print("math.pi %s" %timeit('x = math.pi', setup='import math'))
print("numpy.pi %s" %timeit('x = np.pi', setup='import numpy as np')) 
Kind of looks like C.

This was later replaced with the format() method.
from timeit import timeit
 
print("math.pi {}".format(timeit('x = math.pi', setup='import math')))
print("numpy.pi {seconds}".format(seconds=timeit('x = np.pi', setup='import numpy as np')))
And in python 3.6 the f'string was introduced.
from timeit import timeit
 
print(f"math.pi {timeit('x = math.pi', setup='import math')}")
print(f"numpy.pi {timeit('x = np.pi', setup='import numpy as np')}")
Older formatting is discussed here: https://pyformat.info/

f'strings are discussed here: https://realpython.com/python-f-strings/
RockBlok likes this post
Reply
#4
Thanks for the quick response. I like your format and I'll follow that.

There shouldn't be much difference, but it never hurts to check things.

For my computer, there seems to be about a 30 ns speed difference between math and numpy when using the x = "pi" code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Timeit module Miraclefruit 9 5,951 Jan-28-2018, 04:16 AM
Last Post: Miraclefruit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020