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