jsarb7
Posts: 1
Joined: Fri Nov 22, 2019 9:02 am
Occupation: Teacher

Computer Science Project

Postby jsarb7 » Fri Nov 22, 2019 9:06 am

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!

norman40
Expert
Posts: 1022
Joined: Mon Jul 14, 2014 1:49 pm
Occupation: retired chemist

Re: Computer Science Project

Postby norman40 » Tue Dec 10, 2019 3:57 pm

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


Return to “Grades 6-8: Physical Science”