Skip to content

Instantly share code, notes, and snippets.

@kwatch
Created September 14, 2014 06:53
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 kwatch/e1bc95fcc6cb75c60c94 to your computer and use it in GitHub Desktop.
Save kwatch/e1bc95fcc6cb75c60c94 to your computer and use it in GitHub Desktop.
str.isdigit() と re.match() のベンチマーク
# -*- coding: utf-8 -*-
import sys, re
from benchmarker import Benchmarker
try:
xrange
except:
xrange = range
N = 100000
for bm in Benchmarker(width=30, cycle=10, extra=1):
string = "20140914"
with bm.empty():
for _ in xrange(N):
pass
with bm("re.search()"):
for _ in xrange(N):
re.search(r'^\d+$', string)
with bm("re.compile().search()"):
rexp = re.compile(r'^\d+$')
for _ in xrange(N):
rexp.search(string)
with bm("str.isdigit()"):
for _ in xrange(N):
string.isdigit()
### Result example on CPython 3.4.1:
r"""
## Ranking real
str.isdigit() 0.0204 (100.0%) *************************
re.compile().search() 0.1016 ( 20.1%) *****
re.search() 0.2982 ( 6.9%) **
## Ratio Matrix real [01] [02] [03]
[01] str.isdigit() 0.0204 100.0% 497.2% 1459.6%
[02] re.compile().search() 0.1016 20.1% 100.0% 293.5%
[03] re.search() 0.2982 6.9% 34.1% 100.0%
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment