| 1 | n/a | |
|---|
| 2 | n/a | # Various microbenchmarks comparing unicode and byte string performance |
|---|
| 3 | n/a | # Please keep this file both 2.x and 3.x compatible! |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import timeit |
|---|
| 6 | n/a | import itertools |
|---|
| 7 | n/a | import operator |
|---|
| 8 | n/a | import re |
|---|
| 9 | n/a | import sys |
|---|
| 10 | n/a | import datetime |
|---|
| 11 | n/a | import optparse |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | VERSION = '2.0' |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | def p(*args): |
|---|
| 16 | n/a | sys.stdout.write(' '.join(str(s) for s in args) + '\n') |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | if sys.version_info >= (3,): |
|---|
| 19 | n/a | BYTES = bytes_from_str = lambda x: x.encode('ascii') |
|---|
| 20 | n/a | UNICODE = unicode_from_str = lambda x: x |
|---|
| 21 | n/a | else: |
|---|
| 22 | n/a | BYTES = bytes_from_str = lambda x: x |
|---|
| 23 | n/a | UNICODE = unicode_from_str = lambda x: x.decode('ascii') |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | class UnsupportedType(TypeError): |
|---|
| 26 | n/a | pass |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | p('stringbench v%s' % VERSION) |
|---|
| 30 | n/a | p(sys.version) |
|---|
| 31 | n/a | p(datetime.datetime.now()) |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | REPEAT = 1 |
|---|
| 34 | n/a | REPEAT = 3 |
|---|
| 35 | n/a | #REPEAT = 7 |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | if __name__ != "__main__": |
|---|
| 38 | n/a | raise SystemExit("Must run as main program") |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | parser = optparse.OptionParser() |
|---|
| 41 | n/a | parser.add_option("-R", "--skip-re", dest="skip_re", |
|---|
| 42 | n/a | action="store_true", |
|---|
| 43 | n/a | help="skip regular expression tests") |
|---|
| 44 | n/a | parser.add_option("-8", "--8-bit", dest="bytes_only", |
|---|
| 45 | n/a | action="store_true", |
|---|
| 46 | n/a | help="only do 8-bit string benchmarks") |
|---|
| 47 | n/a | parser.add_option("-u", "--unicode", dest="unicode_only", |
|---|
| 48 | n/a | action="store_true", |
|---|
| 49 | n/a | help="only do Unicode string benchmarks") |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | _RANGE_1000 = list(range(1000)) |
|---|
| 53 | n/a | _RANGE_100 = list(range(100)) |
|---|
| 54 | n/a | _RANGE_10 = list(range(10)) |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | dups = {} |
|---|
| 57 | n/a | def bench(s, group, repeat_count): |
|---|
| 58 | n/a | def blah(f): |
|---|
| 59 | n/a | if f.__name__ in dups: |
|---|
| 60 | n/a | raise AssertionError("Multiple functions with same name: %r" % |
|---|
| 61 | n/a | (f.__name__,)) |
|---|
| 62 | n/a | dups[f.__name__] = 1 |
|---|
| 63 | n/a | f.comment = s |
|---|
| 64 | n/a | f.is_bench = True |
|---|
| 65 | n/a | f.group = group |
|---|
| 66 | n/a | f.repeat_count = repeat_count |
|---|
| 67 | n/a | return f |
|---|
| 68 | n/a | return blah |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | def uses_re(f): |
|---|
| 71 | n/a | f.uses_re = True |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | ####### 'in' comparisons |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | @bench('"A" in "A"*1000', "early match, single character", 1000) |
|---|
| 76 | n/a | def in_test_quick_match_single_character(STR): |
|---|
| 77 | n/a | s1 = STR("A" * 1000) |
|---|
| 78 | n/a | s2 = STR("A") |
|---|
| 79 | n/a | for x in _RANGE_1000: |
|---|
| 80 | n/a | s2 in s1 |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | @bench('"B" in "A"*1000', "no match, single character", 1000) |
|---|
| 83 | n/a | def in_test_no_match_single_character(STR): |
|---|
| 84 | n/a | s1 = STR("A" * 1000) |
|---|
| 85 | n/a | s2 = STR("B") |
|---|
| 86 | n/a | for x in _RANGE_1000: |
|---|
| 87 | n/a | s2 in s1 |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | @bench('"AB" in "AB"*1000', "early match, two characters", 1000) |
|---|
| 91 | n/a | def in_test_quick_match_two_characters(STR): |
|---|
| 92 | n/a | s1 = STR("AB" * 1000) |
|---|
| 93 | n/a | s2 = STR("AB") |
|---|
| 94 | n/a | for x in _RANGE_1000: |
|---|
| 95 | n/a | s2 in s1 |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | @bench('"BC" in "AB"*1000', "no match, two characters", 1000) |
|---|
| 98 | n/a | def in_test_no_match_two_character(STR): |
|---|
| 99 | n/a | s1 = STR("AB" * 1000) |
|---|
| 100 | n/a | s2 = STR("BC") |
|---|
| 101 | n/a | for x in _RANGE_1000: |
|---|
| 102 | n/a | s2 in s1 |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | @bench('"BC" in ("AB"*300+"C")', "late match, two characters", 1000) |
|---|
| 105 | n/a | def in_test_slow_match_two_characters(STR): |
|---|
| 106 | n/a | s1 = STR("AB" * 300+"C") |
|---|
| 107 | n/a | s2 = STR("BC") |
|---|
| 108 | n/a | for x in _RANGE_1000: |
|---|
| 109 | n/a | s2 in s1 |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | @bench('s="ABC"*33; (s+"E") in ((s+"D")*300+s+"E")', |
|---|
| 112 | n/a | "late match, 100 characters", 100) |
|---|
| 113 | n/a | def in_test_slow_match_100_characters(STR): |
|---|
| 114 | n/a | m = STR("ABC"*33) |
|---|
| 115 | n/a | d = STR("D") |
|---|
| 116 | n/a | e = STR("E") |
|---|
| 117 | n/a | s1 = (m+d)*300 + m+e |
|---|
| 118 | n/a | s2 = m+e |
|---|
| 119 | n/a | for x in _RANGE_100: |
|---|
| 120 | n/a | s2 in s1 |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | # Try with regex |
|---|
| 123 | n/a | @uses_re |
|---|
| 124 | n/a | @bench('s="ABC"*33; re.compile(s+"D").search((s+"D")*300+s+"E")', |
|---|
| 125 | n/a | "late match, 100 characters", 100) |
|---|
| 126 | n/a | def re_test_slow_match_100_characters(STR): |
|---|
| 127 | n/a | m = STR("ABC"*33) |
|---|
| 128 | n/a | d = STR("D") |
|---|
| 129 | n/a | e = STR("E") |
|---|
| 130 | n/a | s1 = (m+d)*300 + m+e |
|---|
| 131 | n/a | s2 = m+e |
|---|
| 132 | n/a | pat = re.compile(s2) |
|---|
| 133 | n/a | search = pat.search |
|---|
| 134 | n/a | for x in _RANGE_100: |
|---|
| 135 | n/a | search(s1) |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | #### same tests as 'in' but use 'find' |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | @bench('("A"*1000).find("A")', "early match, single character", 1000) |
|---|
| 141 | n/a | def find_test_quick_match_single_character(STR): |
|---|
| 142 | n/a | s1 = STR("A" * 1000) |
|---|
| 143 | n/a | s2 = STR("A") |
|---|
| 144 | n/a | s1_find = s1.find |
|---|
| 145 | n/a | for x in _RANGE_1000: |
|---|
| 146 | n/a | s1_find(s2) |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | @bench('("A"*1000).find("B")', "no match, single character", 1000) |
|---|
| 149 | n/a | def find_test_no_match_single_character(STR): |
|---|
| 150 | n/a | s1 = STR("A" * 1000) |
|---|
| 151 | n/a | s2 = STR("B") |
|---|
| 152 | n/a | s1_find = s1.find |
|---|
| 153 | n/a | for x in _RANGE_1000: |
|---|
| 154 | n/a | s1_find(s2) |
|---|
| 155 | n/a | |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | @bench('("AB"*1000).find("AB")', "early match, two characters", 1000) |
|---|
| 158 | n/a | def find_test_quick_match_two_characters(STR): |
|---|
| 159 | n/a | s1 = STR("AB" * 1000) |
|---|
| 160 | n/a | s2 = STR("AB") |
|---|
| 161 | n/a | s1_find = s1.find |
|---|
| 162 | n/a | for x in _RANGE_1000: |
|---|
| 163 | n/a | s1_find(s2) |
|---|
| 164 | n/a | |
|---|
| 165 | n/a | @bench('("AB"*1000).find("BC")', "no match, two characters", 1000) |
|---|
| 166 | n/a | def find_test_no_match_two_character(STR): |
|---|
| 167 | n/a | s1 = STR("AB" * 1000) |
|---|
| 168 | n/a | s2 = STR("BC") |
|---|
| 169 | n/a | s1_find = s1.find |
|---|
| 170 | n/a | for x in _RANGE_1000: |
|---|
| 171 | n/a | s1_find(s2) |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | @bench('("AB"*1000).find("CA")', "no match, two characters", 1000) |
|---|
| 174 | n/a | def find_test_no_match_two_character_bis(STR): |
|---|
| 175 | n/a | s1 = STR("AB" * 1000) |
|---|
| 176 | n/a | s2 = STR("CA") |
|---|
| 177 | n/a | s1_find = s1.find |
|---|
| 178 | n/a | for x in _RANGE_1000: |
|---|
| 179 | n/a | s1_find(s2) |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | @bench('("AB"*300+"C").find("BC")', "late match, two characters", 1000) |
|---|
| 182 | n/a | def find_test_slow_match_two_characters(STR): |
|---|
| 183 | n/a | s1 = STR("AB" * 300+"C") |
|---|
| 184 | n/a | s2 = STR("BC") |
|---|
| 185 | n/a | s1_find = s1.find |
|---|
| 186 | n/a | for x in _RANGE_1000: |
|---|
| 187 | n/a | s1_find(s2) |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | @bench('("AB"*300+"CA").find("CA")', "late match, two characters", 1000) |
|---|
| 190 | n/a | def find_test_slow_match_two_characters_bis(STR): |
|---|
| 191 | n/a | s1 = STR("AB" * 300+"CA") |
|---|
| 192 | n/a | s2 = STR("CA") |
|---|
| 193 | n/a | s1_find = s1.find |
|---|
| 194 | n/a | for x in _RANGE_1000: |
|---|
| 195 | n/a | s1_find(s2) |
|---|
| 196 | n/a | |
|---|
| 197 | n/a | @bench('s="ABC"*33; ((s+"D")*500+s+"E").find(s+"E")', |
|---|
| 198 | n/a | "late match, 100 characters", 100) |
|---|
| 199 | n/a | def find_test_slow_match_100_characters(STR): |
|---|
| 200 | n/a | m = STR("ABC"*33) |
|---|
| 201 | n/a | d = STR("D") |
|---|
| 202 | n/a | e = STR("E") |
|---|
| 203 | n/a | s1 = (m+d)*500 + m+e |
|---|
| 204 | n/a | s2 = m+e |
|---|
| 205 | n/a | s1_find = s1.find |
|---|
| 206 | n/a | for x in _RANGE_100: |
|---|
| 207 | n/a | s1_find(s2) |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | @bench('s="ABC"*33; ((s+"D")*500+"E"+s).find("E"+s)', |
|---|
| 210 | n/a | "late match, 100 characters", 100) |
|---|
| 211 | n/a | def find_test_slow_match_100_characters_bis(STR): |
|---|
| 212 | n/a | m = STR("ABC"*33) |
|---|
| 213 | n/a | d = STR("D") |
|---|
| 214 | n/a | e = STR("E") |
|---|
| 215 | n/a | s1 = (m+d)*500 + e+m |
|---|
| 216 | n/a | s2 = e+m |
|---|
| 217 | n/a | s1_find = s1.find |
|---|
| 218 | n/a | for x in _RANGE_100: |
|---|
| 219 | n/a | s1_find(s2) |
|---|
| 220 | n/a | |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | #### Same tests for 'rfind' |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | @bench('("A"*1000).rfind("A")', "early match, single character", 1000) |
|---|
| 225 | n/a | def rfind_test_quick_match_single_character(STR): |
|---|
| 226 | n/a | s1 = STR("A" * 1000) |
|---|
| 227 | n/a | s2 = STR("A") |
|---|
| 228 | n/a | s1_rfind = s1.rfind |
|---|
| 229 | n/a | for x in _RANGE_1000: |
|---|
| 230 | n/a | s1_rfind(s2) |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | @bench('("A"*1000).rfind("B")', "no match, single character", 1000) |
|---|
| 233 | n/a | def rfind_test_no_match_single_character(STR): |
|---|
| 234 | n/a | s1 = STR("A" * 1000) |
|---|
| 235 | n/a | s2 = STR("B") |
|---|
| 236 | n/a | s1_rfind = s1.rfind |
|---|
| 237 | n/a | for x in _RANGE_1000: |
|---|
| 238 | n/a | s1_rfind(s2) |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | @bench('("AB"*1000).rfind("AB")', "early match, two characters", 1000) |
|---|
| 242 | n/a | def rfind_test_quick_match_two_characters(STR): |
|---|
| 243 | n/a | s1 = STR("AB" * 1000) |
|---|
| 244 | n/a | s2 = STR("AB") |
|---|
| 245 | n/a | s1_rfind = s1.rfind |
|---|
| 246 | n/a | for x in _RANGE_1000: |
|---|
| 247 | n/a | s1_rfind(s2) |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | @bench('("AB"*1000).rfind("BC")', "no match, two characters", 1000) |
|---|
| 250 | n/a | def rfind_test_no_match_two_character(STR): |
|---|
| 251 | n/a | s1 = STR("AB" * 1000) |
|---|
| 252 | n/a | s2 = STR("BC") |
|---|
| 253 | n/a | s1_rfind = s1.rfind |
|---|
| 254 | n/a | for x in _RANGE_1000: |
|---|
| 255 | n/a | s1_rfind(s2) |
|---|
| 256 | n/a | |
|---|
| 257 | n/a | @bench('("AB"*1000).rfind("CA")', "no match, two characters", 1000) |
|---|
| 258 | n/a | def rfind_test_no_match_two_character_bis(STR): |
|---|
| 259 | n/a | s1 = STR("AB" * 1000) |
|---|
| 260 | n/a | s2 = STR("CA") |
|---|
| 261 | n/a | s1_rfind = s1.rfind |
|---|
| 262 | n/a | for x in _RANGE_1000: |
|---|
| 263 | n/a | s1_rfind(s2) |
|---|
| 264 | n/a | |
|---|
| 265 | n/a | @bench('("C"+"AB"*300).rfind("CA")', "late match, two characters", 1000) |
|---|
| 266 | n/a | def rfind_test_slow_match_two_characters(STR): |
|---|
| 267 | n/a | s1 = STR("C" + "AB" * 300) |
|---|
| 268 | n/a | s2 = STR("CA") |
|---|
| 269 | n/a | s1_rfind = s1.rfind |
|---|
| 270 | n/a | for x in _RANGE_1000: |
|---|
| 271 | n/a | s1_rfind(s2) |
|---|
| 272 | n/a | |
|---|
| 273 | n/a | @bench('("BC"+"AB"*300).rfind("BC")', "late match, two characters", 1000) |
|---|
| 274 | n/a | def rfind_test_slow_match_two_characters_bis(STR): |
|---|
| 275 | n/a | s1 = STR("BC" + "AB" * 300) |
|---|
| 276 | n/a | s2 = STR("BC") |
|---|
| 277 | n/a | s1_rfind = s1.rfind |
|---|
| 278 | n/a | for x in _RANGE_1000: |
|---|
| 279 | n/a | s1_rfind(s2) |
|---|
| 280 | n/a | |
|---|
| 281 | n/a | @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rfind("E"+s)', |
|---|
| 282 | n/a | "late match, 100 characters", 100) |
|---|
| 283 | n/a | def rfind_test_slow_match_100_characters(STR): |
|---|
| 284 | n/a | m = STR("ABC"*33) |
|---|
| 285 | n/a | d = STR("D") |
|---|
| 286 | n/a | e = STR("E") |
|---|
| 287 | n/a | s1 = e+m + (d+m)*500 |
|---|
| 288 | n/a | s2 = e+m |
|---|
| 289 | n/a | s1_rfind = s1.rfind |
|---|
| 290 | n/a | for x in _RANGE_100: |
|---|
| 291 | n/a | s1_rfind(s2) |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | @bench('s="ABC"*33; (s+"E"+("D"+s)*500).rfind(s+"E")', |
|---|
| 294 | n/a | "late match, 100 characters", 100) |
|---|
| 295 | n/a | def rfind_test_slow_match_100_characters_bis(STR): |
|---|
| 296 | n/a | m = STR("ABC"*33) |
|---|
| 297 | n/a | d = STR("D") |
|---|
| 298 | n/a | e = STR("E") |
|---|
| 299 | n/a | s1 = m+e + (d+m)*500 |
|---|
| 300 | n/a | s2 = m+e |
|---|
| 301 | n/a | s1_rfind = s1.rfind |
|---|
| 302 | n/a | for x in _RANGE_100: |
|---|
| 303 | n/a | s1_rfind(s2) |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | |
|---|
| 306 | n/a | #### Now with index. |
|---|
| 307 | n/a | # Skip the ones which fail because that would include exception overhead. |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | @bench('("A"*1000).index("A")', "early match, single character", 1000) |
|---|
| 310 | n/a | def index_test_quick_match_single_character(STR): |
|---|
| 311 | n/a | s1 = STR("A" * 1000) |
|---|
| 312 | n/a | s2 = STR("A") |
|---|
| 313 | n/a | s1_index = s1.index |
|---|
| 314 | n/a | for x in _RANGE_1000: |
|---|
| 315 | n/a | s1_index(s2) |
|---|
| 316 | n/a | |
|---|
| 317 | n/a | @bench('("AB"*1000).index("AB")', "early match, two characters", 1000) |
|---|
| 318 | n/a | def index_test_quick_match_two_characters(STR): |
|---|
| 319 | n/a | s1 = STR("AB" * 1000) |
|---|
| 320 | n/a | s2 = STR("AB") |
|---|
| 321 | n/a | s1_index = s1.index |
|---|
| 322 | n/a | for x in _RANGE_1000: |
|---|
| 323 | n/a | s1_index(s2) |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | @bench('("AB"*300+"C").index("BC")', "late match, two characters", 1000) |
|---|
| 326 | n/a | def index_test_slow_match_two_characters(STR): |
|---|
| 327 | n/a | s1 = STR("AB" * 300+"C") |
|---|
| 328 | n/a | s2 = STR("BC") |
|---|
| 329 | n/a | s1_index = s1.index |
|---|
| 330 | n/a | for x in _RANGE_1000: |
|---|
| 331 | n/a | s1_index(s2) |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | @bench('s="ABC"*33; ((s+"D")*500+s+"E").index(s+"E")', |
|---|
| 334 | n/a | "late match, 100 characters", 100) |
|---|
| 335 | n/a | def index_test_slow_match_100_characters(STR): |
|---|
| 336 | n/a | m = STR("ABC"*33) |
|---|
| 337 | n/a | d = STR("D") |
|---|
| 338 | n/a | e = STR("E") |
|---|
| 339 | n/a | s1 = (m+d)*500 + m+e |
|---|
| 340 | n/a | s2 = m+e |
|---|
| 341 | n/a | s1_index = s1.index |
|---|
| 342 | n/a | for x in _RANGE_100: |
|---|
| 343 | n/a | s1_index(s2) |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | #### Same for rindex |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | @bench('("A"*1000).rindex("A")', "early match, single character", 1000) |
|---|
| 349 | n/a | def rindex_test_quick_match_single_character(STR): |
|---|
| 350 | n/a | s1 = STR("A" * 1000) |
|---|
| 351 | n/a | s2 = STR("A") |
|---|
| 352 | n/a | s1_rindex = s1.rindex |
|---|
| 353 | n/a | for x in _RANGE_1000: |
|---|
| 354 | n/a | s1_rindex(s2) |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | @bench('("AB"*1000).rindex("AB")', "early match, two characters", 1000) |
|---|
| 357 | n/a | def rindex_test_quick_match_two_characters(STR): |
|---|
| 358 | n/a | s1 = STR("AB" * 1000) |
|---|
| 359 | n/a | s2 = STR("AB") |
|---|
| 360 | n/a | s1_rindex = s1.rindex |
|---|
| 361 | n/a | for x in _RANGE_1000: |
|---|
| 362 | n/a | s1_rindex(s2) |
|---|
| 363 | n/a | |
|---|
| 364 | n/a | @bench('("C"+"AB"*300).rindex("CA")', "late match, two characters", 1000) |
|---|
| 365 | n/a | def rindex_test_slow_match_two_characters(STR): |
|---|
| 366 | n/a | s1 = STR("C" + "AB" * 300) |
|---|
| 367 | n/a | s2 = STR("CA") |
|---|
| 368 | n/a | s1_rindex = s1.rindex |
|---|
| 369 | n/a | for x in _RANGE_1000: |
|---|
| 370 | n/a | s1_rindex(s2) |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rindex("E"+s)', |
|---|
| 373 | n/a | "late match, 100 characters", 100) |
|---|
| 374 | n/a | def rindex_test_slow_match_100_characters(STR): |
|---|
| 375 | n/a | m = STR("ABC"*33) |
|---|
| 376 | n/a | d = STR("D") |
|---|
| 377 | n/a | e = STR("E") |
|---|
| 378 | n/a | s1 = e + m + (d+m)*500 |
|---|
| 379 | n/a | s2 = e + m |
|---|
| 380 | n/a | s1_rindex = s1.rindex |
|---|
| 381 | n/a | for x in _RANGE_100: |
|---|
| 382 | n/a | s1_rindex(s2) |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | #### Same for partition |
|---|
| 386 | n/a | |
|---|
| 387 | n/a | @bench('("A"*1000).partition("A")', "early match, single character", 1000) |
|---|
| 388 | n/a | def partition_test_quick_match_single_character(STR): |
|---|
| 389 | n/a | s1 = STR("A" * 1000) |
|---|
| 390 | n/a | s2 = STR("A") |
|---|
| 391 | n/a | s1_partition = s1.partition |
|---|
| 392 | n/a | for x in _RANGE_1000: |
|---|
| 393 | n/a | s1_partition(s2) |
|---|
| 394 | n/a | |
|---|
| 395 | n/a | @bench('("A"*1000).partition("B")', "no match, single character", 1000) |
|---|
| 396 | n/a | def partition_test_no_match_single_character(STR): |
|---|
| 397 | n/a | s1 = STR("A" * 1000) |
|---|
| 398 | n/a | s2 = STR("B") |
|---|
| 399 | n/a | s1_partition = s1.partition |
|---|
| 400 | n/a | for x in _RANGE_1000: |
|---|
| 401 | n/a | s1_partition(s2) |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | |
|---|
| 404 | n/a | @bench('("AB"*1000).partition("AB")', "early match, two characters", 1000) |
|---|
| 405 | n/a | def partition_test_quick_match_two_characters(STR): |
|---|
| 406 | n/a | s1 = STR("AB" * 1000) |
|---|
| 407 | n/a | s2 = STR("AB") |
|---|
| 408 | n/a | s1_partition = s1.partition |
|---|
| 409 | n/a | for x in _RANGE_1000: |
|---|
| 410 | n/a | s1_partition(s2) |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | @bench('("AB"*1000).partition("BC")', "no match, two characters", 1000) |
|---|
| 413 | n/a | def partition_test_no_match_two_character(STR): |
|---|
| 414 | n/a | s1 = STR("AB" * 1000) |
|---|
| 415 | n/a | s2 = STR("BC") |
|---|
| 416 | n/a | s1_partition = s1.partition |
|---|
| 417 | n/a | for x in _RANGE_1000: |
|---|
| 418 | n/a | s1_partition(s2) |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | @bench('("AB"*300+"C").partition("BC")', "late match, two characters", 1000) |
|---|
| 421 | n/a | def partition_test_slow_match_two_characters(STR): |
|---|
| 422 | n/a | s1 = STR("AB" * 300+"C") |
|---|
| 423 | n/a | s2 = STR("BC") |
|---|
| 424 | n/a | s1_partition = s1.partition |
|---|
| 425 | n/a | for x in _RANGE_1000: |
|---|
| 426 | n/a | s1_partition(s2) |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | @bench('s="ABC"*33; ((s+"D")*500+s+"E").partition(s+"E")', |
|---|
| 429 | n/a | "late match, 100 characters", 100) |
|---|
| 430 | n/a | def partition_test_slow_match_100_characters(STR): |
|---|
| 431 | n/a | m = STR("ABC"*33) |
|---|
| 432 | n/a | d = STR("D") |
|---|
| 433 | n/a | e = STR("E") |
|---|
| 434 | n/a | s1 = (m+d)*500 + m+e |
|---|
| 435 | n/a | s2 = m+e |
|---|
| 436 | n/a | s1_partition = s1.partition |
|---|
| 437 | n/a | for x in _RANGE_100: |
|---|
| 438 | n/a | s1_partition(s2) |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | |
|---|
| 441 | n/a | #### Same for rpartition |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | @bench('("A"*1000).rpartition("A")', "early match, single character", 1000) |
|---|
| 444 | n/a | def rpartition_test_quick_match_single_character(STR): |
|---|
| 445 | n/a | s1 = STR("A" * 1000) |
|---|
| 446 | n/a | s2 = STR("A") |
|---|
| 447 | n/a | s1_rpartition = s1.rpartition |
|---|
| 448 | n/a | for x in _RANGE_1000: |
|---|
| 449 | n/a | s1_rpartition(s2) |
|---|
| 450 | n/a | |
|---|
| 451 | n/a | @bench('("A"*1000).rpartition("B")', "no match, single character", 1000) |
|---|
| 452 | n/a | def rpartition_test_no_match_single_character(STR): |
|---|
| 453 | n/a | s1 = STR("A" * 1000) |
|---|
| 454 | n/a | s2 = STR("B") |
|---|
| 455 | n/a | s1_rpartition = s1.rpartition |
|---|
| 456 | n/a | for x in _RANGE_1000: |
|---|
| 457 | n/a | s1_rpartition(s2) |
|---|
| 458 | n/a | |
|---|
| 459 | n/a | |
|---|
| 460 | n/a | @bench('("AB"*1000).rpartition("AB")', "early match, two characters", 1000) |
|---|
| 461 | n/a | def rpartition_test_quick_match_two_characters(STR): |
|---|
| 462 | n/a | s1 = STR("AB" * 1000) |
|---|
| 463 | n/a | s2 = STR("AB") |
|---|
| 464 | n/a | s1_rpartition = s1.rpartition |
|---|
| 465 | n/a | for x in _RANGE_1000: |
|---|
| 466 | n/a | s1_rpartition(s2) |
|---|
| 467 | n/a | |
|---|
| 468 | n/a | @bench('("AB"*1000).rpartition("BC")', "no match, two characters", 1000) |
|---|
| 469 | n/a | def rpartition_test_no_match_two_character(STR): |
|---|
| 470 | n/a | s1 = STR("AB" * 1000) |
|---|
| 471 | n/a | s2 = STR("BC") |
|---|
| 472 | n/a | s1_rpartition = s1.rpartition |
|---|
| 473 | n/a | for x in _RANGE_1000: |
|---|
| 474 | n/a | s1_rpartition(s2) |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | @bench('("C"+"AB"*300).rpartition("CA")', "late match, two characters", 1000) |
|---|
| 477 | n/a | def rpartition_test_slow_match_two_characters(STR): |
|---|
| 478 | n/a | s1 = STR("C" + "AB" * 300) |
|---|
| 479 | n/a | s2 = STR("CA") |
|---|
| 480 | n/a | s1_rpartition = s1.rpartition |
|---|
| 481 | n/a | for x in _RANGE_1000: |
|---|
| 482 | n/a | s1_rpartition(s2) |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rpartition("E"+s)', |
|---|
| 485 | n/a | "late match, 100 characters", 100) |
|---|
| 486 | n/a | def rpartition_test_slow_match_100_characters(STR): |
|---|
| 487 | n/a | m = STR("ABC"*33) |
|---|
| 488 | n/a | d = STR("D") |
|---|
| 489 | n/a | e = STR("E") |
|---|
| 490 | n/a | s1 = e + m + (d+m)*500 |
|---|
| 491 | n/a | s2 = e + m |
|---|
| 492 | n/a | s1_rpartition = s1.rpartition |
|---|
| 493 | n/a | for x in _RANGE_100: |
|---|
| 494 | n/a | s1_rpartition(s2) |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | |
|---|
| 497 | n/a | #### Same for split(s, 1) |
|---|
| 498 | n/a | |
|---|
| 499 | n/a | @bench('("A"*1000).split("A", 1)', "early match, single character", 1000) |
|---|
| 500 | n/a | def split_test_quick_match_single_character(STR): |
|---|
| 501 | n/a | s1 = STR("A" * 1000) |
|---|
| 502 | n/a | s2 = STR("A") |
|---|
| 503 | n/a | s1_split = s1.split |
|---|
| 504 | n/a | for x in _RANGE_1000: |
|---|
| 505 | n/a | s1_split(s2, 1) |
|---|
| 506 | n/a | |
|---|
| 507 | n/a | @bench('("A"*1000).split("B", 1)', "no match, single character", 1000) |
|---|
| 508 | n/a | def split_test_no_match_single_character(STR): |
|---|
| 509 | n/a | s1 = STR("A" * 1000) |
|---|
| 510 | n/a | s2 = STR("B") |
|---|
| 511 | n/a | s1_split = s1.split |
|---|
| 512 | n/a | for x in _RANGE_1000: |
|---|
| 513 | n/a | s1_split(s2, 1) |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | @bench('("AB"*1000).split("AB", 1)', "early match, two characters", 1000) |
|---|
| 517 | n/a | def split_test_quick_match_two_characters(STR): |
|---|
| 518 | n/a | s1 = STR("AB" * 1000) |
|---|
| 519 | n/a | s2 = STR("AB") |
|---|
| 520 | n/a | s1_split = s1.split |
|---|
| 521 | n/a | for x in _RANGE_1000: |
|---|
| 522 | n/a | s1_split(s2, 1) |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | @bench('("AB"*1000).split("BC", 1)', "no match, two characters", 1000) |
|---|
| 525 | n/a | def split_test_no_match_two_character(STR): |
|---|
| 526 | n/a | s1 = STR("AB" * 1000) |
|---|
| 527 | n/a | s2 = STR("BC") |
|---|
| 528 | n/a | s1_split = s1.split |
|---|
| 529 | n/a | for x in _RANGE_1000: |
|---|
| 530 | n/a | s1_split(s2, 1) |
|---|
| 531 | n/a | |
|---|
| 532 | n/a | @bench('("AB"*300+"C").split("BC", 1)', "late match, two characters", 1000) |
|---|
| 533 | n/a | def split_test_slow_match_two_characters(STR): |
|---|
| 534 | n/a | s1 = STR("AB" * 300+"C") |
|---|
| 535 | n/a | s2 = STR("BC") |
|---|
| 536 | n/a | s1_split = s1.split |
|---|
| 537 | n/a | for x in _RANGE_1000: |
|---|
| 538 | n/a | s1_split(s2, 1) |
|---|
| 539 | n/a | |
|---|
| 540 | n/a | @bench('s="ABC"*33; ((s+"D")*500+s+"E").split(s+"E", 1)', |
|---|
| 541 | n/a | "late match, 100 characters", 100) |
|---|
| 542 | n/a | def split_test_slow_match_100_characters(STR): |
|---|
| 543 | n/a | m = STR("ABC"*33) |
|---|
| 544 | n/a | d = STR("D") |
|---|
| 545 | n/a | e = STR("E") |
|---|
| 546 | n/a | s1 = (m+d)*500 + m+e |
|---|
| 547 | n/a | s2 = m+e |
|---|
| 548 | n/a | s1_split = s1.split |
|---|
| 549 | n/a | for x in _RANGE_100: |
|---|
| 550 | n/a | s1_split(s2, 1) |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | |
|---|
| 553 | n/a | #### Same for rsplit(s, 1) |
|---|
| 554 | n/a | |
|---|
| 555 | n/a | @bench('("A"*1000).rsplit("A", 1)', "early match, single character", 1000) |
|---|
| 556 | n/a | def rsplit_test_quick_match_single_character(STR): |
|---|
| 557 | n/a | s1 = STR("A" * 1000) |
|---|
| 558 | n/a | s2 = STR("A") |
|---|
| 559 | n/a | s1_rsplit = s1.rsplit |
|---|
| 560 | n/a | for x in _RANGE_1000: |
|---|
| 561 | n/a | s1_rsplit(s2, 1) |
|---|
| 562 | n/a | |
|---|
| 563 | n/a | @bench('("A"*1000).rsplit("B", 1)', "no match, single character", 1000) |
|---|
| 564 | n/a | def rsplit_test_no_match_single_character(STR): |
|---|
| 565 | n/a | s1 = STR("A" * 1000) |
|---|
| 566 | n/a | s2 = STR("B") |
|---|
| 567 | n/a | s1_rsplit = s1.rsplit |
|---|
| 568 | n/a | for x in _RANGE_1000: |
|---|
| 569 | n/a | s1_rsplit(s2, 1) |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | |
|---|
| 572 | n/a | @bench('("AB"*1000).rsplit("AB", 1)', "early match, two characters", 1000) |
|---|
| 573 | n/a | def rsplit_test_quick_match_two_characters(STR): |
|---|
| 574 | n/a | s1 = STR("AB" * 1000) |
|---|
| 575 | n/a | s2 = STR("AB") |
|---|
| 576 | n/a | s1_rsplit = s1.rsplit |
|---|
| 577 | n/a | for x in _RANGE_1000: |
|---|
| 578 | n/a | s1_rsplit(s2, 1) |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | @bench('("AB"*1000).rsplit("BC", 1)', "no match, two characters", 1000) |
|---|
| 581 | n/a | def rsplit_test_no_match_two_character(STR): |
|---|
| 582 | n/a | s1 = STR("AB" * 1000) |
|---|
| 583 | n/a | s2 = STR("BC") |
|---|
| 584 | n/a | s1_rsplit = s1.rsplit |
|---|
| 585 | n/a | for x in _RANGE_1000: |
|---|
| 586 | n/a | s1_rsplit(s2, 1) |
|---|
| 587 | n/a | |
|---|
| 588 | n/a | @bench('("C"+"AB"*300).rsplit("CA", 1)', "late match, two characters", 1000) |
|---|
| 589 | n/a | def rsplit_test_slow_match_two_characters(STR): |
|---|
| 590 | n/a | s1 = STR("C" + "AB" * 300) |
|---|
| 591 | n/a | s2 = STR("CA") |
|---|
| 592 | n/a | s1_rsplit = s1.rsplit |
|---|
| 593 | n/a | for x in _RANGE_1000: |
|---|
| 594 | n/a | s1_rsplit(s2, 1) |
|---|
| 595 | n/a | |
|---|
| 596 | n/a | @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rsplit("E"+s, 1)', |
|---|
| 597 | n/a | "late match, 100 characters", 100) |
|---|
| 598 | n/a | def rsplit_test_slow_match_100_characters(STR): |
|---|
| 599 | n/a | m = STR("ABC"*33) |
|---|
| 600 | n/a | d = STR("D") |
|---|
| 601 | n/a | e = STR("E") |
|---|
| 602 | n/a | s1 = e + m + (d+m)*500 |
|---|
| 603 | n/a | s2 = e + m |
|---|
| 604 | n/a | s1_rsplit = s1.rsplit |
|---|
| 605 | n/a | for x in _RANGE_100: |
|---|
| 606 | n/a | s1_rsplit(s2, 1) |
|---|
| 607 | n/a | |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | #### Benchmark the operator-based methods |
|---|
| 610 | n/a | |
|---|
| 611 | n/a | @bench('"A"*10', "repeat 1 character 10 times", 1000) |
|---|
| 612 | n/a | def repeat_single_10_times(STR): |
|---|
| 613 | n/a | s = STR("A") |
|---|
| 614 | n/a | for x in _RANGE_1000: |
|---|
| 615 | n/a | s * 10 |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | @bench('"A"*1000', "repeat 1 character 1000 times", 1000) |
|---|
| 618 | n/a | def repeat_single_1000_times(STR): |
|---|
| 619 | n/a | s = STR("A") |
|---|
| 620 | n/a | for x in _RANGE_1000: |
|---|
| 621 | n/a | s * 1000 |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | @bench('"ABCDE"*10', "repeat 5 characters 10 times", 1000) |
|---|
| 624 | n/a | def repeat_5_10_times(STR): |
|---|
| 625 | n/a | s = STR("ABCDE") |
|---|
| 626 | n/a | for x in _RANGE_1000: |
|---|
| 627 | n/a | s * 10 |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | @bench('"ABCDE"*1000', "repeat 5 characters 1000 times", 1000) |
|---|
| 630 | n/a | def repeat_5_1000_times(STR): |
|---|
| 631 | n/a | s = STR("ABCDE") |
|---|
| 632 | n/a | for x in _RANGE_1000: |
|---|
| 633 | n/a | s * 1000 |
|---|
| 634 | n/a | |
|---|
| 635 | n/a | # + for concat |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | @bench('"Andrew"+"Dalke"', "concat two strings", 1000) |
|---|
| 638 | n/a | def concat_two_strings(STR): |
|---|
| 639 | n/a | s1 = STR("Andrew") |
|---|
| 640 | n/a | s2 = STR("Dalke") |
|---|
| 641 | n/a | for x in _RANGE_1000: |
|---|
| 642 | n/a | s1+s2 |
|---|
| 643 | n/a | |
|---|
| 644 | n/a | @bench('s1+s2+s3+s4+...+s20', "concat 20 strings of words length 4 to 15", |
|---|
| 645 | n/a | 1000) |
|---|
| 646 | n/a | def concat_many_strings(STR): |
|---|
| 647 | n/a | s1=STR('TIXSGYNREDCVBHJ') |
|---|
| 648 | n/a | s2=STR('PUMTLXBZVDO') |
|---|
| 649 | n/a | s3=STR('FVZNJ') |
|---|
| 650 | n/a | s4=STR('OGDXUW') |
|---|
| 651 | n/a | s5=STR('WEIMRNCOYVGHKB') |
|---|
| 652 | n/a | s6=STR('FCQTNMXPUZH') |
|---|
| 653 | n/a | s7=STR('TICZJYRLBNVUEAK') |
|---|
| 654 | n/a | s8=STR('REYB') |
|---|
| 655 | n/a | s9=STR('PWUOQ') |
|---|
| 656 | n/a | s10=STR('EQHCMKBS') |
|---|
| 657 | n/a | s11=STR('AEVDFOH') |
|---|
| 658 | n/a | s12=STR('IFHVD') |
|---|
| 659 | n/a | s13=STR('JGTCNLXWOHQ') |
|---|
| 660 | n/a | s14=STR('ITSKEPYLROZAWXF') |
|---|
| 661 | n/a | s15=STR('THEK') |
|---|
| 662 | n/a | s16=STR('GHPZFBUYCKMNJIT') |
|---|
| 663 | n/a | s17=STR('JMUZ') |
|---|
| 664 | n/a | s18=STR('WLZQMTB') |
|---|
| 665 | n/a | s19=STR('KPADCBW') |
|---|
| 666 | n/a | s20=STR('TNJHZQAGBU') |
|---|
| 667 | n/a | for x in _RANGE_1000: |
|---|
| 668 | n/a | (s1 + s2+ s3+ s4+ s5+ s6+ s7+ s8+ s9+s10+ |
|---|
| 669 | n/a | s11+s12+s13+s14+s15+s16+s17+s18+s19+s20) |
|---|
| 670 | n/a | |
|---|
| 671 | n/a | |
|---|
| 672 | n/a | #### Benchmark join |
|---|
| 673 | n/a | |
|---|
| 674 | n/a | def get_bytes_yielding_seq(STR, arg): |
|---|
| 675 | n/a | if STR is BYTES and sys.version_info >= (3,): |
|---|
| 676 | n/a | raise UnsupportedType |
|---|
| 677 | n/a | return STR(arg) |
|---|
| 678 | n/a | |
|---|
| 679 | n/a | @bench('"A".join("")', |
|---|
| 680 | n/a | "join empty string, with 1 character sep", 100) |
|---|
| 681 | n/a | def join_empty_single(STR): |
|---|
| 682 | n/a | sep = STR("A") |
|---|
| 683 | n/a | s2 = get_bytes_yielding_seq(STR, "") |
|---|
| 684 | n/a | sep_join = sep.join |
|---|
| 685 | n/a | for x in _RANGE_100: |
|---|
| 686 | n/a | sep_join(s2) |
|---|
| 687 | n/a | |
|---|
| 688 | n/a | @bench('"ABCDE".join("")', |
|---|
| 689 | n/a | "join empty string, with 5 character sep", 100) |
|---|
| 690 | n/a | def join_empty_5(STR): |
|---|
| 691 | n/a | sep = STR("ABCDE") |
|---|
| 692 | n/a | s2 = get_bytes_yielding_seq(STR, "") |
|---|
| 693 | n/a | sep_join = sep.join |
|---|
| 694 | n/a | for x in _RANGE_100: |
|---|
| 695 | n/a | sep_join(s2) |
|---|
| 696 | n/a | |
|---|
| 697 | n/a | @bench('"A".join("ABC..Z")', |
|---|
| 698 | n/a | "join string with 26 characters, with 1 character sep", 1000) |
|---|
| 699 | n/a | def join_alphabet_single(STR): |
|---|
| 700 | n/a | sep = STR("A") |
|---|
| 701 | n/a | s2 = get_bytes_yielding_seq(STR, "ABCDEFGHIJKLMnOPQRSTUVWXYZ") |
|---|
| 702 | n/a | sep_join = sep.join |
|---|
| 703 | n/a | for x in _RANGE_1000: |
|---|
| 704 | n/a | sep_join(s2) |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | @bench('"ABCDE".join("ABC..Z")', |
|---|
| 707 | n/a | "join string with 26 characters, with 5 character sep", 1000) |
|---|
| 708 | n/a | def join_alphabet_5(STR): |
|---|
| 709 | n/a | sep = STR("ABCDE") |
|---|
| 710 | n/a | s2 = get_bytes_yielding_seq(STR, "ABCDEFGHIJKLMnOPQRSTUVWXYZ") |
|---|
| 711 | n/a | sep_join = sep.join |
|---|
| 712 | n/a | for x in _RANGE_1000: |
|---|
| 713 | n/a | sep_join(s2) |
|---|
| 714 | n/a | |
|---|
| 715 | n/a | @bench('"A".join(list("ABC..Z"))', |
|---|
| 716 | n/a | "join list of 26 characters, with 1 character sep", 1000) |
|---|
| 717 | n/a | def join_alphabet_list_single(STR): |
|---|
| 718 | n/a | sep = STR("A") |
|---|
| 719 | n/a | s2 = [STR(x) for x in "ABCDEFGHIJKLMnOPQRSTUVWXYZ"] |
|---|
| 720 | n/a | sep_join = sep.join |
|---|
| 721 | n/a | for x in _RANGE_1000: |
|---|
| 722 | n/a | sep_join(s2) |
|---|
| 723 | n/a | |
|---|
| 724 | n/a | @bench('"ABCDE".join(list("ABC..Z"))', |
|---|
| 725 | n/a | "join list of 26 characters, with 5 character sep", 1000) |
|---|
| 726 | n/a | def join_alphabet_list_five(STR): |
|---|
| 727 | n/a | sep = STR("ABCDE") |
|---|
| 728 | n/a | s2 = [STR(x) for x in "ABCDEFGHIJKLMnOPQRSTUVWXYZ"] |
|---|
| 729 | n/a | sep_join = sep.join |
|---|
| 730 | n/a | for x in _RANGE_1000: |
|---|
| 731 | n/a | sep_join(s2) |
|---|
| 732 | n/a | |
|---|
| 733 | n/a | @bench('"A".join(["Bob"]*100))', |
|---|
| 734 | n/a | "join list of 100 words, with 1 character sep", 1000) |
|---|
| 735 | n/a | def join_100_words_single(STR): |
|---|
| 736 | n/a | sep = STR("A") |
|---|
| 737 | n/a | s2 = [STR("Bob")]*100 |
|---|
| 738 | n/a | sep_join = sep.join |
|---|
| 739 | n/a | for x in _RANGE_1000: |
|---|
| 740 | n/a | sep_join(s2) |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | @bench('"ABCDE".join(["Bob"]*100))', |
|---|
| 743 | n/a | "join list of 100 words, with 5 character sep", 1000) |
|---|
| 744 | n/a | def join_100_words_5(STR): |
|---|
| 745 | n/a | sep = STR("ABCDE") |
|---|
| 746 | n/a | s2 = [STR("Bob")]*100 |
|---|
| 747 | n/a | sep_join = sep.join |
|---|
| 748 | n/a | for x in _RANGE_1000: |
|---|
| 749 | n/a | sep_join(s2) |
|---|
| 750 | n/a | |
|---|
| 751 | n/a | #### split tests |
|---|
| 752 | n/a | |
|---|
| 753 | n/a | @bench('("Here are some words. "*2).split()', "split whitespace (small)", 1000) |
|---|
| 754 | n/a | def whitespace_split(STR): |
|---|
| 755 | n/a | s = STR("Here are some words. "*2) |
|---|
| 756 | n/a | s_split = s.split |
|---|
| 757 | n/a | for x in _RANGE_1000: |
|---|
| 758 | n/a | s_split() |
|---|
| 759 | n/a | |
|---|
| 760 | n/a | @bench('("Here are some words. "*2).rsplit()', "split whitespace (small)", 1000) |
|---|
| 761 | n/a | def whitespace_rsplit(STR): |
|---|
| 762 | n/a | s = STR("Here are some words. "*2) |
|---|
| 763 | n/a | s_rsplit = s.rsplit |
|---|
| 764 | n/a | for x in _RANGE_1000: |
|---|
| 765 | n/a | s_rsplit() |
|---|
| 766 | n/a | |
|---|
| 767 | n/a | @bench('("Here are some words. "*2).split(None, 1)', |
|---|
| 768 | n/a | "split 1 whitespace", 1000) |
|---|
| 769 | n/a | def whitespace_split_1(STR): |
|---|
| 770 | n/a | s = STR("Here are some words. "*2) |
|---|
| 771 | n/a | s_split = s.split |
|---|
| 772 | n/a | N = None |
|---|
| 773 | n/a | for x in _RANGE_1000: |
|---|
| 774 | n/a | s_split(N, 1) |
|---|
| 775 | n/a | |
|---|
| 776 | n/a | @bench('("Here are some words. "*2).rsplit(None, 1)', |
|---|
| 777 | n/a | "split 1 whitespace", 1000) |
|---|
| 778 | n/a | def whitespace_rsplit_1(STR): |
|---|
| 779 | n/a | s = STR("Here are some words. "*2) |
|---|
| 780 | n/a | s_rsplit = s.rsplit |
|---|
| 781 | n/a | N = None |
|---|
| 782 | n/a | for x in _RANGE_1000: |
|---|
| 783 | n/a | s_rsplit(N, 1) |
|---|
| 784 | n/a | |
|---|
| 785 | n/a | @bench('("Here are some words. "*2).partition(" ")', |
|---|
| 786 | n/a | "split 1 whitespace", 1000) |
|---|
| 787 | n/a | def whitespace_partition(STR): |
|---|
| 788 | n/a | sep = STR(" ") |
|---|
| 789 | n/a | s = STR("Here are some words. "*2) |
|---|
| 790 | n/a | s_partition = s.partition |
|---|
| 791 | n/a | for x in _RANGE_1000: |
|---|
| 792 | n/a | s_partition(sep) |
|---|
| 793 | n/a | |
|---|
| 794 | n/a | @bench('("Here are some words. "*2).rpartition(" ")', |
|---|
| 795 | n/a | "split 1 whitespace", 1000) |
|---|
| 796 | n/a | def whitespace_rpartition(STR): |
|---|
| 797 | n/a | sep = STR(" ") |
|---|
| 798 | n/a | s = STR("Here are some words. "*2) |
|---|
| 799 | n/a | s_rpartition = s.rpartition |
|---|
| 800 | n/a | for x in _RANGE_1000: |
|---|
| 801 | n/a | s_rpartition(sep) |
|---|
| 802 | n/a | |
|---|
| 803 | n/a | human_text = """\ |
|---|
| 804 | n/a | Python is a dynamic object-oriented programming language that can be |
|---|
| 805 | n/a | used for many kinds of software development. It offers strong support |
|---|
| 806 | n/a | for integration with other languages and tools, comes with extensive |
|---|
| 807 | n/a | standard libraries, and can be learned in a few days. Many Python |
|---|
| 808 | n/a | programmers report substantial productivity gains and feel the language |
|---|
| 809 | n/a | encourages the development of higher quality, more maintainable code. |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | Python runs on Windows, Linux/Unix, Mac OS X, Amiga, Palm |
|---|
| 812 | n/a | Handhelds, and Nokia mobile phones. Python has also been ported to the |
|---|
| 813 | n/a | Java and .NET virtual machines. |
|---|
| 814 | n/a | |
|---|
| 815 | n/a | Python is distributed under an OSI-approved open source license that |
|---|
| 816 | n/a | makes it free to use, even for commercial products. |
|---|
| 817 | n/a | """*25 |
|---|
| 818 | n/a | human_text_bytes = bytes_from_str(human_text) |
|---|
| 819 | n/a | human_text_unicode = unicode_from_str(human_text) |
|---|
| 820 | n/a | def _get_human_text(STR): |
|---|
| 821 | n/a | if STR is UNICODE: |
|---|
| 822 | n/a | return human_text_unicode |
|---|
| 823 | n/a | if STR is BYTES: |
|---|
| 824 | n/a | return human_text_bytes |
|---|
| 825 | n/a | raise AssertionError |
|---|
| 826 | n/a | |
|---|
| 827 | n/a | @bench('human_text.split()', "split whitespace (huge)", 10) |
|---|
| 828 | n/a | def whitespace_split_huge(STR): |
|---|
| 829 | n/a | s = _get_human_text(STR) |
|---|
| 830 | n/a | s_split = s.split |
|---|
| 831 | n/a | for x in _RANGE_10: |
|---|
| 832 | n/a | s_split() |
|---|
| 833 | n/a | |
|---|
| 834 | n/a | @bench('human_text.rsplit()', "split whitespace (huge)", 10) |
|---|
| 835 | n/a | def whitespace_rsplit_huge(STR): |
|---|
| 836 | n/a | s = _get_human_text(STR) |
|---|
| 837 | n/a | s_rsplit = s.rsplit |
|---|
| 838 | n/a | for x in _RANGE_10: |
|---|
| 839 | n/a | s_rsplit() |
|---|
| 840 | n/a | |
|---|
| 841 | n/a | |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | @bench('"this\\nis\\na\\ntest\\n".split("\\n")', "split newlines", 1000) |
|---|
| 844 | n/a | def newlines_split(STR): |
|---|
| 845 | n/a | s = STR("this\nis\na\ntest\n") |
|---|
| 846 | n/a | s_split = s.split |
|---|
| 847 | n/a | nl = STR("\n") |
|---|
| 848 | n/a | for x in _RANGE_1000: |
|---|
| 849 | n/a | s_split(nl) |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | @bench('"this\\nis\\na\\ntest\\n".rsplit("\\n")', "split newlines", 1000) |
|---|
| 853 | n/a | def newlines_rsplit(STR): |
|---|
| 854 | n/a | s = STR("this\nis\na\ntest\n") |
|---|
| 855 | n/a | s_rsplit = s.rsplit |
|---|
| 856 | n/a | nl = STR("\n") |
|---|
| 857 | n/a | for x in _RANGE_1000: |
|---|
| 858 | n/a | s_rsplit(nl) |
|---|
| 859 | n/a | |
|---|
| 860 | n/a | @bench('"this\\nis\\na\\ntest\\n".splitlines()', "split newlines", 1000) |
|---|
| 861 | n/a | def newlines_splitlines(STR): |
|---|
| 862 | n/a | s = STR("this\nis\na\ntest\n") |
|---|
| 863 | n/a | s_splitlines = s.splitlines |
|---|
| 864 | n/a | for x in _RANGE_1000: |
|---|
| 865 | n/a | s_splitlines() |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | ## split text with 2000 newlines |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | def _make_2000_lines(): |
|---|
| 870 | n/a | import random |
|---|
| 871 | n/a | r = random.Random(100) |
|---|
| 872 | n/a | chars = list(map(chr, range(32, 128))) |
|---|
| 873 | n/a | i = 0 |
|---|
| 874 | n/a | while i < len(chars): |
|---|
| 875 | n/a | chars[i] = " " |
|---|
| 876 | n/a | i += r.randrange(9) |
|---|
| 877 | n/a | s = "".join(chars) |
|---|
| 878 | n/a | s = s*4 |
|---|
| 879 | n/a | words = [] |
|---|
| 880 | n/a | for i in range(2000): |
|---|
| 881 | n/a | start = r.randrange(96) |
|---|
| 882 | n/a | n = r.randint(5, 65) |
|---|
| 883 | n/a | words.append(s[start:start+n]) |
|---|
| 884 | n/a | return "\n".join(words)+"\n" |
|---|
| 885 | n/a | |
|---|
| 886 | n/a | _text_with_2000_lines = _make_2000_lines() |
|---|
| 887 | n/a | _text_with_2000_lines_bytes = bytes_from_str(_text_with_2000_lines) |
|---|
| 888 | n/a | _text_with_2000_lines_unicode = unicode_from_str(_text_with_2000_lines) |
|---|
| 889 | n/a | def _get_2000_lines(STR): |
|---|
| 890 | n/a | if STR is UNICODE: |
|---|
| 891 | n/a | return _text_with_2000_lines_unicode |
|---|
| 892 | n/a | if STR is BYTES: |
|---|
| 893 | n/a | return _text_with_2000_lines_bytes |
|---|
| 894 | n/a | raise AssertionError |
|---|
| 895 | n/a | |
|---|
| 896 | n/a | |
|---|
| 897 | n/a | @bench('"...text...".split("\\n")', "split 2000 newlines", 10) |
|---|
| 898 | n/a | def newlines_split_2000(STR): |
|---|
| 899 | n/a | s = _get_2000_lines(STR) |
|---|
| 900 | n/a | s_split = s.split |
|---|
| 901 | n/a | nl = STR("\n") |
|---|
| 902 | n/a | for x in _RANGE_10: |
|---|
| 903 | n/a | s_split(nl) |
|---|
| 904 | n/a | |
|---|
| 905 | n/a | @bench('"...text...".rsplit("\\n")', "split 2000 newlines", 10) |
|---|
| 906 | n/a | def newlines_rsplit_2000(STR): |
|---|
| 907 | n/a | s = _get_2000_lines(STR) |
|---|
| 908 | n/a | s_rsplit = s.rsplit |
|---|
| 909 | n/a | nl = STR("\n") |
|---|
| 910 | n/a | for x in _RANGE_10: |
|---|
| 911 | n/a | s_rsplit(nl) |
|---|
| 912 | n/a | |
|---|
| 913 | n/a | @bench('"...text...".splitlines()', "split 2000 newlines", 10) |
|---|
| 914 | n/a | def newlines_splitlines_2000(STR): |
|---|
| 915 | n/a | s = _get_2000_lines(STR) |
|---|
| 916 | n/a | s_splitlines = s.splitlines |
|---|
| 917 | n/a | for x in _RANGE_10: |
|---|
| 918 | n/a | s_splitlines() |
|---|
| 919 | n/a | |
|---|
| 920 | n/a | |
|---|
| 921 | n/a | ## split text on "--" characters |
|---|
| 922 | n/a | @bench( |
|---|
| 923 | n/a | '"this--is--a--test--of--the--emergency--broadcast--system".split("--")', |
|---|
| 924 | n/a | "split on multicharacter separator (small)", 1000) |
|---|
| 925 | n/a | def split_multichar_sep_small(STR): |
|---|
| 926 | n/a | s = STR("this--is--a--test--of--the--emergency--broadcast--system") |
|---|
| 927 | n/a | s_split = s.split |
|---|
| 928 | n/a | pat = STR("--") |
|---|
| 929 | n/a | for x in _RANGE_1000: |
|---|
| 930 | n/a | s_split(pat) |
|---|
| 931 | n/a | @bench( |
|---|
| 932 | n/a | '"this--is--a--test--of--the--emergency--broadcast--system".rsplit("--")', |
|---|
| 933 | n/a | "split on multicharacter separator (small)", 1000) |
|---|
| 934 | n/a | def rsplit_multichar_sep_small(STR): |
|---|
| 935 | n/a | s = STR("this--is--a--test--of--the--emergency--broadcast--system") |
|---|
| 936 | n/a | s_rsplit = s.rsplit |
|---|
| 937 | n/a | pat = STR("--") |
|---|
| 938 | n/a | for x in _RANGE_1000: |
|---|
| 939 | n/a | s_rsplit(pat) |
|---|
| 940 | n/a | |
|---|
| 941 | n/a | ## split dna text on "ACTAT" characters |
|---|
| 942 | n/a | @bench('dna.split("ACTAT")', |
|---|
| 943 | n/a | "split on multicharacter separator (dna)", 10) |
|---|
| 944 | n/a | def split_multichar_sep_dna(STR): |
|---|
| 945 | n/a | s = _get_dna(STR) |
|---|
| 946 | n/a | s_split = s.split |
|---|
| 947 | n/a | pat = STR("ACTAT") |
|---|
| 948 | n/a | for x in _RANGE_10: |
|---|
| 949 | n/a | s_split(pat) |
|---|
| 950 | n/a | |
|---|
| 951 | n/a | @bench('dna.rsplit("ACTAT")', |
|---|
| 952 | n/a | "split on multicharacter separator (dna)", 10) |
|---|
| 953 | n/a | def rsplit_multichar_sep_dna(STR): |
|---|
| 954 | n/a | s = _get_dna(STR) |
|---|
| 955 | n/a | s_rsplit = s.rsplit |
|---|
| 956 | n/a | pat = STR("ACTAT") |
|---|
| 957 | n/a | for x in _RANGE_10: |
|---|
| 958 | n/a | s_rsplit(pat) |
|---|
| 959 | n/a | |
|---|
| 960 | n/a | |
|---|
| 961 | n/a | |
|---|
| 962 | n/a | ## split with limits |
|---|
| 963 | n/a | |
|---|
| 964 | n/a | GFF3_example = "\t".join([ |
|---|
| 965 | n/a | "I", "Genomic_canonical", "region", "357208", "396183", ".", "+", ".", |
|---|
| 966 | n/a | "ID=Sequence:R119;note=Clone R119%3B Genbank AF063007;Name=R119"]) |
|---|
| 967 | n/a | |
|---|
| 968 | n/a | @bench('GFF3_example.split("\\t")', "tab split", 1000) |
|---|
| 969 | n/a | def tab_split_no_limit(STR): |
|---|
| 970 | n/a | sep = STR("\t") |
|---|
| 971 | n/a | s = STR(GFF3_example) |
|---|
| 972 | n/a | s_split = s.split |
|---|
| 973 | n/a | for x in _RANGE_1000: |
|---|
| 974 | n/a | s_split(sep) |
|---|
| 975 | n/a | |
|---|
| 976 | n/a | @bench('GFF3_example.split("\\t", 8)', "tab split", 1000) |
|---|
| 977 | n/a | def tab_split_limit(STR): |
|---|
| 978 | n/a | sep = STR("\t") |
|---|
| 979 | n/a | s = STR(GFF3_example) |
|---|
| 980 | n/a | s_split = s.split |
|---|
| 981 | n/a | for x in _RANGE_1000: |
|---|
| 982 | n/a | s_split(sep, 8) |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | @bench('GFF3_example.rsplit("\\t")', "tab split", 1000) |
|---|
| 985 | n/a | def tab_rsplit_no_limit(STR): |
|---|
| 986 | n/a | sep = STR("\t") |
|---|
| 987 | n/a | s = STR(GFF3_example) |
|---|
| 988 | n/a | s_rsplit = s.rsplit |
|---|
| 989 | n/a | for x in _RANGE_1000: |
|---|
| 990 | n/a | s_rsplit(sep) |
|---|
| 991 | n/a | |
|---|
| 992 | n/a | @bench('GFF3_example.rsplit("\\t", 8)', "tab split", 1000) |
|---|
| 993 | n/a | def tab_rsplit_limit(STR): |
|---|
| 994 | n/a | sep = STR("\t") |
|---|
| 995 | n/a | s = STR(GFF3_example) |
|---|
| 996 | n/a | s_rsplit = s.rsplit |
|---|
| 997 | n/a | for x in _RANGE_1000: |
|---|
| 998 | n/a | s_rsplit(sep, 8) |
|---|
| 999 | n/a | |
|---|
| 1000 | n/a | #### Count characters |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | @bench('...text.with.2000.newlines.count("\\n")', |
|---|
| 1003 | n/a | "count newlines", 10) |
|---|
| 1004 | n/a | def count_newlines(STR): |
|---|
| 1005 | n/a | s = _get_2000_lines(STR) |
|---|
| 1006 | n/a | s_count = s.count |
|---|
| 1007 | n/a | nl = STR("\n") |
|---|
| 1008 | n/a | for x in _RANGE_10: |
|---|
| 1009 | n/a | s_count(nl) |
|---|
| 1010 | n/a | |
|---|
| 1011 | n/a | # Orchid sequences concatenated, from Biopython |
|---|
| 1012 | n/a | _dna = """ |
|---|
| 1013 | n/a | CGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGGGTT |
|---|
| 1014 | n/a | AATCTGGAGGATCTGTTTACTTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGAATTGCCATCG |
|---|
| 1015 | n/a | AGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGCAGTTTTGCTCCAAGTCGTT |
|---|
| 1016 | n/a | TGACACATAATTGGTGAAGGGGGTGGCATCCTTCCCTGACCCTCCCCCAACTATTTTTTTAACAACTCTC |
|---|
| 1017 | n/a | AGCAACGGAGACTCAGTCTTCGGCAAATGCGATAAATGGTGTGAATTGCAGAATCCCGTGCACCATCGAG |
|---|
| 1018 | n/a | TCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCTGCCTGGGCATTGCGAGTCATAT |
|---|
| 1019 | n/a | CTCTCCCTTAACGAGGCTGTCCATACATACTGTTCAGCCGGTGCGGATGTGAGTTTGGCCCCTTGTTCTT |
|---|
| 1020 | n/a | TGGTACGGGGGGTCTAAGAGCTGCATGGGCTTTTGATGGTCCTAAATACGGCAAGAGGTGGACGAACTAT |
|---|
| 1021 | n/a | GCTACAACAAAATTGTTGTGCAGAGGCCCCGGGTTGTCGTATTAGATGGGCCACCGTAATCTGAAGACCC |
|---|
| 1022 | n/a | TTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGGCCATTTGGTTGCGACCCCAGGTCAG |
|---|
| 1023 | n/a | GTGAGCAACAGCTGTCGTAACAAGGTTTCCGTAGGGTGAACTGCGGAAGGATCATTGTTGAGATCACATA |
|---|
| 1024 | n/a | ATAATTGATCGAGTTAATCTGGAGGATCTGTTTACTTGGGTCACCCATGGGCATTTGCTGTTGAAGTGAC |
|---|
| 1025 | n/a | CTAGATTTGCCATCGAGCCTCCTTGGGAGCATCCTTGTTGGCGATATCTAAACCCTCAATTTTTCCCCCA |
|---|
| 1026 | n/a | ATCAAATTACACAAAATTGGTGGAGGGGGTGGCATTCTTCCCTTACCCTCCCCCAAATATTTTTTTAACA |
|---|
| 1027 | n/a | ACTCTCAGCAACGGATATCTCAGCTCTTGCATCGATGAAGAACCCACCGAAATGCGATAAATGGTGTGAA |
|---|
| 1028 | n/a | TTGCAGAATCCCGTGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACG |
|---|
| 1029 | n/a | CCTGCCTGGGCATTGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACATACTGTTCAGCCGGTGCG |
|---|
| 1030 | n/a | GATGTGAGTTTGGCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGATGCATGGGCTTTTGATGGTCCTAA |
|---|
| 1031 | n/a | ATACGGCAAGAGGTGGACGAACTATGCTACAACAAAATTGTTGTGCAAAGGCCCCGGGTTGTCGTATAAG |
|---|
| 1032 | n/a | ATGGGCCACCGATATCTGAAGACCCTTTTGGACCCCATTGGAGCCCATCAACCCATGTCAGTTGATGGCC |
|---|
| 1033 | n/a | ATTCGTAACAAGGTTTCCGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGA |
|---|
| 1034 | n/a | GTTAATCTGGAGGATCTGTTTACTTGGGTCACCCATGGGCATTTGCTGTTGAAGTGACCTAGATTTGCCA |
|---|
| 1035 | n/a | TCGAGCCTCCTTGGGAGCTTTCTTGTTGGCGATATCTAAACCCTTGCCCGGCAGAGTTTTGGGAATCCCG |
|---|
| 1036 | n/a | TGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCTGCCTGGGCAT |
|---|
| 1037 | n/a | TGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACACACCTGTTCAGCCGGTGCGGATGTGAGTTTG |
|---|
| 1038 | n/a | GCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGCTGCATGGGCTTTTGATGGTCCTAAATACGGCAAGAG |
|---|
| 1039 | n/a | GTGGACGAACTATGCTACAACAAAATTGTTGTGCAAAGGCCCCGGGTTGTCGTATTAGATGGGCCACCAT |
|---|
| 1040 | n/a | AATCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGGCCATTTGGTTGC |
|---|
| 1041 | n/a | GACCCAGTCAGGTGAGGGTAGGTGAACCTGCGGAAGGATCATTGTTGAGATCACATAATAATTGATCGAG |
|---|
| 1042 | n/a | TTAATCTGGAGGATCTGTTTACTTTGGTCACCCATGGGCATTTGCTGTTGAAGTGACCTAGATTTGCCAT |
|---|
| 1043 | n/a | CGAGCCTCCTTGGGAGCTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGGAGTTTGGCGCCAAGTCA |
|---|
| 1044 | n/a | TATGACACATAATTGGTGAAGGGGGTGGCATCCTGCCCTGACCCTCCCCAAATTATTTTTTTAACAACTC |
|---|
| 1045 | n/a | TCAGCAACGGATATCTCGGCTCTTGCATCGATGAAGAACGCAGCGAAATGCGATAAATGGTGTGAATTGC |
|---|
| 1046 | n/a | AGAATCCCGTGAACCATCGAGTCTTTGGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCACGCCT |
|---|
| 1047 | n/a | GCCTGGGCATTGGGAATCATATCTCTCCCCTAACGAGGCTATCCAAACATACTGTTCATCCGGTGCGGAT |
|---|
| 1048 | n/a | GTGAGTTTGGCCCCTTGTTCTTTGGTACCGGGGGTCTAAGAGCTGCATGGGCATTTGATGGTCCTCAAAA |
|---|
| 1049 | n/a | CGGCAAGAGGTGGACGAACTATGCCACAACAAAATTGTTGTCCCAAGGCCCCGGGTTGTCGTATTAGATG |
|---|
| 1050 | n/a | GGCCACCGTAACCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATGACCA |
|---|
| 1051 | n/a | TTTGTTGCGACCCCAGTCAGCTGAGCAACCCGCTGAGTGGAAGGTCATTGCCGATATCACATAATAATTG |
|---|
| 1052 | n/a | ATCGAGTTAATCTGGAGGATCTGTTTACTTGGTCACCCATGAGCATTTGCTGTTGAAGTGACCTAGATTT |
|---|
| 1053 | n/a | GCCATCGAGCCTCCTTGGGAGTTTTCTTGTTGGCGAGATCTAAACCCTTGCCCGGCGGAGTTGTGCGCCA |
|---|
| 1054 | n/a | AGTCATATGACACATAATTGGTGAAGGGGGTGGCATCCTGCCCTGACCCTCCCCAAATTATTTTTTTAAC |
|---|
| 1055 | n/a | AACTCTCAGCAACGGATATCTCGGCTCTTGCATCGATGAAGAACGCAGCGAAATGCGATAAATGGTGTGA |
|---|
| 1056 | n/a | ATTGCAGAATCCCGTGAACCATCGAGTCTTTGAACGCAAGTTGCGCCCGAGGCCATCAGGCCAAGGGCAC |
|---|
| 1057 | n/a | GCCTGCCTGGGCATTGCGAGTCATATCTCTCCCTTAACGAGGCTGTCCATACATACTGTTCATCCGGTGC |
|---|
| 1058 | n/a | GGATGTGAGTTTGGCCCCTTGTTCTTTGGTACGGGGGGTCTAAGAGCTGCATGGGCATTTGATGGTCCTC |
|---|
| 1059 | n/a | AAAACGGCAAGAGGTGGACGAACTATGCTACAACCAAATTGTTGTCCCAAGGCCCCGGGTTGTCGTATTA |
|---|
| 1060 | n/a | GATGGGCCACCGTAACCTGAAGACCCTTTTGAACCCCATTGGAGGCCCATCAACCCATGATCAGTTGATG |
|---|
| 1061 | n/a | ACCATGTGTTGCGACCCCAGTCAGCTGAGCAACGCGCTGAGCGTAACAAGGTTTCCGTAGGTGGACCTCC |
|---|
| 1062 | n/a | GGGAGGATCATTGTTGAGATCACATAATAATTGATCGAGGTAATCTGGAGGATCTGCATATTTTGGTCAC |
|---|
| 1063 | n/a | """ |
|---|
| 1064 | n/a | _dna = "".join(_dna.splitlines()) |
|---|
| 1065 | n/a | _dna = _dna * 25 |
|---|
| 1066 | n/a | _dna_bytes = bytes_from_str(_dna) |
|---|
| 1067 | n/a | _dna_unicode = unicode_from_str(_dna) |
|---|
| 1068 | n/a | |
|---|
| 1069 | n/a | def _get_dna(STR): |
|---|
| 1070 | n/a | if STR is UNICODE: |
|---|
| 1071 | n/a | return _dna_unicode |
|---|
| 1072 | n/a | if STR is BYTES: |
|---|
| 1073 | n/a | return _dna_bytes |
|---|
| 1074 | n/a | raise AssertionError |
|---|
| 1075 | n/a | |
|---|
| 1076 | n/a | @bench('dna.count("AACT")', "count AACT substrings in DNA example", 10) |
|---|
| 1077 | n/a | def count_aact(STR): |
|---|
| 1078 | n/a | seq = _get_dna(STR) |
|---|
| 1079 | n/a | seq_count = seq.count |
|---|
| 1080 | n/a | needle = STR("AACT") |
|---|
| 1081 | n/a | for x in _RANGE_10: |
|---|
| 1082 | n/a | seq_count(needle) |
|---|
| 1083 | n/a | |
|---|
| 1084 | n/a | ##### startswith and endswith |
|---|
| 1085 | n/a | |
|---|
| 1086 | n/a | @bench('"Andrew".startswith("A")', 'startswith single character', 1000) |
|---|
| 1087 | n/a | def startswith_single(STR): |
|---|
| 1088 | n/a | s1 = STR("Andrew") |
|---|
| 1089 | n/a | s2 = STR("A") |
|---|
| 1090 | n/a | s1_startswith = s1.startswith |
|---|
| 1091 | n/a | for x in _RANGE_1000: |
|---|
| 1092 | n/a | s1_startswith(s2) |
|---|
| 1093 | n/a | |
|---|
| 1094 | n/a | @bench('"Andrew".startswith("Andrew")', 'startswith multiple characters', |
|---|
| 1095 | n/a | 1000) |
|---|
| 1096 | n/a | def startswith_multiple(STR): |
|---|
| 1097 | n/a | s1 = STR("Andrew") |
|---|
| 1098 | n/a | s2 = STR("Andrew") |
|---|
| 1099 | n/a | s1_startswith = s1.startswith |
|---|
| 1100 | n/a | for x in _RANGE_1000: |
|---|
| 1101 | n/a | s1_startswith(s2) |
|---|
| 1102 | n/a | |
|---|
| 1103 | n/a | @bench('"Andrew".startswith("Anders")', |
|---|
| 1104 | n/a | 'startswith multiple characters - not!', 1000) |
|---|
| 1105 | n/a | def startswith_multiple_not(STR): |
|---|
| 1106 | n/a | s1 = STR("Andrew") |
|---|
| 1107 | n/a | s2 = STR("Anders") |
|---|
| 1108 | n/a | s1_startswith = s1.startswith |
|---|
| 1109 | n/a | for x in _RANGE_1000: |
|---|
| 1110 | n/a | s1_startswith(s2) |
|---|
| 1111 | n/a | |
|---|
| 1112 | n/a | |
|---|
| 1113 | n/a | # endswith |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | @bench('"Andrew".endswith("w")', 'endswith single character', 1000) |
|---|
| 1116 | n/a | def endswith_single(STR): |
|---|
| 1117 | n/a | s1 = STR("Andrew") |
|---|
| 1118 | n/a | s2 = STR("w") |
|---|
| 1119 | n/a | s1_endswith = s1.endswith |
|---|
| 1120 | n/a | for x in _RANGE_1000: |
|---|
| 1121 | n/a | s1_endswith(s2) |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | @bench('"Andrew".endswith("Andrew")', 'endswith multiple characters', 1000) |
|---|
| 1124 | n/a | def endswith_multiple(STR): |
|---|
| 1125 | n/a | s1 = STR("Andrew") |
|---|
| 1126 | n/a | s2 = STR("Andrew") |
|---|
| 1127 | n/a | s1_endswith = s1.endswith |
|---|
| 1128 | n/a | for x in _RANGE_1000: |
|---|
| 1129 | n/a | s1_endswith(s2) |
|---|
| 1130 | n/a | |
|---|
| 1131 | n/a | @bench('"Andrew".endswith("Anders")', |
|---|
| 1132 | n/a | 'endswith multiple characters - not!', 1000) |
|---|
| 1133 | n/a | def endswith_multiple_not(STR): |
|---|
| 1134 | n/a | s1 = STR("Andrew") |
|---|
| 1135 | n/a | s2 = STR("Anders") |
|---|
| 1136 | n/a | s1_endswith = s1.endswith |
|---|
| 1137 | n/a | for x in _RANGE_1000: |
|---|
| 1138 | n/a | s1_endswith(s2) |
|---|
| 1139 | n/a | |
|---|
| 1140 | n/a | #### Strip |
|---|
| 1141 | n/a | |
|---|
| 1142 | n/a | @bench('"Hello!\\n".strip()', 'strip terminal newline', 1000) |
|---|
| 1143 | n/a | def terminal_newline_strip_right(STR): |
|---|
| 1144 | n/a | s = STR("Hello!\n") |
|---|
| 1145 | n/a | s_strip = s.strip |
|---|
| 1146 | n/a | for x in _RANGE_1000: |
|---|
| 1147 | n/a | s_strip() |
|---|
| 1148 | n/a | |
|---|
| 1149 | n/a | @bench('"Hello!\\n".rstrip()', 'strip terminal newline', 1000) |
|---|
| 1150 | n/a | def terminal_newline_rstrip(STR): |
|---|
| 1151 | n/a | s = STR("Hello!\n") |
|---|
| 1152 | n/a | s_rstrip = s.rstrip |
|---|
| 1153 | n/a | for x in _RANGE_1000: |
|---|
| 1154 | n/a | s_rstrip() |
|---|
| 1155 | n/a | |
|---|
| 1156 | n/a | @bench('"\\nHello!".strip()', 'strip terminal newline', 1000) |
|---|
| 1157 | n/a | def terminal_newline_strip_left(STR): |
|---|
| 1158 | n/a | s = STR("\nHello!") |
|---|
| 1159 | n/a | s_strip = s.strip |
|---|
| 1160 | n/a | for x in _RANGE_1000: |
|---|
| 1161 | n/a | s_strip() |
|---|
| 1162 | n/a | |
|---|
| 1163 | n/a | @bench('"\\nHello!\\n".strip()', 'strip terminal newline', 1000) |
|---|
| 1164 | n/a | def terminal_newline_strip_both(STR): |
|---|
| 1165 | n/a | s = STR("\nHello!\n") |
|---|
| 1166 | n/a | s_strip = s.strip |
|---|
| 1167 | n/a | for x in _RANGE_1000: |
|---|
| 1168 | n/a | s_strip() |
|---|
| 1169 | n/a | |
|---|
| 1170 | n/a | @bench('"\\nHello!".rstrip()', 'strip terminal newline', 1000) |
|---|
| 1171 | n/a | def terminal_newline_lstrip(STR): |
|---|
| 1172 | n/a | s = STR("\nHello!") |
|---|
| 1173 | n/a | s_lstrip = s.lstrip |
|---|
| 1174 | n/a | for x in _RANGE_1000: |
|---|
| 1175 | n/a | s_lstrip() |
|---|
| 1176 | n/a | |
|---|
| 1177 | n/a | @bench('s="Hello!\\n"; s[:-1] if s[-1]=="\\n" else s', |
|---|
| 1178 | n/a | 'strip terminal newline', 1000) |
|---|
| 1179 | n/a | def terminal_newline_if_else(STR): |
|---|
| 1180 | n/a | s = STR("Hello!\n") |
|---|
| 1181 | n/a | NL = STR("\n") |
|---|
| 1182 | n/a | for x in _RANGE_1000: |
|---|
| 1183 | n/a | s[:-1] if (s[-1] == NL) else s |
|---|
| 1184 | n/a | |
|---|
| 1185 | n/a | |
|---|
| 1186 | n/a | # Strip multiple spaces or tabs |
|---|
| 1187 | n/a | |
|---|
| 1188 | n/a | @bench('"Hello\\t \\t".strip()', 'strip terminal spaces and tabs', 1000) |
|---|
| 1189 | n/a | def terminal_space_strip(STR): |
|---|
| 1190 | n/a | s = STR("Hello\t \t!") |
|---|
| 1191 | n/a | s_strip = s.strip |
|---|
| 1192 | n/a | for x in _RANGE_1000: |
|---|
| 1193 | n/a | s_strip() |
|---|
| 1194 | n/a | |
|---|
| 1195 | n/a | @bench('"Hello\\t \\t".rstrip()', 'strip terminal spaces and tabs', 1000) |
|---|
| 1196 | n/a | def terminal_space_rstrip(STR): |
|---|
| 1197 | n/a | s = STR("Hello!\t \t") |
|---|
| 1198 | n/a | s_rstrip = s.rstrip |
|---|
| 1199 | n/a | for x in _RANGE_1000: |
|---|
| 1200 | n/a | s_rstrip() |
|---|
| 1201 | n/a | |
|---|
| 1202 | n/a | @bench('"\\t \\tHello".rstrip()', 'strip terminal spaces and tabs', 1000) |
|---|
| 1203 | n/a | def terminal_space_lstrip(STR): |
|---|
| 1204 | n/a | s = STR("\t \tHello!") |
|---|
| 1205 | n/a | s_lstrip = s.lstrip |
|---|
| 1206 | n/a | for x in _RANGE_1000: |
|---|
| 1207 | n/a | s_lstrip() |
|---|
| 1208 | n/a | |
|---|
| 1209 | n/a | |
|---|
| 1210 | n/a | #### replace |
|---|
| 1211 | n/a | @bench('"This is a test".replace(" ", "\\t")', 'replace single character', |
|---|
| 1212 | n/a | 1000) |
|---|
| 1213 | n/a | def replace_single_character(STR): |
|---|
| 1214 | n/a | s = STR("This is a test!") |
|---|
| 1215 | n/a | from_str = STR(" ") |
|---|
| 1216 | n/a | to_str = STR("\t") |
|---|
| 1217 | n/a | s_replace = s.replace |
|---|
| 1218 | n/a | for x in _RANGE_1000: |
|---|
| 1219 | n/a | s_replace(from_str, to_str) |
|---|
| 1220 | n/a | |
|---|
| 1221 | n/a | @uses_re |
|---|
| 1222 | n/a | @bench('re.sub(" ", "\\t", "This is a test"', 'replace single character', |
|---|
| 1223 | n/a | 1000) |
|---|
| 1224 | n/a | def replace_single_character_re(STR): |
|---|
| 1225 | n/a | s = STR("This is a test!") |
|---|
| 1226 | n/a | pat = re.compile(STR(" ")) |
|---|
| 1227 | n/a | to_str = STR("\t") |
|---|
| 1228 | n/a | pat_sub = pat.sub |
|---|
| 1229 | n/a | for x in _RANGE_1000: |
|---|
| 1230 | n/a | pat_sub(to_str, s) |
|---|
| 1231 | n/a | |
|---|
| 1232 | n/a | @bench('"...text.with.2000.lines...replace("\\n", " ")', |
|---|
| 1233 | n/a | 'replace single character, big string', 10) |
|---|
| 1234 | n/a | def replace_single_character_big(STR): |
|---|
| 1235 | n/a | s = _get_2000_lines(STR) |
|---|
| 1236 | n/a | from_str = STR("\n") |
|---|
| 1237 | n/a | to_str = STR(" ") |
|---|
| 1238 | n/a | s_replace = s.replace |
|---|
| 1239 | n/a | for x in _RANGE_10: |
|---|
| 1240 | n/a | s_replace(from_str, to_str) |
|---|
| 1241 | n/a | |
|---|
| 1242 | n/a | @uses_re |
|---|
| 1243 | n/a | @bench('re.sub("\\n", " ", "...text.with.2000.lines...")', |
|---|
| 1244 | n/a | 'replace single character, big string', 10) |
|---|
| 1245 | n/a | def replace_single_character_big_re(STR): |
|---|
| 1246 | n/a | s = _get_2000_lines(STR) |
|---|
| 1247 | n/a | pat = re.compile(STR("\n")) |
|---|
| 1248 | n/a | to_str = STR(" ") |
|---|
| 1249 | n/a | pat_sub = pat.sub |
|---|
| 1250 | n/a | for x in _RANGE_10: |
|---|
| 1251 | n/a | pat_sub(to_str, s) |
|---|
| 1252 | n/a | |
|---|
| 1253 | n/a | |
|---|
| 1254 | n/a | @bench('dna.replace("ATC", "ATT")', |
|---|
| 1255 | n/a | 'replace multiple characters, dna', 10) |
|---|
| 1256 | n/a | def replace_multiple_characters_dna(STR): |
|---|
| 1257 | n/a | seq = _get_dna(STR) |
|---|
| 1258 | n/a | from_str = STR("ATC") |
|---|
| 1259 | n/a | to_str = STR("ATT") |
|---|
| 1260 | n/a | seq_replace = seq.replace |
|---|
| 1261 | n/a | for x in _RANGE_10: |
|---|
| 1262 | n/a | seq_replace(from_str, to_str) |
|---|
| 1263 | n/a | |
|---|
| 1264 | n/a | # This increases the character count |
|---|
| 1265 | n/a | @bench('"...text.with.2000.newlines...replace("\\n", "\\r\\n")', |
|---|
| 1266 | n/a | 'replace and expand multiple characters, big string', 10) |
|---|
| 1267 | n/a | def replace_multiple_character_big(STR): |
|---|
| 1268 | n/a | s = _get_2000_lines(STR) |
|---|
| 1269 | n/a | from_str = STR("\n") |
|---|
| 1270 | n/a | to_str = STR("\r\n") |
|---|
| 1271 | n/a | s_replace = s.replace |
|---|
| 1272 | n/a | for x in _RANGE_10: |
|---|
| 1273 | n/a | s_replace(from_str, to_str) |
|---|
| 1274 | n/a | |
|---|
| 1275 | n/a | |
|---|
| 1276 | n/a | # This decreases the character count |
|---|
| 1277 | n/a | @bench('"When shall we three meet again?".replace("ee", "")', |
|---|
| 1278 | n/a | 'replace/remove multiple characters', 1000) |
|---|
| 1279 | n/a | def replace_multiple_character_remove(STR): |
|---|
| 1280 | n/a | s = STR("When shall we three meet again?") |
|---|
| 1281 | n/a | from_str = STR("ee") |
|---|
| 1282 | n/a | to_str = STR("") |
|---|
| 1283 | n/a | s_replace = s.replace |
|---|
| 1284 | n/a | for x in _RANGE_1000: |
|---|
| 1285 | n/a | s_replace(from_str, to_str) |
|---|
| 1286 | n/a | |
|---|
| 1287 | n/a | |
|---|
| 1288 | n/a | big_s = "A" + ("Z"*128*1024) |
|---|
| 1289 | n/a | big_s_bytes = bytes_from_str(big_s) |
|---|
| 1290 | n/a | big_s_unicode = unicode_from_str(big_s) |
|---|
| 1291 | n/a | def _get_big_s(STR): |
|---|
| 1292 | n/a | if STR is UNICODE: return big_s_unicode |
|---|
| 1293 | n/a | if STR is BYTES: return big_s_bytes |
|---|
| 1294 | n/a | raise AssertionError |
|---|
| 1295 | n/a | |
|---|
| 1296 | n/a | # The older replace implementation counted all matches in |
|---|
| 1297 | n/a | # the string even when it only needed to make one replacement. |
|---|
| 1298 | n/a | @bench('("A" + ("Z"*128*1024)).replace("A", "BB", 1)', |
|---|
| 1299 | n/a | 'quick replace single character match', 10) |
|---|
| 1300 | n/a | def quick_replace_single_match(STR): |
|---|
| 1301 | n/a | s = _get_big_s(STR) |
|---|
| 1302 | n/a | from_str = STR("A") |
|---|
| 1303 | n/a | to_str = STR("BB") |
|---|
| 1304 | n/a | s_replace = s.replace |
|---|
| 1305 | n/a | for x in _RANGE_10: |
|---|
| 1306 | n/a | s_replace(from_str, to_str, 1) |
|---|
| 1307 | n/a | |
|---|
| 1308 | n/a | @bench('("A" + ("Z"*128*1024)).replace("AZZ", "BBZZ", 1)', |
|---|
| 1309 | n/a | 'quick replace multiple character match', 10) |
|---|
| 1310 | n/a | def quick_replace_multiple_match(STR): |
|---|
| 1311 | n/a | s = _get_big_s(STR) |
|---|
| 1312 | n/a | from_str = STR("AZZ") |
|---|
| 1313 | n/a | to_str = STR("BBZZ") |
|---|
| 1314 | n/a | s_replace = s.replace |
|---|
| 1315 | n/a | for x in _RANGE_10: |
|---|
| 1316 | n/a | s_replace(from_str, to_str, 1) |
|---|
| 1317 | n/a | |
|---|
| 1318 | n/a | |
|---|
| 1319 | n/a | #### |
|---|
| 1320 | n/a | |
|---|
| 1321 | n/a | # CCP does a lot of this, for internationalisation of ingame messages. |
|---|
| 1322 | n/a | _format = "The %(thing)s is %(place)s the %(location)s." |
|---|
| 1323 | n/a | _format_dict = { "thing":"THING", "place":"PLACE", "location":"LOCATION", } |
|---|
| 1324 | n/a | _format_bytes = bytes_from_str(_format) |
|---|
| 1325 | n/a | _format_unicode = unicode_from_str(_format) |
|---|
| 1326 | n/a | _format_dict_bytes = dict((bytes_from_str(k), bytes_from_str(v)) for (k,v) in _format_dict.items()) |
|---|
| 1327 | n/a | _format_dict_unicode = dict((unicode_from_str(k), unicode_from_str(v)) for (k,v) in _format_dict.items()) |
|---|
| 1328 | n/a | |
|---|
| 1329 | n/a | def _get_format(STR): |
|---|
| 1330 | n/a | if STR is UNICODE: |
|---|
| 1331 | n/a | return _format_unicode |
|---|
| 1332 | n/a | if STR is BYTES: |
|---|
| 1333 | n/a | if sys.version_info >= (3,): |
|---|
| 1334 | n/a | raise UnsupportedType |
|---|
| 1335 | n/a | return _format_bytes |
|---|
| 1336 | n/a | raise AssertionError |
|---|
| 1337 | n/a | |
|---|
| 1338 | n/a | def _get_format_dict(STR): |
|---|
| 1339 | n/a | if STR is UNICODE: |
|---|
| 1340 | n/a | return _format_dict_unicode |
|---|
| 1341 | n/a | if STR is BYTES: |
|---|
| 1342 | n/a | if sys.version_info >= (3,): |
|---|
| 1343 | n/a | raise UnsupportedType |
|---|
| 1344 | n/a | return _format_dict_bytes |
|---|
| 1345 | n/a | raise AssertionError |
|---|
| 1346 | n/a | |
|---|
| 1347 | n/a | # Formatting. |
|---|
| 1348 | n/a | @bench('"The %(k1)s is %(k2)s the %(k3)s."%{"k1":"x","k2":"y","k3":"z",}', |
|---|
| 1349 | n/a | 'formatting a string type with a dict', 1000) |
|---|
| 1350 | n/a | def format_with_dict(STR): |
|---|
| 1351 | n/a | s = _get_format(STR) |
|---|
| 1352 | n/a | d = _get_format_dict(STR) |
|---|
| 1353 | n/a | for x in _RANGE_1000: |
|---|
| 1354 | n/a | s % d |
|---|
| 1355 | n/a | |
|---|
| 1356 | n/a | |
|---|
| 1357 | n/a | #### Upper- and lower- case conversion |
|---|
| 1358 | n/a | |
|---|
| 1359 | n/a | @bench('("Where in the world is Carmen San Deigo?"*10).lower()', |
|---|
| 1360 | n/a | "case conversion -- rare", 1000) |
|---|
| 1361 | n/a | def lower_conversion_rare(STR): |
|---|
| 1362 | n/a | s = STR("Where in the world is Carmen San Deigo?"*10) |
|---|
| 1363 | n/a | s_lower = s.lower |
|---|
| 1364 | n/a | for x in _RANGE_1000: |
|---|
| 1365 | n/a | s_lower() |
|---|
| 1366 | n/a | |
|---|
| 1367 | n/a | @bench('("WHERE IN THE WORLD IS CARMEN SAN DEIGO?"*10).lower()', |
|---|
| 1368 | n/a | "case conversion -- dense", 1000) |
|---|
| 1369 | n/a | def lower_conversion_dense(STR): |
|---|
| 1370 | n/a | s = STR("WHERE IN THE WORLD IS CARMEN SAN DEIGO?"*10) |
|---|
| 1371 | n/a | s_lower = s.lower |
|---|
| 1372 | n/a | for x in _RANGE_1000: |
|---|
| 1373 | n/a | s_lower() |
|---|
| 1374 | n/a | |
|---|
| 1375 | n/a | |
|---|
| 1376 | n/a | @bench('("wHERE IN THE WORLD IS cARMEN sAN dEIGO?"*10).upper()', |
|---|
| 1377 | n/a | "case conversion -- rare", 1000) |
|---|
| 1378 | n/a | def upper_conversion_rare(STR): |
|---|
| 1379 | n/a | s = STR("Where in the world is Carmen San Deigo?"*10) |
|---|
| 1380 | n/a | s_upper = s.upper |
|---|
| 1381 | n/a | for x in _RANGE_1000: |
|---|
| 1382 | n/a | s_upper() |
|---|
| 1383 | n/a | |
|---|
| 1384 | n/a | @bench('("where in the world is carmen san deigo?"*10).upper()', |
|---|
| 1385 | n/a | "case conversion -- dense", 1000) |
|---|
| 1386 | n/a | def upper_conversion_dense(STR): |
|---|
| 1387 | n/a | s = STR("where in the world is carmen san deigo?"*10) |
|---|
| 1388 | n/a | s_upper = s.upper |
|---|
| 1389 | n/a | for x in _RANGE_1000: |
|---|
| 1390 | n/a | s_upper() |
|---|
| 1391 | n/a | |
|---|
| 1392 | n/a | |
|---|
| 1393 | n/a | # end of benchmarks |
|---|
| 1394 | n/a | |
|---|
| 1395 | n/a | ################# |
|---|
| 1396 | n/a | |
|---|
| 1397 | n/a | class BenchTimer(timeit.Timer): |
|---|
| 1398 | n/a | def best(self, repeat=1): |
|---|
| 1399 | n/a | for i in range(1, 10): |
|---|
| 1400 | n/a | number = 10**i |
|---|
| 1401 | n/a | x = self.timeit(number) |
|---|
| 1402 | n/a | if x > 0.02: |
|---|
| 1403 | n/a | break |
|---|
| 1404 | n/a | times = [x] |
|---|
| 1405 | n/a | for i in range(1, repeat): |
|---|
| 1406 | n/a | times.append(self.timeit(number)) |
|---|
| 1407 | n/a | return min(times) / number |
|---|
| 1408 | n/a | |
|---|
| 1409 | n/a | def main(): |
|---|
| 1410 | n/a | (options, test_names) = parser.parse_args() |
|---|
| 1411 | n/a | if options.bytes_only and options.unicode_only: |
|---|
| 1412 | n/a | raise SystemExit("Only one of --8-bit and --unicode are allowed") |
|---|
| 1413 | n/a | |
|---|
| 1414 | n/a | bench_functions = [] |
|---|
| 1415 | n/a | for (k,v) in globals().items(): |
|---|
| 1416 | n/a | if hasattr(v, "is_bench"): |
|---|
| 1417 | n/a | if test_names: |
|---|
| 1418 | n/a | for name in test_names: |
|---|
| 1419 | n/a | if name in v.group: |
|---|
| 1420 | n/a | break |
|---|
| 1421 | n/a | else: |
|---|
| 1422 | n/a | # Not selected, ignore |
|---|
| 1423 | n/a | continue |
|---|
| 1424 | n/a | if options.skip_re and hasattr(v, "uses_re"): |
|---|
| 1425 | n/a | continue |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | bench_functions.append( (v.group, k, v) ) |
|---|
| 1428 | n/a | bench_functions.sort() |
|---|
| 1429 | n/a | |
|---|
| 1430 | n/a | p("bytes\tunicode") |
|---|
| 1431 | n/a | p("(in ms)\t(in ms)\t%\tcomment") |
|---|
| 1432 | n/a | |
|---|
| 1433 | n/a | bytes_total = uni_total = 0.0 |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | for title, group in itertools.groupby(bench_functions, |
|---|
| 1436 | n/a | operator.itemgetter(0)): |
|---|
| 1437 | n/a | # Flush buffer before each group |
|---|
| 1438 | n/a | sys.stdout.flush() |
|---|
| 1439 | n/a | p("="*10, title) |
|---|
| 1440 | n/a | for (_, k, v) in group: |
|---|
| 1441 | n/a | if hasattr(v, "is_bench"): |
|---|
| 1442 | n/a | bytes_time = 0.0 |
|---|
| 1443 | n/a | bytes_time_s = " - " |
|---|
| 1444 | n/a | if not options.unicode_only: |
|---|
| 1445 | n/a | try: |
|---|
| 1446 | n/a | bytes_time = BenchTimer("__main__.%s(__main__.BYTES)" % (k,), |
|---|
| 1447 | n/a | "import __main__").best(REPEAT) |
|---|
| 1448 | n/a | bytes_time_s = "%.2f" % (1000 * bytes_time) |
|---|
| 1449 | n/a | bytes_total += bytes_time |
|---|
| 1450 | n/a | except UnsupportedType: |
|---|
| 1451 | n/a | bytes_time_s = "N/A" |
|---|
| 1452 | n/a | uni_time = 0.0 |
|---|
| 1453 | n/a | uni_time_s = " - " |
|---|
| 1454 | n/a | if not options.bytes_only: |
|---|
| 1455 | n/a | try: |
|---|
| 1456 | n/a | uni_time = BenchTimer("__main__.%s(__main__.UNICODE)" % (k,), |
|---|
| 1457 | n/a | "import __main__").best(REPEAT) |
|---|
| 1458 | n/a | uni_time_s = "%.2f" % (1000 * uni_time) |
|---|
| 1459 | n/a | uni_total += uni_time |
|---|
| 1460 | n/a | except UnsupportedType: |
|---|
| 1461 | n/a | uni_time_s = "N/A" |
|---|
| 1462 | n/a | try: |
|---|
| 1463 | n/a | average = bytes_time/uni_time |
|---|
| 1464 | n/a | except (TypeError, ZeroDivisionError): |
|---|
| 1465 | n/a | average = 0.0 |
|---|
| 1466 | n/a | p("%s\t%s\t%.1f\t%s (*%d)" % ( |
|---|
| 1467 | n/a | bytes_time_s, uni_time_s, 100.*average, |
|---|
| 1468 | n/a | v.comment, v.repeat_count)) |
|---|
| 1469 | n/a | |
|---|
| 1470 | n/a | if bytes_total == uni_total == 0.0: |
|---|
| 1471 | n/a | p("That was zippy!") |
|---|
| 1472 | n/a | else: |
|---|
| 1473 | n/a | try: |
|---|
| 1474 | n/a | ratio = bytes_total/uni_total |
|---|
| 1475 | n/a | except ZeroDivisionError: |
|---|
| 1476 | n/a | ratio = 0.0 |
|---|
| 1477 | n/a | p("%.2f\t%.2f\t%.1f\t%s" % ( |
|---|
| 1478 | n/a | 1000*bytes_total, 1000*uni_total, 100.*ratio, |
|---|
| 1479 | n/a | "TOTAL")) |
|---|
| 1480 | n/a | |
|---|
| 1481 | n/a | if __name__ == "__main__": |
|---|
| 1482 | n/a | main() |
|---|