-
-
Save bbengfort/bc7f985b2b18d789a30d8a52145aed8b to your computer and use it in GitHub Desktop.
Benchmark for/else loop vs function for date parsing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pytz | |
import time | |
import random | |
import numpy as np | |
import pandas as pd | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
from functools import wraps | |
from datetime import datetime | |
HUMAN_DATETIME = "%a %b %d %H:%M:%S %Y %z" | |
HUMAN_DATE = "%b %d, %Y" | |
HUMAN_TIME = "%I:%M:%S %p" | |
HUMAN_HOUR_TZ = "%-I:%M%p %Z" | |
JSON_DATETIME = "%Y-%m-%dT%H:%M:%S.%fZ" # Must be UTC | |
ISO8601_DATETIMEX = "%Y-%m-%dT%H:%M:%S" | |
ISO8601_DATETIME = "%Y-%m-%dT%H:%M:%S%z" | |
RFC3339_DATETIME = ISO8601_DATETIME | |
ISO8601_DATE = "%Y-%m-%d" | |
ISO8601_TIME = "%H:%M:%S" | |
COMMON_DATETIME = "%d/%b/%Y:%H:%M:%S %z" | |
WEB_UTC_DATETIME = "%a, %b %d, %Y at %H:%M UTC" | |
PG_DATE_FMT = "%Y-%m-%d" | |
PG_TIMESTAMP_FMT = "%Y-%m-%d %H:%M:%S" # Must be UTC | |
DATETIME_FORMATS = ( | |
HUMAN_DATETIME, | |
HUMAN_DATE, | |
HUMAN_TIME, | |
JSON_DATETIME, | |
ISO8601_DATETIMEX, | |
ISO8601_DATETIME, | |
RFC3339_DATETIME, | |
ISO8601_DATE, | |
ISO8601_TIME, | |
COMMON_DATETIME, | |
WEB_UTC_DATETIME, | |
PG_DATE_FMT, | |
PG_TIMESTAMP_FMT, | |
) | |
def timeit(func): | |
@wraps(func) | |
def timer_wrapper(*args, **kwargs): | |
""" | |
Inner function that uses the Timer context object | |
""" | |
start = time.time() | |
func(*args, **kwargs) | |
return time.time() - start | |
return timer_wrapper | |
def generate_dateset(n=100): | |
return [ | |
pytz.utc.localize(datetime.utcnow()).strftime(random.choice(DATETIME_FORMATS)) | |
for _ in range(n) | |
] | |
@timeit | |
def benchmark_function(dateset): | |
def parse_timestamp(ts): | |
for fmt in DATETIME_FORMATS: | |
try: | |
return datetime.strptime(ts, fmt) | |
except ValueError: | |
continue | |
raise ValueError("could not parse timestamp") | |
for ts in dateset: | |
ts = parse_timestamp(ts) | |
@timeit | |
def benchmark_forelse(dateset): | |
for ts in dateset: | |
# Try to parse a timestamp with a bunch of formats | |
for fmt in DATETIME_FORMATS: | |
try: | |
ts = datetime.strptime(ts, fmt) | |
break | |
except ValueError: | |
continue | |
else: | |
# Could not parse with any of the formats required | |
raise ValueError("could not parse timestamp") | |
if __name__ == '__main__': | |
index = [] | |
funcb = [] | |
foreb = [] | |
for n in np.linspace(1000, 500000, 10): | |
n = int(n) | |
index.append(n) | |
dateset = generate_dateset(n) | |
funcb.append(benchmark_function(dateset)) | |
foreb.append(benchmark_forelse(dateset)) | |
print("{} parses".format(n)) | |
print("benchmark complete") | |
funcb = pd.Series(funcb, index=index, name="function") | |
foreb = pd.Series(foreb, index=index, name="for/else") | |
sns.set_style('whitegrid') | |
sns.set_context('notebook') | |
_, ax = plt.subplots(figsize=(9,6)) | |
funcb.plot(ax=ax, c='b') | |
foreb.plot(ax=ax, c='g') | |
ax.legend(frameon=True) | |
ax.set_ylabel("time in seconds") | |
ax.set_xlabel("number of dates parsed") | |
plt.savefig("2018-05-17-benchmark.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment