Skip to content

Instantly share code, notes, and snippets.

@nekoya
Last active December 18, 2015 17:38
Show Gist options
  • Save nekoya/5819512 to your computer and use it in GitHub Desktop.
Save nekoya/5819512 to your computer and use it in GitHub Desktop.
import calendar, pytz, time
from datetime import datetime
from benchmarker import Benchmarker
jst = pytz.timezone('Asia/Tokyo')
now_utc = datetime.now(pytz.utc)
now_jst = datetime.now(jst)
with Benchmarker(width=20, loop=10000) as bm:
for _ in bm('utc2utc'):
now_utc.astimezone(pytz.utc)
for _ in bm('jst2utc'):
now_jst.astimezone(pytz.utc)
for _ in bm('jst2jst'):
now_jst.astimezone(jst)
for _ in bm('jst2jst_each'):
now_jst.astimezone(pytz.timezone('Asia/Tokyo'))
"""
on MacBookAir late 2010
## benchmarker: release 3.0.1 (for python)
## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5664)]
## python version: 2.7.1
## python executable: /Users/ryo/mypython/bin/python
## user sys total real
utc2utc 0.0000 0.0000 0.0000 0.0079
jst2utc 0.0900 0.0000 0.0900 0.0889
jst2jst 0.1700 0.0000 0.1700 0.1801
jst2jst_each 0.2700 0.0000 0.2700 0.2891
## Ranking real
utc2utc 0.0079 (100.0%) *************************
jst2utc 0.0889 ( 8.9%) **
jst2jst 0.1801 ( 4.4%) *
jst2jst_each 0.2891 ( 2.7%) *
## Ratio Matrix real [01] [02] [03] [04]
[01] utc2utc 0.0079 100.0% 1125.6% 2279.7% 3659.3%
[02] jst2utc 0.0889 8.9% 100.0% 202.5% 325.1%
[03] jst2jst 0.1801 4.4% 49.4% 100.0% 160.5%
[04] jst2jst_each 0.2891 2.7% 30.8% 62.3% 100.0%
"""
import calendar, pytz, time
from datetime import datetime
from benchmarker import Benchmarker
jst = pytz.timezone('Asia/Tokyo')
now = datetime.now()
now_utc = datetime.now(pytz.utc)
now_jst = datetime.now(pytz.timezone('Asia/Tokyo'))
with Benchmarker(width=20, loop=10000) as bm:
for _ in bm('create naive'):
datetime.now()
for _ in bm('create aware'):
datetime.now(pytz.utc)
for _ in bm('create aware jst'):
datetime.now(jst)
for _ in bm('naive strftime'):
now.strftime('%Y-%m-%d %H:%M:%S')
for _ in bm('aware strftime'):
now_utc.strftime('%Y-%m-%d %H:%M:%S')
for _ in bm('aware jst strftime'):
now_jst.strftime('%Y-%m-%d %H:%M:%S')
"""
on MacBookAir late 2010
## benchmarker: release 3.0.1 (for python)
## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5664)]
## python version: 2.7.1
## python executable: /Users/ryo/mypython/bin/python
## user sys total real
create naive 0.0200 0.0000 0.0200 0.0255
create aware 0.0900 0.0000 0.0900 0.0988
create aware jst 0.1800 0.0000 0.1800 0.1753
naive strftime 0.1400 0.0000 0.1400 0.1409
aware strftime 0.1600 0.0000 0.1600 0.1648
aware jst strftime 0.1700 0.0000 0.1700 0.1724
## Ranking real
create naive 0.0255 (100.0%) *************************
create aware 0.0988 ( 25.9%) ******
naive strftime 0.1409 ( 18.1%) *****
aware strftime 0.1648 ( 15.5%) ****
aware jst strftime 0.1724 ( 14.8%) ****
create aware jst 0.1753 ( 14.6%) ****
## Ratio Matrix real [01] [02] [03] [04] [05] [06]
[01] create naive 0.0255 100.0% 386.8% 551.9% 645.7% 675.2% 686.6%
[02] create aware 0.0988 25.9% 100.0% 142.7% 166.9% 174.5% 177.5%
[03] naive strftime 0.1409 18.1% 70.1% 100.0% 117.0% 122.4% 124.4%
[04] aware strftime 0.1648 15.5% 59.9% 85.5% 100.0% 104.6% 106.3%
[05] aware jst strftime 0.1724 14.8% 57.3% 81.7% 95.6% 100.0% 101.7%
[06] create aware jst 0.1753 14.6% 56.3% 80.4% 94.0% 98.3% 100.0%
"""
import calendar, pytz, time
from datetime import datetime
from benchmarker import Benchmarker
now_utc = datetime.now(pytz.utc)
now_jst = datetime.now(pytz.timezone('Asia/Tokyo'))
utcnow = datetime.utcnow()
localnow = datetime.now()
with Benchmarker(width=20, loop=10000) as bm:
for _ in bm('timegm naive'):
calendar.timegm(utcnow.timetuple())
for _ in bm('timegm aware'):
calendar.timegm(now_utc.timetuple())
for _ in bm('mktime naive'):
int(time.mktime(localnow.timetuple()))
for _ in bm('mktime aware'):
int(time.mktime(now_jst.timetuple()))
for _ in bm('strftime naive'):
localnow.strftime('%s')
for _ in bm('strftime aware'):
now_jst.strftime('%s')
"""
on MacBookAir late 2010
## benchmarker: release 3.0.1 (for python)
## python platform: darwin [GCC 4.2.1 (Apple Inc. build 5664)]
## python version: 2.7.1
## python executable: /Users/ryo/mypython/bin/python
## user sys total real
timegm naive 0.0600 0.0000 0.0600 0.0746
timegm aware 0.0800 0.0000 0.0800 0.0966
mktime naive 0.1300 0.0000 0.1300 0.1442
mktime aware 0.1500 0.0000 0.1500 0.1863
strftime naive 0.1800 0.0100 0.1900 0.2832
strftime aware 0.2200 0.0000 0.2200 0.2810
## Ranking real
timegm naive 0.0746 (100.0%) *************************
timegm aware 0.0966 ( 77.2%) *******************
mktime naive 0.1442 ( 51.7%) *************
mktime aware 0.1863 ( 40.0%) **********
strftime aware 0.2810 ( 26.5%) *******
strftime naive 0.2832 ( 26.3%) *******
## Ratio Matrix real [01] [02] [03] [04] [05] [06]
[01] timegm naive 0.0746 100.0% 129.5% 193.4% 249.9% 376.9% 379.8%
[02] timegm aware 0.0966 77.2% 100.0% 149.3% 193.0% 290.9% 293.2%
[03] mktime naive 0.1442 51.7% 67.0% 100.0% 129.2% 194.9% 196.4%
[04] mktime aware 0.1863 40.0% 51.8% 77.4% 100.0% 150.8% 152.0%
[05] strftime aware 0.2810 26.5% 34.4% 51.3% 66.3% 100.0% 100.8%
[06] strftime naive 0.2832 26.3% 34.1% 50.9% 65.8% 99.2% 100.0%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment