Skip to content

Instantly share code, notes, and snippets.

@FanchenBao
Last active August 18, 2022 22:56
Show Gist options
  • Save FanchenBao/6c02849fd419a95c61ac0c7432addbe4 to your computer and use it in GitHub Desktop.
Save FanchenBao/6c02849fd419a95c61ac0c7432addbe4 to your computer and use it in GitHub Desktop.
An example of using timeit for bench marking
import timeit
setup = """
import pandas as pd
from dateutil import parser
from datetime import datetime
def test_datetime(timestamp):
try:
datetime.fromisoformat(timestamp)
except Exception:
pd.to_datetime(timestamp).to_pydatetime()
def test_dateutil(timestamp):
parser.parse(timestamp)
def test_pandas(timestamp):
pd.to_datetime(timestamp).to_pydatetime()
"""
def time_function(method, timestamp):
timed_code = f'test_{method}("{timestamp}")'
time = min(timeit.Timer(timed_code, setup=setup).repeat(7, 10000))
print("{}: {} ms".format(method, int(time * 1000)))
timestamp = ['2022-08-18T00:00:00', '2022-08-18T00:00:00+00:00', '2022-08-18T00:01:30.000Z']
for t in timestamp:
print(t)
time_function('datetime', t)
time_function("pandas", t)
time_function("dateutil", t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment