Skip to content

Instantly share code, notes, and snippets.

@Kush1101
Created September 1, 2020 06:40
Show Gist options
  • Save Kush1101/4bfe14883f32bc59899d8e4aa511083d to your computer and use it in GitHub Desktop.
Save Kush1101/4bfe14883f32bc59899d8e4aa511083d to your computer and use it in GitHub Desktop.
Medium_timeit
import timeit
#This code will be executed only once, before stmt.
setup_code = "from math import sqrt"
#This code will be executed as specified by the parameter 'number'
stmt_code = "sum(sqrt(x) for x in range(1,10000))"
iterations = 10000
time = timeit.timeit(stmt = stmt_code, setup = setup_code, number = iterations)
print(time)
#17.9 seconds
#THIS OUTPUT IS FOR 10000 ITERATIONS OF THE CODE SNIPPET AND NOT A SINGLE ITERATION.
print(f"The time for single iteration is {time/iterations}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment