Skip to content

Instantly share code, notes, and snippets.

@almarklein
Last active March 14, 2023 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save almarklein/20640253fe54fc35d4a4c6dfcc1ad625 to your computer and use it in GitHub Desktop.
Save almarklein/20640253fe54fc35d4a4c6dfcc1ad625 to your computer and use it in GitHub Desktop.
timestamp speed and precision
from datetime import datetime
import numpy as np
import time
n = 100000
mark1 = datetime.now()
t0 = time.perf_counter()
count = 0
for i in range(n):
mark2 = datetime.now()
if mark1 < mark2:
mark1 = mark2
count += 1
print(time.perf_counter() - t0, count)
mark1 = np.datetime64(datetime.now())
t0 = time.perf_counter()
count = 0
for i in range(n):
mark2 = np.datetime64(datetime.now())
if mark1 < mark2:
mark1 = mark2
count += 1
print(time.perf_counter() - t0, count)
mark1 = time.time()
t0 = time.perf_counter()
count = 0
for i in range(n):
mark2 = time.time()
if mark1 < mark2:
mark1 = mark2
count += 1
print(time.perf_counter() - t0, count)
mark1 = time.perf_counter_ns()
t0 = time.perf_counter()
count = 0
for i in range(n):
mark2 = time.perf_counter_ns()
if mark1 < mark2:
mark1 = mark2
count += 1
print(time.perf_counter() - t0, count)
@almarklein
Copy link
Author

On my machine:

0.042509458002314204 42070
0.2643832920002751 100000
0.010956333000649465 10884
0.015090416000020923 100000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment