1from pybench import Test 2from string import join 3 4class ConcatStrings(Test): 5 6 version = 2.0 7 operations = 10 * 5 8 rounds = 100000 9 10 def test(self): 11 12 # Make sure the strings are *not* interned 13 s = join(map(str,range(100))) 14 t = join(map(str,range(1,101))) 15 16 for i in xrange(self.rounds): 17 t + s 18 t + s 19 t + s 20 t + s 21 t + s 22 23 t + s 24 t + s 25 t + s 26 t + s 27 t + s 28 29 t + s 30 t + s 31 t + s 32 t + s 33 t + s 34 35 t + s 36 t + s 37 t + s 38 t + s 39 t + s 40 41 t + s 42 t + s 43 t + s 44 t + s 45 t + s 46 47 t + s 48 t + s 49 t + s 50 t + s 51 t + s 52 53 t + s 54 t + s 55 t + s 56 t + s 57 t + s 58 59 t + s 60 t + s 61 t + s 62 t + s 63 t + s 64 65 t + s 66 t + s 67 t + s 68 t + s 69 t + s 70 71 t + s 72 t + s 73 t + s 74 t + s 75 t + s 76 77 def calibrate(self): 78 79 s = join(map(str,range(100))) 80 t = join(map(str,range(1,101))) 81 82 for i in xrange(self.rounds): 83 pass 84 85 86class CompareStrings(Test): 87 88 version = 2.0 89 operations = 10 * 5 90 rounds = 200000 91 92 def test(self): 93 94 # Make sure the strings are *not* interned 95 s = join(map(str,range(10))) 96 t = join(map(str,range(10))) + "abc" 97 98 for i in xrange(self.rounds): 99 t < s 100 t > s 101 t == s 102 t > s 103 t < s 104 105 t < s 106 t > s 107 t == s 108 t > s 109 t < s 110 111 t < s 112 t > s 113 t == s 114 t > s 115 t < s 116 117 t < s 118 t > s 119 t == s 120 t > s 121 t < s 122 123 t < s 124 t > s 125 t == s 126 t > s 127 t < s 128 129 t < s 130 t > s 131 t == s 132 t > s 133 t < s 134 135 t < s 136 t > s 137 t == s 138 t > s 139 t < s 140 141 t < s 142 t > s 143 t == s 144 t > s 145 t < s 146 147 t < s 148 t > s 149 t == s 150 t > s 151 t < s 152 153 t < s 154 t > s 155 t == s 156 t > s 157 t < s 158 159 def calibrate(self): 160 161 s = join(map(str,range(10))) 162 t = join(map(str,range(10))) + "abc" 163 164 for i in xrange(self.rounds): 165 pass 166 167 168class CompareInternedStrings(Test): 169 170 version = 2.0 171 operations = 10 * 5 172 rounds = 300000 173 174 def test(self): 175 176 # Make sure the strings *are* interned 177 s = intern(join(map(str,range(10)))) 178 t = s 179 180 for i in xrange(self.rounds): 181 t == s 182 t == s 183 t >= s 184 t > s 185 t < s 186 187 t == s 188 t == s 189 t >= s 190 t > s 191 t < s 192 193 t == s 194 t == s 195 t >= s 196 t > s 197 t < s 198 199 t == s 200 t == s 201 t >= s 202 t > s 203 t < s 204 205 t == s 206 t == s 207 t >= s 208 t > s 209 t < s 210 211 t == s 212 t == s 213 t >= s 214 t > s 215 t < s 216 217 t == s 218 t == s 219 t >= s 220 t > s 221 t < s 222 223 t == s 224 t == s 225 t >= s 226 t > s 227 t < s 228 229 t == s 230 t == s 231 t >= s 232 t > s 233 t < s 234 235 t == s 236 t == s 237 t >= s 238 t > s 239 t < s 240 241 def calibrate(self): 242 243 s = intern(join(map(str,range(10)))) 244 t = s 245 246 for i in xrange(self.rounds): 247 pass 248 249 250class CreateStringsWithConcat(Test): 251 252 version = 2.0 253 operations = 10 * 5 254 rounds = 200000 255 256 def test(self): 257 258 for i in xrange(self.rounds): 259 s = 'om' 260 s = s + 'xbx' 261 s = s + 'xcx' 262 s = s + 'xdx' 263 s = s + 'xex' 264 265 s = s + 'xax' 266 s = s + 'xbx' 267 s = s + 'xcx' 268 s = s + 'xdx' 269 s = s + 'xex' 270 271 s = s + 'xax' 272 s = s + 'xbx' 273 s = s + 'xcx' 274 s = s + 'xdx' 275 s = s + 'xex' 276 277 s = s + 'xax' 278 s = s + 'xbx' 279 s = s + 'xcx' 280 s = s + 'xdx' 281 s = s + 'xex' 282 283 s = s + 'xax' 284 s = s + 'xbx' 285 s = s + 'xcx' 286 s = s + 'xdx' 287 s = s + 'xex' 288 289 s = s + 'xax' 290 s = s + 'xbx' 291 s = s + 'xcx' 292 s = s + 'xdx' 293 s = s + 'xex' 294 295 s = s + 'xax' 296 s = s + 'xbx' 297 s = s + 'xcx' 298 s = s + 'xdx' 299 s = s + 'xex' 300 301 s = s + 'xax' 302 s = s + 'xbx' 303 s = s + 'xcx' 304 s = s + 'xdx' 305 s = s + 'xex' 306 307 s = s + 'xax' 308 s = s + 'xbx' 309 s = s + 'xcx' 310 s = s + 'xdx' 311 s = s + 'xex' 312 313 s = s + 'xax' 314 s = s + 'xbx' 315 s = s + 'xcx' 316 s = s + 'xdx' 317 s = s + 'xex' 318 319 def calibrate(self): 320 321 for i in xrange(self.rounds): 322 pass 323 324 325class StringSlicing(Test): 326 327 version = 2.0 328 operations = 5 * 7 329 rounds = 160000 330 331 def test(self): 332 333 s = join(map(str,range(100))) 334 335 for i in xrange(self.rounds): 336 337 s[50:] 338 s[:25] 339 s[50:55] 340 s[-1:] 341 s[:1] 342 s[2:] 343 s[11:-11] 344 345 s[50:] 346 s[:25] 347 s[50:55] 348 s[-1:] 349 s[:1] 350 s[2:] 351 s[11:-11] 352 353 s[50:] 354 s[:25] 355 s[50:55] 356 s[-1:] 357 s[:1] 358 s[2:] 359 s[11:-11] 360 361 s[50:] 362 s[:25] 363 s[50:55] 364 s[-1:] 365 s[:1] 366 s[2:] 367 s[11:-11] 368 369 s[50:] 370 s[:25] 371 s[50:55] 372 s[-1:] 373 s[:1] 374 s[2:] 375 s[11:-11] 376 377 def calibrate(self): 378 379 s = join(map(str,range(100))) 380 381 for i in xrange(self.rounds): 382 pass 383 384### String methods 385 386if hasattr('', 'lower'): 387 388 class StringMappings(Test): 389 390 version = 2.0 391 operations = 3 * (5 + 4 + 2 + 1) 392 rounds = 70000 393 394 def test(self): 395 396 s = join(map(chr,range(20)),'') 397 t = join(map(chr,range(50)),'') 398 u = join(map(chr,range(100)),'') 399 v = join(map(chr,range(256)),'') 400 401 for i in xrange(self.rounds): 402 403 s.lower() 404 s.lower() 405 s.lower() 406 s.lower() 407 s.lower() 408 409 s.upper() 410 s.upper() 411 s.upper() 412 s.upper() 413 s.upper() 414 415 s.title() 416 s.title() 417 s.title() 418 s.title() 419 s.title() 420 421 t.lower() 422 t.lower() 423 t.lower() 424 t.lower() 425 426 t.upper() 427 t.upper() 428 t.upper() 429 t.upper() 430 431 t.title() 432 t.title() 433 t.title() 434 t.title() 435 436 u.lower() 437 u.lower() 438 439 u.upper() 440 u.upper() 441 442 u.title() 443 u.title() 444 445 v.lower() 446 447 v.upper() 448 449 v.title() 450 451 def calibrate(self): 452 453 s = join(map(chr,range(20)),'') 454 t = join(map(chr,range(50)),'') 455 u = join(map(chr,range(100)),'') 456 v = join(map(chr,range(256)),'') 457 458 for i in xrange(self.rounds): 459 pass 460 461 class StringPredicates(Test): 462 463 version = 2.0 464 operations = 10 * 7 465 rounds = 100000 466 467 def test(self): 468 469 data = ('abc', '123', ' ', '\xe4\xf6\xfc', '\xdf'*10) 470 len_data = len(data) 471 472 for i in xrange(self.rounds): 473 s = data[i % len_data] 474 475 s.isalnum() 476 s.isalpha() 477 s.isdigit() 478 s.islower() 479 s.isspace() 480 s.istitle() 481 s.isupper() 482 483 s.isalnum() 484 s.isalpha() 485 s.isdigit() 486 s.islower() 487 s.isspace() 488 s.istitle() 489 s.isupper() 490 491 s.isalnum() 492 s.isalpha() 493 s.isdigit() 494 s.islower() 495 s.isspace() 496 s.istitle() 497 s.isupper() 498 499 s.isalnum() 500 s.isalpha() 501 s.isdigit() 502 s.islower() 503 s.isspace() 504 s.istitle() 505 s.isupper() 506 507 s.isalnum() 508 s.isalpha() 509 s.isdigit() 510 s.islower() 511 s.isspace() 512 s.istitle() 513 s.isupper() 514 515 s.isalnum() 516 s.isalpha() 517 s.isdigit() 518 s.islower() 519 s.isspace() 520 s.istitle() 521 s.isupper() 522 523 s.isalnum() 524 s.isalpha() 525 s.isdigit() 526 s.islower() 527 s.isspace() 528 s.istitle() 529 s.isupper() 530 531 s.isalnum() 532 s.isalpha() 533 s.isdigit() 534 s.islower() 535 s.isspace() 536 s.istitle() 537 s.isupper() 538 539 s.isalnum() 540 s.isalpha() 541 s.isdigit() 542 s.islower() 543 s.isspace() 544 s.istitle() 545 s.isupper() 546 547 s.isalnum() 548 s.isalpha() 549 s.isdigit() 550 s.islower() 551 s.isspace() 552 s.istitle() 553 s.isupper() 554 555 def calibrate(self): 556 557 data = ('abc', '123', ' ', '\u1234\u2345\u3456', '\uFFFF'*10) 558 data = ('abc', '123', ' ', '\xe4\xf6\xfc', '\xdf'*10) 559 len_data = len(data) 560 561 for i in xrange(self.rounds): 562 s = data[i % len_data] 563