Last active
August 29, 2015 14:06
-
-
Save kwatch/f923fb5a71da3f69eccb to your computer and use it in GitHub Desktop.
str.startswith() と re.match() のベンチマーク
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 = "https://www.python.org/" | |
with bm.empty(): | |
for _ in xrange(N): | |
pass | |
with bm("re.match()"): | |
for _ in xrange(N): | |
re.match(r'^https://', string) | |
with bm("re.compile().search()"): | |
rexp = re.compile(r'^https://') | |
for _ in xrange(N): | |
rexp.match(string) | |
with bm("str.startswith() #1"): | |
for _ in xrange(N): | |
string.startswith(("http://", "https://")) | |
with bm("str.startswith() #2"): | |
patterns = ("http://", "https://") | |
for _ in xrange(N): | |
string.startswith(patterns) | |
### Result example on CPython 3.4.1: | |
r""" | |
## Ranking real | |
str.startswith() #1 0.0414 (100.0%) ************************* | |
str.startswith() #2 0.0447 ( 92.6%) *********************** | |
re.compile().search() 0.0941 ( 44.0%) *********** | |
re.match() 0.2490 ( 16.6%) **** | |
## Ratio Matrix real [01] [02] [03] [04] | |
[01] str.startswith() #1 0.0414 100.0% 108.0% 227.2% 601.2% | |
[02] str.startswith() #2 0.0447 92.6% 100.0% 210.5% 556.9% | |
[03] re.compile().search() 0.0941 44.0% 47.5% 100.0% 264.6% | |
[04] re.match() 0.2490 16.6% 18.0% 37.8% 100.0% | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment