Page 1 of 1

Computer Science Project

Posted: Fri Nov 22, 2019 9:06 am
by jsarb7
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!

Re: Computer Science Project

Posted: Tue Dec 10, 2019 3:57 pm
by norman40
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