Code: Select all
import datetime
x = 0
y = datetime.datetime.now()
sec = y.second+1
sec2 = 0
while sec2 <= sec:
y = datetime.datetime.now()
sec2 = y.second
x += 1
print(sec,x,sec2)
Code: Select all
import datetime
x = 0
y = datetime.datetime.now()
sec = y.second+1
sec2 = 0
while sec2 <= sec:
y = datetime.datetime.now()
sec2 = y.second
x += 1
print(sec,x,sec2)
Code: Select all
36 3226509 37 interval length 1.873971700668335
38 2153708 39 interval length 1.2862308025360107
40 1725832 41 interval length 1.077143669128418
42 2223634 43 interval length 1.251556396484375
44 1861549 45 interval length 1.0645787715911865
46 2343877 47 interval length 1.3598928451538086
48 3093324 49 interval length 1.7761437892913818
50 2428348 51 interval length 1.3990514278411865
52 2947491 53 interval length 1.6705169677734375
54 1760270 55 interval length 1.0613574981689453
56 1972413 57 interval length 1.1316509246826172
58 2722967 59 interval length 1.7606496810913086
Code: Select all
import datetime
import time
import random
def execute():
x = 0
y = datetime.datetime.now()
sec = y.second+1
t_sec = time.time()
sec2 = 0
while sec2 <= sec:
y = datetime.datetime.now()
sec2 = y.second
t_sec2 = time.time()
x += 1
print(sec,x,sec2, " ", "interval length", t_sec2-t_sec)
for _ in range(20):
time.sleep( random.random() )
execute()
Code: Select all
import time
seconds = 0
counter = 0
time.sleep(1)
start_time = time.monotonic()
while True:
current_time = time.monotonic()
elapsed_time = current_time - start_time
if elapsed_time > seconds:
print("Finished iterating in: " + str(int(elapsed_time)) + " seconds")
break
else:
counter+=1
print(counter)
It almost surely takes more time to query the clock than add the numbers.
Code: Select all
from datetime import datetime, timedelta
def execute():
x=0
stoptime = datetime.now() + timedelta(seconds=1)
while datetime.now() < stoptime:
x += 1
print ("I did {} iterations in 1 second".format(x))
for _ in range(20):
execute()
I think you need to use python3 on the Pi as the python command runs version 2 by default. I changed the code to readkheylen25 wrote: ↑Thu May 06, 2021 4:38 pmThat was a pretty interesting exercise.
I came up with this code:On my Windows machine (Python 3.9 and VS Code), I got pretty consistent results:Code: Select all
from datetime import datetime, timedelta def execute(): x=0 stoptime = datetime.now() + timedelta(seconds=1) while datetime.now() < stoptime: x += 1 print ("I did {} iterations in 1 second".format(x)) for _ in range(20): execute()
I did 1664617 iterations in 1 second
I did 1703692 iterations in 1 second
I did 1754271 iterations in 1 second
I did 1575467 iterations in 1 second
I did 1809778 iterations in 1 second
I did 1795219 iterations in 1 second
I did 1805086 iterations in 1 second
....
On the same machine, but using Python 3.8 under WSL under Ubuntu through VS Code, I got results more in the 650.000 range, over two and a half times slower.
Now I want to run this on a Pi so badly, but I got both of them working for me elsewhere at the moment
Code: Select all
import time
seconds = 1
counter = 0
time.sleep(1)
start_time = time.monotonic()
while True:
current_time = time.monotonic()
elapsed_time = current_time - start_time
if elapsed_time > seconds:
print("Finished iterating in: " + str(elapsed_time) + " seconds")
print("I did "+str(counter/elapsed_time)+" iterations per second ")
break
else:
counter+=1
Code: Select all
$ python3 sectest.py
Finished iterating in: 1.0000003422610462 seconds
I did 618416.788340023 iterations per second
Code: Select all
from datetime import datetime, timedelta
iterations = 4000000
x=0
starttime = datetime.now()
for _ in range(iterations):
x += 1
elapsed = (datetime.now()-starttime).total_seconds()
print('I did {} iterations in {} seconds'.format(x, elapsed))
print('Which is {} iterations per second'.format(x/elapsed))