#๐Ÿ”’ How to get average of end times

43 messages ยท Page 1 of 1 (latest)

scarlet quartz
#
import time
import random

for x in range (6):
    start = time.time()
    print (random.randrange(1,1000000000))
    end = time.time()
    print(f"Time taken to run the code was {end - start} seconds")

I want to get the average of the end times, how do i add the end time to a variable after every run?

sonic lionBOT
#

@scarlet quartz

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

scarlet pike
velvet otter
#
import time
import random

durations = []

for x in range(6):
    start = time.time()
    print(random.randrange(1, 1000000000))
    durations.append(time.time() - start)

if durations:
    print(f"Averge time taken to run the code was {sum(durations) / len(durations)} seconds")
scarlet quartz
#

Im getting an average of about 1.3 secs

#

just curious if better hardware would make that time faster

gritty smelt
#
373124080
569400644
829035636
291837383
371323286
433043841
Averge time taken to run the code was 8.463859558105469e-06 seconds

1.3 seconds seems a bit slow. What hardware, what Python version?

#

That's using offby1's code above on Python 3.10.6.

scarlet quartz
gritty smelt
#

There should be some dropdown about selecting a Python. It should show what's current.

#

But 1.3s vs 8us is a huge difference.

scarlet quartz
gritty smelt
#
>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=10, micro=6, releaselevel='final', serial=0)
#

Maybe random.randrange got a reimplementation.

#

Hmm, 3.8 here:

349534162
470901265
280833842
277962006
268003026
245916348
Averge time taken to run the code was 9.338061014811197e-06 seconds

Essentially the same.

#

Can you show the code the 1.3s came from?

scarlet quartz
gritty smelt
#

You said 1.3 seconds. This is in microseconds.

scarlet quartz
#

ohh

gritty smelt
#

Care to show the code and rerun it?

scarlet quartz
#

thats all it is

#
import time
import random

durations = []

for x in range(6):
    start = time.time()
    mian=(random.randrange(1, 1000000000))
    durations.append(time.time() - start)

if durations:
    print(f"Averge time taken to run the code was {sum(durations) / len(durations)} seconds")```
gritty smelt
#

Can you run it and copy/paste the output?

scarlet quartz
#

Averge time taken to run the code was 2.9007593790690103e-06 seconds

gritty smelt
#

That's more like it.

scarlet quartz
#

yah i didn't know that was microseconds

gritty smelt
#

e-06

scarlet quartz
#

yah that makes sense

#

how many micro in a second

gritty smelt
#

1000000

#

10^6, a million

scarlet quartz
#

ok cool

gritty smelt
#

Same 6 as in the e-06.

scarlet quartz
#

so 2 millionth of a second

gritty smelt
#

Closer to 3.

scarlet quartz
#

yah got it cool

gritty smelt
#

The number of iterations is small enough that's you'll see some variance.

scarlet quartz
#

yah i was running it a thousand times and getting closer to the same number

gritty smelt
#

There's a timeit module in the stdlib for running code to measure its average time, it is common to ask it to run the code many times for the average.

scarlet quartz
#

thx for the help

sonic lionBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.