These are the situations tested with both scenarios
def function_1():
from time import perf_counter
from datetime import datetime
t1 = datetime.now().astimezone()
min_tmp = 1_000_000_000
for _ in range(10_000):
tmp1 = perf_counter()
int(t1.replace(hour=0,minute=0,second=0).timestamp())
tmp2 = perf_counter()
min_tmp = min(min_tmp, tmp2-tmp1)
print(f"elapsed {min_tmp*1E6} microseconds (µs)")
from datetime import datetime
from time import mktime
min_tmp = 1_000_000_000
for _ in range(10_000):
t2 = datetime.now().astimezone()
tmp1 = perf_counter()
int(mktime(t2.date().timetuple()))
tmp2 = perf_counter()
min_tmp = min(min_tmp, tmp2-tmp1)
print(f"elapsed {min_tmp*1E6} microseconds (µs)")
function_1()
results
elapsed 1.401989720761776 microseconds (µs)
elapsed 1.8440186977386475 microseconds (µs)
def function_2():
from time import perf_counter_ns
from datetime import datetime
t1 = datetime.now().astimezone()
min_tmp = 1_000_000_000
for _ in range(10_000):
tmp1 = perf_counter_ns()
int(t1.replace(hour=0,minute=0,second=0).timestamp())
tmp2 = perf_counter_ns()
min_tmp = min(min_tmp, tmp2-tmp1)
print(f"elapsed {min_tmp/1E3} microseconds (µs)")
from datetime import datetime
from time import mktime
min_tmp = 1_000_000_000
for _ in range(10_000):
t2 = datetime.now().astimezone()
tmp1 = perf_counter_ns()
int(mktime(t2.date().timetuple()))
tmp2 = perf_counter_ns()
min_tmp = min(min_tmp, tmp2-tmp1)
print(f"elapsed {min_tmp/1E3} microseconds (µs)")
function_2()
results
elapsed 1.402 microseconds (µs)
elapsed 1.844 microseconds (µs)
def function_3():
from timeit import timeit
setup = (
"from datetime import datetime;"
"t1 = datetime.now().astimezone()"
)
statement = (
"int(t1.replace(hour=0,minute=0,second=0).timestamp())"
)
elapsed = timeit(statement, setup)
print(f"elapsed {elapsed}")
# print(f"elapsed {elapsed} microseconds (µs)")
setup = (
"from datetime import datetime;"
"from time import mktime;"
"t2 = datetime.now().astimezone()"
)
statement = (
"int(mktime(t2.date().timetuple()))"
)
elapsed = timeit(statement, setup)
print(f"elapsed {elapsed}")
# print(f"elapsed {elapsed} microseconds (µs)")
function_3()
results
elapsed 1.425439064973034
elapsed 1.778326635947451
$ python -m timeit -s "from datetime import datetime;t1 = datetime.now().astimezone()" -r 10 -n 1000000 "int(t1.replace(hour=0,minute=0,second=0).timestamp())"
1000000 loops, best of 10: 1.39 usec per loop
$ python -m timeit -s "from datetime import datetime;from time import mktime;t2 = datetime.now().astimezone()" -r 10 -n 1000000 "int(mktime(t2.date().timetuple()))"
1000000 loops, best of 10: 1.77 usec per loop
def function_5():
from timeit import timeit
from time import perf_counter_ns
setup = (
"from datetime import datetime;"
"t1 = datetime.now().astimezone()"
)
statement = (
"int(t1.replace(hour=0,minute=0,second=0).timestamp())"
)
elapsed = timeit(statement, setup, timer=perf_counter_ns)
print(f"elapsed {elapsed*1E-9}")
setup = (
"from datetime import datetime;"
"from time import mktime;"
"t2 = datetime.now().astimezone()"
)
statement = (
"int(mktime(t2.date().timetuple()))"
)
elapsed = timeit(statement, setup, timer=perf_counter_ns)
print(f"elapsed {elapsed*1E-9}")
function_5()
results
elapsed 1.392349128
elapsed 1.77519522
not possible since there is no cli option to change default timer when calling timeit
as module
all previous steps where executed in a python env with version 3.11.8 reason why I'm not including that version here
$ python3.12.2 -m timeit -s "from datetime import datetime;t1 = datetime.now().astimezone()" -r 10 -n 1000000 "int(t1.replace(hour=0,minute=0,second=0).timestamp())"
1000000 loops, best of 10: 1.63 usec per loop
$ python3.12.2 -m timeit -s "from datetime import datetime;from time import mktime;t2 = datetime.now().astimezone()" -r 10 -n 1000000 "int(mktime(t2.date().timetuple()))"
1000000 loops, best of 10: 2.02 usec per loop
$ python3.10.13 -m timeit -s "from datetime import datetime;t1 = datetime.now().astimezone()" -r 10 -n 1000000 "int(t1.replace(hour=0,minute=0,second=0).timestamp())"
1000000 loops, best of 10: 1.48 usec per loop
$ python3.10.13 -m timeit -s "from datetime import datetime;from time import mktime;t2 = datetime.now().astimezone()" -r 10 -n 1000000 "int(mktime(t2.date().timetuple()))"
1000000 loops, best of 10: 1.93 usec per loop
$ python3.9.18 -m timeit -s "from datetime import datetime;t1 = datetime.now().astimezone()" -r 10 -n 1000000 "int(t1.replace(hour=0,minute=0,second=0).timestamp())"
1000000 loops, best of 10: 1.47 usec per loop
$ python3.9.18 -m timeit -s "from datetime import datetime;from time import mktime;t2 = datetime.now().astimezone()" -r 10 -n 1000000 "int(mktime(t2.date().timetuple()))"
1000000 loops, best of 10: 1.93 usec per loop
order to read the files:
get-date.alternatives.py
- in this file I list some of the ways that I found through my research in order to get the actual dateget-epoch-start-day.py
- in this file are the final 2 scenarios that I found to get the epoch seconds of the beginning of a selected dateget-epoch-start-day.bench.md
- in this file I walkthrough all the test I made in order to confirm which of the 2 scenarios perform betterlocal-timezone-to-utc-seconds.py
- in this file I list some of the ways that I found to get the difference between my local timezone and UTC in secondslocal-timezone-to-utc-seconds.bench.sh
- in this file I did a benchmark of the ways found in file4.