Computer Science Project

Ask questions about projects relating to: aerodynamics or hydrodynamics, astronomy, chemistry, electricity, electronics, physics, or engineering.

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

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

Computer Science Project

Post 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!
norman40
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

Post 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
Locked

Return to “Grades 6-8: Physical Science”