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


Messages In This Thread
Pi modules and %timeit - by RockBlok - Dec-09-2023, 04:12 PM
RE: Pi modules and %timeit - by Gribouillis - Dec-09-2023, 04:19 PM
RE: Pi modules and %timeit - by deanhystad - Dec-09-2023, 04:23 PM
RE: Pi modules and %timeit - by RockBlok - Dec-09-2023, 04:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Timeit module Miraclefruit 9 6,003 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