Skip to content

Instantly share code, notes, and snippets.

@hhatto
Created June 6, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhatto/8f90945916fe76a0c3cf7a2c7d7d332e to your computer and use it in GitHub Desktop.
Save hhatto/8f90945916fe76a0c3cf7a2c7d7d332e to your computer and use it in GitHub Desktop.
from datetime import datetime
from benchmarker import Benchmarker
with Benchmarker(1000*500, width=40) as bench:
@bench("datetime->str(strftime)")
def _(bm):
d = datetime(2019, 1, 2)
for i in bm:
_ = d.strftime("%Y-%m-%d")
@bench("datetime->str(direct)")
def _(bm):
d = datetime(2019, 1, 2)
for i in bm:
_ = "{}-{:02d}-{:02d}".format(d.year, d.month, d.day)
@bench("str->datetime(strptime)")
def _(bm):
d = "2019-01-02"
for i in bm:
_ = datetime.strptime(d, "%Y-%m-%d")
@bench("str->datetime(direct)")
def _(bm):
d = "2019-01-02"
for i in bm:
year, month, day = d.split("-")
_ = datetime(int(year), int(month), int(day))
@hhatto
Copy link
Author

hhatto commented Jun 6, 2019

$ python datetimebench.py
## benchmarker:         release 4.0.1 (for python)
## python version:      3.7.3
## python compiler:     Clang 10.0.1 (clang-1001.0.46.3)
## python platform:     Darwin-18.6.0-x86_64-i386-64bit
## python executable:   /Users/hattori/.virtualenvs/py373/bin/python
## cpu model:           Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
## parameters:          loop=500000, cycle=1, extra=0

##                                            real    (total    = user    + sys)
datetime->str(strftime)                     1.2603    1.2600    1.2600    0.0000
datetime->str(direct)                       0.3558    0.3600    0.3600    0.0000
str->datetime(strptime)                     3.2475    3.2400    3.2400    0.0000
str->datetime(direct)                       0.4091    0.4100    0.4100    0.0000

## Ranking                                    real
datetime->str(direct)                       0.3558  (100.0) ********************
str->datetime(direct)                       0.4091  ( 87.0) *****************
datetime->str(strftime)                     1.2603  ( 28.2) ******
str->datetime(strptime)                     3.2475  ( 11.0) **

## Matrix                                     real    [01]    [02]    [03]    [04]
[01] datetime->str(direct)                  0.3558   100.0   115.0   354.2   912.7
[02] str->datetime(direct)                  0.4091    87.0   100.0   308.1   793.9
[03] datetime->str(strftime)                1.2603    28.2    32.5   100.0   257.7
[04] str->datetime(strptime)                3.2475    11.0    12.6    38.8   100.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment