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