Dec-09-2023, 04:12 PM
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.
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 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)