[Python-checkins] r46459 - sandbox/trunk/stringbench/stringbench.py
andrew.dalke
python-checkins at python.org
Sat May 27 15:33:20 CEST 2006
Author: andrew.dalke
Date: Sat May 27 15:33:19 2006
New Revision: 46459
Modified:
sandbox/trunk/stringbench/stringbench.py
Log:
Changed the timings for the longer tests so they don't take so long.
Fixed duplicate function names and comment strings.
Modified: sandbox/trunk/stringbench/stringbench.py
==============================================================================
--- sandbox/trunk/stringbench/stringbench.py (original)
+++ sandbox/trunk/stringbench/stringbench.py Sat May 27 15:33:19 2006
@@ -36,8 +36,13 @@
_RANGE_100 = range(100)
_RANGE_10 = range(10)
+dups = {}
def bench(s, group, repeat_count):
def blah(f):
+ if f.__name__ in dups:
+ raise AssertionError("Multiple functions with same name: %r" %
+ (f.__name__,))
+ dups[f.__name__] = 1
f.comment = s
f.is_bench = True
f.group = group
@@ -79,29 +84,29 @@
for x in _RANGE_1000:
s2 in s1
- at bench('"BC" in ("AB"*1000+"C")', "late match, two characters", 1000)
+ at bench('"BC" in ("AB"*300+"C")', "late match, two characters", 1000)
def in_test_slow_match_two_characters(STR):
- s1 = STR("AB" * 1000+"C")
+ s1 = STR("AB" * 300+"C")
s2 = STR("BC")
for x in _RANGE_1000:
s2 in s1
- at bench('s="ABC"*33; s in ((s+"D")*500+s+"E")',
+ at bench('s="ABC"*33; (s+"E") in ((s+"D")*300+s+"E")',
"late match, 100 characters", 100)
def in_test_slow_match_100_characters(STR):
m = STR("ABC"*33)
- s1 = (m+"D")*500 + m+"E"
+ s1 = (m+"D")*300 + m+"E"
s2 = m+"E"
for x in _RANGE_100:
s2 in s1
# Try with regex
@uses_re
- at bench('s="ABC"*33; re.compile(s+"D").search((s+"D")*500+s+"E")',
+ at bench('s="ABC"*33; re.compile(s+"D").search((s+"D")*300+s+"E")',
"late match, 100 characters", 100)
def re_test_slow_match_100_characters(STR):
m = STR("ABC"*33)
- s1 = (m+"D")*500 + m+"E"
+ s1 = (m+"D")*300 + m+"E"
s2 = m+"E"
pat = re.compile(s2)
search = pat.search
@@ -148,9 +153,9 @@
for x in _RANGE_1000:
s1_find(s2)
- at bench('"BC" in ("AB"*1000+"C")', "late match, two characters", 1000)
+ at bench('("AB"*300+"C").find("BC")', "late match, two characters", 1000)
def find_test_slow_match_two_characters(STR):
- s1 = STR("AB" * 1000+"C")
+ s1 = STR("AB" * 300+"C")
s2 = STR("BC")
s1_find = s1.find
for x in _RANGE_1000:
@@ -188,9 +193,9 @@
for x in _RANGE_1000:
s1_index(s2)
- at bench('("AB"*1000+"C").index("BC")', "late match, two characters", 1000)
+ at bench('("AB"*300+"C").index("BC")', "late match, two characters", 1000)
def index_test_slow_match_two_characters(STR):
- s1 = STR("AB" * 1000+"C")
+ s1 = STR("AB" * 300+"C")
s2 = STR("BC")
s1_index = s1.index
for x in _RANGE_1000:
@@ -243,7 +248,7 @@
@bench('s1+s2+s3+s4+...+s20', "concat 20 strings of words length 4 to 15",
1000)
-def concat_two_strings(STR):
+def concat_many_strings(STR):
s1=STR('TIXSGYNREDCVBHJ')
s2=STR('PUMTLXBZVDO')
s3=STR('FVZNJ')
@@ -790,13 +795,13 @@
pat_sub(to_str, s)
@bench('"...text.with.2000.lines...replace("\\n", " ")',
- 'replace single character, big string', 100)
+ 'replace single character, big string', 10)
def replace_single_character_big(STR):
s = _get_2000_lines(STR)
from_str = STR("\n")
to_str = STR(" ")
s_replace = s.replace
- for x in _RANGE_100:
+ for x in _RANGE_10:
s_replace(from_str, to_str)
@uses_re
@@ -821,26 +826,15 @@
for x in _RANGE_10:
seq_replace(from_str, to_str)
-# This changes the total number of character
- at bench('"...text.with.2000.newlines.',
- 'replace multiple characters, big string', 10)
-def replace_multiple_character_big(STR):
- s = _get_2000_lines(STR)
- from_str = STR("\n")
- to_str = STR("\r\n")
- s_replace = s.replace
- for x in _RANGE_10:
- s_replace(from_str, to_str)
-
# This increases the character count
@bench('"...text.with.2000.newlines...replace("\\n", "\\r\\n")',
- 'replace multiple characters, big string', 100)
+ 'replace and expand multiple characters, big string', 10)
def replace_multiple_character_big(STR):
s = _get_2000_lines(STR)
from_str = STR("\n")
to_str = STR("\r\n")
s_replace = s.replace
- for x in _RANGE_100:
+ for x in _RANGE_10:
s_replace(from_str, to_str)
@@ -1030,11 +1024,11 @@
print "That was zippy!"
else:
try:
- average = str_total/uni_total
+ ratio = str_time/uni_time
except ZeroDivisionError:
- average = 0.0
+ ratio = 0.0
print "%.2f\t%.2f\t%.1f\t%s" % (
- 1000*str_total, 1000*uni_total, 100.*average,
+ 1000*str_total, 1000*uni_total, 100.*ratio,
"TOTAL")
if __name__ == "__main__":
More information about the Python-checkins
mailing list