[Python-checkins] r46083 - sandbox/trunk/stringbench sandbox/trunk/stringbench/stringbench.py

andrew.dalke python-checkins at python.org
Mon May 22 20:38:17 CEST 2006


Author: andrew.dalke
Date: Mon May 22 20:38:16 2006
New Revision: 46083

Added:
   sandbox/trunk/stringbench/
   sandbox/trunk/stringbench/stringbench.py   (contents, props changed)
Log:
"Need for speed" microbenchmarks for string processing



Added: sandbox/trunk/stringbench/stringbench.py
==============================================================================
--- (empty file)
+++ sandbox/trunk/stringbench/stringbench.py	Mon May 22 20:38:16 2006
@@ -0,0 +1,662 @@
+
+import timeit
+import itertools
+import operator
+import re
+import sys
+import datetime
+
+print sys.version
+print datetime.datetime.now()
+
+REPEAT = 1
+REPEAT = 3
+
+if __name__ != "__main__":
+    raise SystemExit("Must run as main program")
+
+_RANGE_1000 = range(1000)
+_RANGE_100 = range(100)
+
+def bench(s="", group=1):
+    def blah(f):
+        f.comment = s
+        f.is_bench = True
+        f.group = group
+        return f
+    return blah
+
+####### 'in' comparisons
+
+
+ at bench('"A" in "A"*1000', group="early match, single character")
+def in_test_quick_match_single_character(STR):
+    s1 = STR("A" * 1000)
+    s2 = STR("A")
+    for x in _RANGE_1000:
+        s2 in s1
+
+ at bench('"B" in "A"*1000', group="no match, single character")
+def in_test_no_match_single_character(STR):
+    s1 = STR("A" * 1000)
+    s2 = STR("B")
+    for x in _RANGE_1000:
+        s2 in s1
+
+
+ at bench('"AB" in "AB"*1000', group="early match, two characters")
+def in_test_quick_match_two_characters(STR):
+    s1 = STR("AB" * 1000)
+    s2 = STR("AB")
+    for x in _RANGE_1000:
+        s2 in s1
+
+ at bench('"BC" in "AB"*1000', group="no match, two characters")
+def in_test_no_match_two_character(STR):
+    s1 = STR("AB" * 1000)
+    s2 = STR("BC")
+    for x in _RANGE_1000:
+        s2 in s1
+
+ at bench('"BC" in ("AB"*1000+"C")', group="late match, two characters")
+def in_test_slow_match_two_characters(STR):
+    s1 = STR("AB" * 1000+"C")
+    s2 = STR("BC")
+    for x in _RANGE_1000:
+        s2 in s1
+
+ at bench('s="ABC"*33; s in ((s+"D")*500+s+"E")',
+       group="late match, 100 characters")
+def in_test_slow_match_100_characters(STR):
+    m = STR("ABC"*33)
+    s1 = (m+"D")*500 + m+"E"
+    s2 = m+"E"
+    for x in _RANGE_100:
+        s2 in s1
+
+# Try with regex
+ at bench('s="ABC"*33; re.compile(s+"D").search((s+"D")*500+s+"E")',
+       group="late match, 100 characters")
+def re_test_slow_match_100_characters(STR):
+    m = STR("ABC"*33)
+    s1 = (m+"D")*500 + m+"E"
+    s2 = m+"E"
+    pat = re.compile(s2)
+    search = pat.search
+    for x in _RANGE_100:
+        search(s1)
+        
+
+#### same tests as 'in' but use 'find'
+
+ at bench('("A"*1000).find("A")', group="early match, single character")
+def find_quick_match_single_character(STR):
+    s1 = STR("A" * 1000)
+    s2 = STR("A")
+    s1_find = s1.find
+    for x in _RANGE_1000:
+        s1_find(s2)
+
+ at bench('("A"*1000).find("B")', group="no match, single character")
+def find_test_no_match_single_character(STR):
+    s1 = STR("A" * 1000)
+    s2 = STR("B")
+    s1_find = s1.find
+    for x in _RANGE_1000:
+        s1_find(s2)
+
+
+ at bench('("AB"*1000).find("AB")', group="early match, two characters")
+def find_test_quick_match_two_characters(STR):
+    s1 = STR("AB" * 1000)
+    s2 = STR("AB")
+    s1_find = s1.find
+    for x in _RANGE_1000:
+        s1_find(s2)
+
+ at bench('("AB"*1000).find("BC")', group="no match, two characters")
+def find_test_no_match_two_character(STR):
+    s1 = STR("AB" * 1000)
+    s2 = STR("BC")
+    s1_find = s1.find
+    for x in _RANGE_1000:
+        s1_find(s2)
+
+ at bench('"BC" in ("AB"*1000+"C")', group="late match, two characters")
+def find_test_slow_match_two_characters(STR):
+    s1 = STR("AB" * 1000+"C")
+    s2 = STR("BC")
+    s1_find = s1.find
+    for x in _RANGE_1000:
+        s1_find(s2)
+
+ at bench('s="ABC"*33; ((s+"D")*500+s+"E").find(s)',
+       group="late match, 100 characters")
+def find_test_slow_match_100_characters(STR):
+    m = STR("ABC"*33)
+    s1 = (m+"D")*500 + m+"E"
+    s2 = m+"E"
+    s1_find = s1.find
+    for x in _RANGE_100:
+        s1_find(s2)
+
+#### Now with index.
+# Skip the ones which fail because the
+
+        
+ at bench('("A"*1000).index("A")', group="early match, single character")
+def index_test_quick_match_single_character(STR):
+    s1 = STR("A" * 1000)
+    s2 = STR("A")
+    s1_index = s1.index
+    for x in _RANGE_1000:
+        s1_index(s2)
+
+
+ at bench('("AB"*1000).index("AB")', group="early match, two characters")
+def index_test_quick_match_two_characters(STR):
+    s1 = STR("AB" * 1000)
+    s2 = STR("AB")
+    s1_index = s1.index
+    for x in _RANGE_1000:
+        s1_index(s2)
+
+ at bench('("AB"*1000+"C").index("BC")', group="late match, two characters")
+def index_test_slow_match_two_characters(STR):
+    s1 = STR("AB" * 1000+"C")
+    s2 = STR("BC")
+    s1_index = s1.index
+    for x in _RANGE_1000:
+        s1_index(s2)
+
+ at bench('s="ABC"*33; ((s+"D")*500+s+"E").index(s)',
+       group="late match, 100 characters")
+def index_test_slow_match_100_characters(STR):
+    m = STR("ABC"*33)
+    s1 = (m+"D")*500 + m+"E"
+    s2 = m+"E"
+    s1_index = s1.index
+    for x in _RANGE_100:
+        s1_index(s2)
+
+#### Benchmark the operator-based methods
+
+ at bench('"A"*10', "repeat 1 character 10 times")
+def repeat_single_10_times(STR):
+    s = STR("A")
+    for x in _RANGE_1000:
+        s * 10
+
+ at bench('"A"*1000', "repeat 1 character 1000 times")
+def repeat_single_1000_times(STR):
+    s = STR("A")
+    for x in _RANGE_1000:
+        s * 1000
+
+ at bench('"ABCDE"*10', "repeat 5 characters 10 times")
+def repeat_5_10_times(STR):
+    s = STR("ABCDE")
+    for x in _RANGE_1000:
+        s * 10
+
+ at bench('"ABCDE"*1000', "repeat 5 characters 1000 times")
+def repeat_5_1000_times(STR):
+    s = STR("ABCDE")
+    for x in _RANGE_1000:
+        s * 1000
+        
+# + for concat
+
+ at bench('"Andrew"+"Dalke"', "concat two strings")
+def concat_two_strings(STR):
+    s1 = STR("Andrew")
+    s2 = STR("Dalke")
+    for x in _RANGE_1000:
+        s1+s2
+
+ at bench('s1+s2+s3+s4+...+s20', "concat 20 strings of words length 4 to 15")
+def concat_two_strings(STR):
+    s1=STR('TIXSGYNREDCVBHJ')
+    s2=STR('PUMTLXBZVDO')
+    s3=STR('FVZNJ')
+    s4=STR('OGDXUW')
+    s5=STR('WEIMRNCOYVGHKB')
+    s6=STR('FCQTNMXPUZH')
+    s7=STR('TICZJYRLBNVUEAK')
+    s8=STR('REYB')
+    s9=STR('PWUOQ')
+    s10=STR('EQHCMKBS')
+    s11=STR('AEVDFOH')
+    s12=STR('IFHVD')
+    s13=STR('JGTCNLXWOHQ')
+    s14=STR('ITSKEPYLROZAWXF')
+    s15=STR('THEK')
+    s16=STR('GHPZFBUYCKMNJIT')
+    s17=STR('JMUZ')
+    s18=STR('WLZQMTB')
+    s19=STR('KPADCBW')
+    s20=STR('TNJHZQAGBU')
+    for x in _RANGE_1000:
+        (s1 + s2+ s3+ s4+ s5+ s6+ s7+ s8+ s9+s10+
+         s11+s12+s13+s14+s15+s16+s17+s18+s19+s20)
+
+
+#### Benchmark join
+
+ at bench('"A".join("")',
+       "join empty string, with 1 character sep")
+def join_empty_single(STR):
+    sep = STR("A")
+    s2 = STR("")
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"ABCDE".join("")',
+       "join empty string, with 5 character sep")
+def join_empty_5(STR):
+    sep = STR("ABCDE")
+    s2 = STR("")
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"A".join("ABC..Z")',
+       "join string with 26 characters, with 1 character sep")
+def join_alphabet_single(STR):
+    sep = STR("A")
+    s2 = STR("ABCDEFGHIJKLMnOPQRSTUVWXYZ")
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"ABCDE".join("ABC..Z")',
+       "join string with 26 characters, with 5 character sep")
+def join_alphabet_5(STR):
+    sep = STR("ABCDE")
+    s2 = STR("ABCDEFGHIJKLMnOPQRSTUVWXYZ")
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"A".join(list("ABC..Z"))',
+       "join list of 26 characters, with 1 character sep")
+def join_alphabet_list_single(STR):
+    sep = STR("A")
+    s2 = list(STR("ABCDEFGHIJKLMnOPQRSTUVWXYZ"))
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"ABCDE".join(list("ABC..Z"))',
+       "join list of 26 characters, with 5 character sep")
+def join_alphabet_list_five(STR):
+    sep = STR("ABCDE")
+    s2 = list(STR("ABCDEFGHIJKLMnOPQRSTUVWXYZ"))
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"A".join(["Bob"]*1000))',
+       "join list of 1000 words, with 1 character sep")
+def join_1000_words_single(STR):
+    sep = STR("A")
+    s2 = [STR("Bob")]*1000
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+ at bench('"ABCDE".join(["Bob"]*1000))',
+       "join list of 1000 words, with 5 character sep")
+def join_1000_words_5(STR):
+    sep = STR("ABCDE")
+    s2 = [STR("Bob")]*1000
+    sep_join = sep.join
+    for x in _RANGE_1000:
+        sep_join(s2)
+
+#### split tests
+
+ at bench('"this\\nis\\na\\ntest\\n".split("\\n")', "split newlines")
+def newlines_split(STR):
+    s = STR("this\nis\na\ntest\n")
+    s_split = s.split
+    for x in _RANGE_1000:
+        s_split("\n")
+        
+ at bench('"this\\nis\\na\\ntest\\n".rsplit("\\n")', "split newlines")
+def newlines_rsplit(STR):
+    s = STR("this\nis\na\ntest\n")
+    s_rsplit = s.rsplit
+    for x in _RANGE_1000:
+        s_rsplit("\n")
+        
+ at bench('"this\\nis\\na\\ntest\\n".splitlines()', "split newlines")
+def newlines_splitlines(STR):
+    s = STR("this\nis\na\ntest\n")
+    s_splitlines = s.splitlines
+    for x in _RANGE_1000:
+        s_splitlines()
+
+## split text with 2000 newlines
+    
+def _make_2000_lines():
+    import random
+    r = random.Random(100)
+    s = "".join(map(str, range(32, 128)))
+    s = s*4
+    words = []
+    for i in range(2000):
+        start = r.randrange(0, 26)
+        n = r.randint(5, 65)
+        words.append(s[start:n])
+    return "\n".join(words)+"\n"
+
+_text_with_2000_lines = _make_2000_lines()
+_text_with_2000_lines_unicode = unicode(_text_with_2000_lines)
+def _get_2000_lines(STR):
+    if STR is unicode:
+        return _text_with_2000_lines_unicode
+    if STR is str:
+        return _text_with_2000_lines
+    raise AssertionError
+        
+
+ at bench('"...text...".split("\\n")', "split 2000 newlines")
+def newlines_split_2000(STR):
+    s = _get_2000_lines(STR)
+    s_split = s.split
+    for x in _RANGE_100:
+        s_split("\n")
+        
+ at bench('"...text...".rsplit("\\n")', "split 2000 newlines")
+def newlines_rsplit_2000(STR):
+    s = _get_2000_lines(STR)
+    s_rsplit = s.rsplit
+    for x in _RANGE_100:
+        s_rsplit("\n")
+        
+ at bench('"...text...".splitlines()', "split 2000 newlines")
+def newlines_splitlines_2000(STR):
+    s = _get_2000_lines(STR)
+    s_splitlines = s.splitlines
+    for x in _RANGE_100:
+        s_splitlines()
+
+
+## split text on "--" characters
+ at bench(
+    '"this--is--a--test--of--the--emergency--broadcast--system".split("--")',
+    "split on multicharacter seperator")
+def split_multichar_sep(STR):
+    s = STR("this--is--a--test--of--the--emergency--broadcast--system")
+    s_split = s.split
+    for x in _RANGE_1000:
+        s_split("--")
+ at bench(
+    '"this--is--a--test--of--the--emergency--broadcast--system".rsplit("--")',
+    "split on multicharacter seperator")
+def rsplit_multichar_sep(STR):
+    s = STR("this--is--a--test--of--the--emergency--broadcast--system")
+    s_rsplit = s.rsplit
+    for x in _RANGE_1000:
+        s_rsplit("--")
+
+## split with limits
+
+GFF3_example = "\t".join([
+    "I", "Genomic_canonical", "region", "357208", "396183", ".", "+", ".",
+    "ID=Sequence:R119;note=Clone R119%3B Genbank AF063007;Name=R119"])
+
+ at bench('GFF3_example.split("\\t")', "tab split")
+def tab_split_no_limit(STR):
+    s = STR(GFF3_example)
+    s_split = s.split
+    for x in _RANGE_1000:
+        s_split("\t")
+        
+ at bench('GFF3_example.split("\\t", 8)', "tab split")
+def tab_split_limit(STR):
+    s = STR(GFF3_example)
+    s_split = s.split
+    for x in _RANGE_1000:
+        s_split("\t", 8)
+        
+ at bench('GFF3_example.rsplit("\\t")', "tab split")
+def tab_rsplit_no_limit(STR):
+    s = STR(GFF3_example)
+    s_rsplit = s.rsplit
+    for x in _RANGE_1000:
+        s_rsplit("\t")
+        
+ at bench('GFF3_example.rsplit("\\t", 8)', "tab split")
+def tab_rsplit_limit(STR):
+    s = STR(GFF3_example)
+    s_rsplit = s.rsplit
+    for x in _RANGE_1000:
+        s_rsplit("\t", 8)
+
+#### Count characters
+
+ at bench('...text.with.2000.newlines.count("\\n")',
+       "count newlines")
+def count_newlines(STR):
+    s = _get_2000_lines(STR)
+    s_count = s.count
+    for x in _RANGE_100:
+        s_count("\n")
+
+# Orchid sequences concatenated, from Biopython
+_dna = """
+CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGGGTT
+AATCTGGAGGATCTGTTTACTTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGAATTGCCATCG
+AGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGCAGTTTTGCTCCAAGTCGTT
+TGACACATAATTGGTGAAGGGGGTGGCATCCTTCCCTGACCCTCCCCCAACTATTTTTTTAACAACTCTC
+AGCAACGGAGACTCAGTCTTCGGCAAATGCGATAAATGGTGTGAATTGCAGAATCCCGTGCACCATCGAG
+TCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCTGCCTGGGCATTGCGAGTCATAT
+CTCTCCCTTAACGAGGCTGTCCATACATACTGTTCAGCCGGTGCGGATGTGAGTTTGGCCCCTTGTTCTT
+TGGTACGGGGGGTCTAAGAGCTGCATGGGCTTTTGATGGTCCTAAATACGGCAAGAGGTGGACGAACTAT
+GCTACAACAAAATTGTTGTGCAGAGGCCCCGGGTTGTCGTATTAGATGGGCCACCGTAATCTGAAGACCC
+TTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGGCCATTTGGTTGCGACCCCAGGTCAG
+GTGAGCAACAGCTGTCGTAACAAGGTTTCCGTAGGGTGAACTGCGGAAGGATCATTGTTGAGATCACATA
+ATAATTGATCGAGTTAATCTGGAGGATCTGTTTACTTGGGTCACCCATGGGCATTTGCTGTTGAAGTGAC
+CTAGATTTGCCATCGAGCCTCCTTGGGAGCATCCTTGTTGGCGATATCTAAACCCTCAATTTTTCCCCCA
+ATCAAATTACACAAAATTGGTGGAGGGGGTGGCATTCTTCCCTTACCCTCCCCCAAATATTTTTTTAACA
+ACTCTCAGCAACGGATATCTCAGCTCTTGCATCGATGAAGAACCCACCGAAATGCGATAAATGGTGTGAA
+TTGCAGAATCCCGTGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACG
+CCTGCCTGGGCATTGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACATACTGTTCAGCCGGTGCG
+GATGTGAGTTTGGCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGATGCATGGGCTTTTGATGGTCCTAA
+ATACGGCAAGAGGTGGACGAACTATGCTACAACAAAATTGTTGTGCAAAGGCCCCGGGTTGTCGTATAAG
+ATGGGCCACCGATATCTGAAGACCCTTTTGGACCCCATTGGAGCCCATCAACCCATGTCAGTTGATGGCC
+ATTCGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGA
+GTTAATCTGGAGGATCTGTTTACTTGGGTCACCCATGGGCATTTGCTGTTGAAGTGACCTAGATTTGCCA
+TCGAGCCTCCTTGGGAGCTTTCTTGTTGGCGATATCTAAACCCTTGCCCGGCAGAGTTTTGGGAATCCCG
+TGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCTGCCTGGGCAT
+TGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACACACCTGTTCAGCCGGTGCGGATGTGAGTTTG
+GCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGCTGCATGGGCTTTTGATGGTCCTAAATACGGCAAGAG
+GTGGACGAACTATGCTACAACAAAATTGTTGTGCAAAGGCCCCGGGTTGTCGTATTAGATGGGCCACCAT
+AATCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGGCCATTTGGTTGC
+GACCCAGTCAGGTGAGGGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGAG
+TTAATCTGGAGGATCTGTTTACTTTGGTCACCCATGGGCATTTGCTGTTGAAGTGACCTAGATTTGCCAT
+CGAGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGGAGTTTGGCGCCAAGTCA
+TATGACACATAATTGGTGAAGGGGGTGGCATCCTGCCCTGACCCTCCCCAAATTATTTTTTTAACAACTC
+TCAGCAACGGATATCTCGGCTCTTGCATCGATGAAGAACGCAGCGAAATGCGATAAATGGTGTGAATTGC
+AGAATCCCGTGAACCATCGAGTCTTTGGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCT
+GCCTGGGCATTGGGAATCATATCTCTCCCCTAACGAGGCTATCCAAACATACTGTTCATCCGGTGCGGAT
+GTGAGTTTGGCCCCTTGTTCTTTGGTACCGGGGGTCTAAGAGCTGCATGGGCATTTGATGGTCCTCAAAA
+CGGCAAGAGGTGGACGAACTATGCCACAACAAAATTGTTGTCCCAAGGCCCCGGGTTGTCGTATTAGATG
+GGCCACCGTAACCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGACCA
+TTTGTTGCGACCCCAGTCAGCTGAGCAACCCGCTGAGTGGAAGGTCATTGCCGATATCACATAATAATTG
+ATCGAGTTAATCTGGAGGATCTGTTTACTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGATTT
+GCCATCGAGCCTCCTTGGGAGTTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGGAGTTGTGCGCCA
+AGTCATATGACACATAATTGGTGAAGGGGGTGGCATCCTGCCCTGACCCTCCCCAAATTATTTTTTTAAC
+AACTCTCAGCAACGGATATCTCGGCTCTTGCATCGATGAAGAACGCAGCGAAATGCGATAAATGGTGTGA
+ATTGCAGAATCCCGTGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCAC
+GCCTGCCTGGGCATTGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACATACTGTTCATCCGGTGC
+GGATGTGAGTTTGGCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGCTGCATGGGCATTTGATGGTCCTC
+AAAACGGCAAGAGGTGGACGAACTATGCTACAACCAAATTGTTGTCCCAAGGCCCCGGGTTGTCGTATTA
+GATGGGCCACCGTAACCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATG
+ACCATGTGTTGCGACCCCAGTCAGCTGAGCAACGCGCTGAGCGTAACAAGGTTTCCGTAGGTGGACCTCC
+GGGAGGATCATTGTTGAGATCACATAATAATTGATCGAGGTAATCTGGAGGATCTGCATATTTTGGTCAC
+"""
+_dna = "".join(_dna.splitlines())
+_dna = _dna * 50
+_dna_unicode = unicode(_dna)
+
+ at bench('dna.count("AACT")', "count AACT substrings in DNA example")
+def count_aact(STR):
+    if STR is unicode:
+        seq = _dna_unicode
+    elif STR is str:
+        seq = _dna
+    else:
+        raise AssertionError
+    seq_count = seq.count
+    for x in _RANGE_100:
+        seq_count("AACT")
+
+##### startswith and endswith
+
+ at bench('"Andrew".startswith("A")', 'startswith single character')
+def startswith_single(STR):
+    s1 = STR("Andrew")
+    s2 = STR("A")
+    s1_startswith = s1.startswith
+    for x in _RANGE_1000:
+        s1_startswith(s2)
+
+ at bench('"Andrew".startswith("Andrew")', 'startswith multiple characters')
+def startswith_multiple(STR):
+    s1 = STR("Andrew")
+    s2 = STR("Andrew")
+    s1_startswith = s1.startswith
+    for x in _RANGE_1000:
+        s1_startswith(s2)
+
+ at bench('"Andrew".startswith("Anders")',
+       'startswith multiple characters - not!')
+def startswith_multiple_not(STR):
+    s1 = STR("Andrew")
+    s2 = STR("Anders")
+    s1_startswith = s1.startswith
+    for x in _RANGE_1000:
+        s1_startswith(s2)
+
+
+# endswith
+
+ at bench('"Andrew".endswith("w")', 'endswith single character')
+def endswith_single(STR):
+    s1 = STR("Andrew")
+    s2 = STR("w")
+    s1_endswith = s1.endswith
+    for x in _RANGE_1000:
+        s1_endswith(s2)
+
+ at bench('"Andrew".endswith("Andrew")', 'endswith multiple characters')
+def endswith_multiple(STR):
+    s1 = STR("Andrew")
+    s2 = STR("Andrew")
+    s1_endswith = s1.endswith
+    for x in _RANGE_1000:
+        s1_endswith(s2)
+
+ at bench('"Andrew".endswith("Anders")',
+       'endswith multiple characters - not!')
+def endswith_multiple_not(STR):
+    s1 = STR("Andrew")
+    s2 = STR("Anders")
+    s1_endswith = s1.endswith
+    for x in _RANGE_1000:
+        s1_endswith(s2)
+
+#### Strip
+
+ at bench('"Hello!\\n".strip()', 'strip terminal newline')
+def terminal_newline_strip(STR):
+    s = STR("Hello!\n")
+    s_strip = s.strip
+    for x in _RANGE_1000:
+        s_strip()
+
+ at bench('"Hello!\\n".rstrip()', 'strip terminal newline')
+def terminal_newline_rstrip(STR):
+    s = STR("Hello!\n")
+    s_rstrip = s.rstrip
+    for x in _RANGE_1000:
+        s_rstrip()
+
+ at bench('s="Hello!\\n"; s[:-1] if s[-1]=="\\n" else s',
+       'strip terminal newline')
+def terminal_newline_if_else(STR):
+    s = STR("Hello!\n")
+    NL = STR("\n")
+    for x in _RANGE_1000:
+        s[:-1] if (s[-1] == NL) else s
+
+
+        
+# strip
+# lstrip
+# rstrip
+# replace
+
+# rfind
+# rindex
+
+
+# end of benchmarks
+
+#################
+
+class BenchTimer(timeit.Timer):
+    def best(self, repeat=1):
+        for i in range(1, 10):
+            number = 10**i
+            try:
+                x = self.timeit(number)
+            except:
+                self.print_exc()
+                raise
+            if x > 0.2:
+                break
+        times = [x]
+        for i in range(1, repeat):
+            times.append(self.timeit(number))
+        return min(times) / number
+
+def main():
+    test_names = sys.argv[1:]
+    
+    bench_functions = []
+    for (k,v) in globals().items():
+        if hasattr(v, "is_bench"):
+            if test_names:
+                for name in test_names:
+                    if name in v.group:
+                        break
+                else:
+                    # Not selected, ignore
+                    continue
+            bench_functions.append( (v.group, k, v) )
+    bench_functions.sort()
+
+    print "string\tunicode"
+    print "(in ms)\t(in ms)\t%\tcomment"
+
+    for title, group in itertools.groupby(bench_functions,
+                                      operator.itemgetter(0)):
+        print "="*10, title
+        for (_, k, v) in group:
+            if hasattr(v, "is_bench"):
+                str_time = BenchTimer("__main__.%s(str)" % (k,),
+                                      "import __main__").best(REPEAT)
+                uni_time = BenchTimer("__main__.%s(unicode)" % (k,),
+                                      "import __main__").best(REPEAT)
+                print "%.2f\t%.2f\t%.1f\t%s" % (
+                    1000*str_time, 1000*uni_time, 100.*str_time/uni_time,
+                    v.comment)
+
+if __name__ == "__main__":
+    main()
+    


More information about the Python-checkins mailing list