Skip to content

Instantly share code, notes, and snippets.

@freach
Created August 11, 2016 10:28
Show Gist options
  • Save freach/307c9e5262cdbd75285291c4dc2cdeeb to your computer and use it in GitHub Desktop.
Save freach/307c9e5262cdbd75285291c4dc2cdeeb to your computer and use it in GitHub Desktop.
Compare performance of ciso8601, mxDateTime and udatetime
from __future__ import print_function
import sys
from datetime import datetime
from time import time
try:
import ciso8601
except ImportError:
print('pip install ciso8601')
sys.exit(1)
try:
from mx.DateTime import ISO
import mx.DateTime as DateTime
except ImportError:
print('pip install egenix-mx-base')
sys.exit(1)
try:
import udatetime
except ImportError:
print('pip install udatetime')
sys.exit(1)
import pytz
def get_local_utc_offset():
ts = time()
return (
datetime.fromtimestamp(ts) - datetime.utcfromtimestamp(ts)
).total_seconds() / 60
RFC3339_DATE = '2016-07-18'
RFC3339_TIME = '12:58:26.485897-02:00'
RFC3339_DATE_TIME = RFC3339_DATE + 'T' + RFC3339_TIME
DATE_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%f'
DATETIME_OBJ = datetime.strptime(RFC3339_DATE_TIME[:-6], DATE_TIME_FORMAT)\
.replace(tzinfo=pytz.FixedOffset(get_local_utc_offset() * -1))
TIME = time()
def benchmark_parse():
def _datetime():
dt = datetime.strptime(RFC3339_DATE_TIME[:-6], DATE_TIME_FORMAT)
if RFC3339_DATE_TIME[-6] in ('+', '-'):
negative = False
if RFC3339_DATE_TIME[-6] == '-':
negative = True
(hour, minute) = RFC3339_DATE_TIME[-5:].split(':')
hour = int(hour)
minute = int(minute)
offset = (hour * 60) + minute
if negative:
offset = offset * -1
return dt.replace(tzinfo=pytz.FixedOffset(offset))
return dt.replace(tzinfo=pytz.FixedOffset(0))
def _udatetime():
return udatetime.from_string(RFC3339_DATE_TIME)
def _ciso8601():
return ciso8601.parse_datetime(RFC3339_DATE_TIME)
def _mxdatetime():
return ISO.ParseDateTime(RFC3339_DATE_TIME)
return (_datetime, _udatetime, _ciso8601, _mxdatetime)
def benchmark_format():
def _datetime():
offset = DATETIME_OBJ.tzinfo.utcoffset(None).total_seconds() / 60
tz = ''
if offset < 0:
offset = offset * -1
tz = '-%02d:%02d' % (offset / 60, offset % 60)
else:
tz = '+%02d:%02d' % (offset / 60, offset % 60)
return DATETIME_OBJ.strftime(DATE_TIME_FORMAT) + tz
uda = udatetime.from_string(RFC3339_DATE_TIME)
def _udatetime():
return udatetime.to_string(uda)
mx = ISO.ParseDateTime(RFC3339_DATE_TIME)
def _mxdatetime():
return ISO.str(mx)
return (_datetime, _udatetime, _mxdatetime)
def benchmark_utcnow():
def _datetime():
return datetime.utcnow().replace(tzinfo=pytz.FixedOffset(0))
def _udatetime():
return udatetime.utcnow()
def _mxdatetime():
return DateTime.gmt()
return (_datetime, _udatetime, _mxdatetime)
def benchmark_now():
def _datetime():
return datetime.now().replace(
tzinfo=pytz.FixedOffset(get_local_utc_offset())
)
def _udatetime():
return udatetime.now()
def _mxdatetime():
return DateTime.now()
return (_datetime, _udatetime, _mxdatetime)
def benchmark_fromtimestamp():
def _datetime():
return datetime.fromtimestamp(TIME).replace(
tzinfo=pytz.FixedOffset(get_local_utc_offset())
)
def _udatetime():
return udatetime.fromtimestamp(TIME)
def _mxdatetime():
return DateTime.DateTimeFromTicks(TIME)
return (_datetime, _udatetime, _mxdatetime)
def benchmark_utcfromtimestamp():
def _datetime():
return datetime.utcfromtimestamp(TIME)\
.replace(tzinfo=pytz.FixedOffset(0))
def _udatetime():
return udatetime.utcfromtimestamp(TIME)
def _mxdatetime():
return DateTime.gmtime(TIME)
return (_datetime, _udatetime, _mxdatetime)
if __name__ == '__main__':
import timeit
benchmarks = [
benchmark_parse,
benchmark_format,
benchmark_utcnow,
benchmark_now,
benchmark_fromtimestamp,
benchmark_utcfromtimestamp,
]
test_only = False
if len(sys.argv) == 2 and sys.argv[1] == 'test':
test_only = True
print('Executing benchmarks ...')
for k in benchmarks:
print('\n============ %s' % k.__name__)
mins = []
for func in k():
if test_only:
print(func.__name__, func())
else:
times =\
timeit.repeat('func()', setup='from __main__ import func')
t = min(times)
mins.append(t)
print(func.__name__, t, times)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment