Skip to content

Instantly share code, notes, and snippets.

@mmazur
Last active December 21, 2021 11:40
Show Gist options
  • Save mmazur/c813d12b7e01ad24271e5ba8852294d7 to your computer and use it in GitHub Desktop.
Save mmazur/c813d12b7e01ad24271e5ba8852294d7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import numpy as np
from time import perf_counter
from pandas import to_datetime, Series
from datetime import datetime
fromtimestamp = datetime.fromtimestamp
rng = np.random.default_rng()
ints = Series(rng.integers(1, 1640084533, 200000))
slow_start = perf_counter()
slow = ints.apply(lambda ts: to_datetime(ts, unit="s"))
slow_stop = perf_counter()
fast_start = perf_counter()
fast = ints.apply(lambda ts: fromtimestamp(ts))
fast_stop = perf_counter()
print(f"Slow: {slow_stop-slow_start}\nFast: {fast_stop-fast_start}")
# Outcome;
# [mmazur@klapek ~]$ ./test.py
# Slow: 21.08506918803323
# Fast: 0.16851535497698933
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment