#๐ How to get average of end times
43 messages ยท Page 1 of 1 (latest)
@scarlet quartz
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.
append to a list and then divide it by its length
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")
so hypothetically is there any way to lower the duration
Im getting an average of about 1.3 secs
just curious if better hardware would make that time faster
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.
pycharm hwo do i check version
There should be some dropdown about selecting a Python. It should show what's current.
But 1.3s vs 8us is a huge difference.
3.8
>>> 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?
so u said 1 was slow how is 9 not slower
You said 1.3 seconds. This is in microseconds.
ohh
Care to show the code and rerun it?
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")```
Can you run it and copy/paste the output?
Averge time taken to run the code was 2.9007593790690103e-06 seconds
That's more like it.
yah i didn't know that was microseconds
e-06
ok cool
Same 6 as in the e-06.
so 2 millionth of a second
Closer to 3.
yah got it cool
The number of iterations is small enough that's you'll see some variance.
yah i was running it a thousand times and getting closer to the same number
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.
thx for the help
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.