I am doing a science fair project about timing math equations on Python. The code I am using is :
import timeit
code_to_test = """
a = range(100000)
b = []
for i in a:
b.append(i*2)
"""
x = 33
y = 999
print(x + y)
elapsed_time = timeit.timeit(code_to_test, number=100)/100
print(elapsed_time)
Will this time the math problems? It says the elapsed time, but I am unaware if that includes the time to do the math problem. Thanks in advance!
Computer Science Project
Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators
-
- Former Expert
- Posts: 1022
- Joined: Mon Jul 14, 2014 1:49 pm
- Occupation: retired chemist
- Project Question: Volunteer
- Project Due Date: n/a
- Project Status: Not applicable
Re: Computer Science Project
Hi jasrb7,
Python code is well outside of my expertise but as I understand the “timeit” module your code will provide the elapsed time for the loop that computes “b”. But the addition of x and y won't be timed.
You could test this by changing the number of cycles in the loop to see if the elapsed time also changes.
I hope this helps. Please ask again if you have more questions.
A. Norman
Python code is well outside of my expertise but as I understand the “timeit” module your code will provide the elapsed time for the loop that computes “b”. But the addition of x and y won't be timed.
You could test this by changing the number of cycles in the loop to see if the elapsed time also changes.
I hope this helps. Please ask again if you have more questions.
A. Norman