move sections
[python/dscho.git] / Tools / pybench / Unicode.py
blob153a91e66d7384ab76b5bb871df9790afaffe9c3
1 try:
2 unicode
3 except NameError:
4 raise ImportError
6 from pybench import Test
7 from string import join
9 class ConcatUnicode(Test):
11 version = 2.0
12 operations = 10 * 5
13 rounds = 60000
15 def test(self):
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))))
21 for i in xrange(self.rounds):
22 t + s
23 t + s
24 t + s
25 t + s
26 t + s
28 t + s
29 t + s
30 t + s
31 t + s
32 t + s
34 t + s
35 t + s
36 t + s
37 t + s
38 t + s
40 t + s
41 t + s
42 t + s
43 t + s
44 t + s
46 t + s
47 t + s
48 t + s
49 t + s
50 t + s
52 t + s
53 t + s
54 t + s
55 t + s
56 t + s
58 t + s
59 t + s
60 t + s
61 t + s
62 t + s
64 t + s
65 t + s
66 t + s
67 t + s
68 t + s
70 t + s
71 t + s
72 t + s
73 t + s
74 t + s
76 t + s
77 t + s
78 t + s
79 t + s
80 t + s
82 def calibrate(self):
84 s = unicode(join(map(str,range(100))))
85 t = unicode(join(map(str,range(1,101))))
87 for i in xrange(self.rounds):
88 pass
91 class CompareUnicode(Test):
93 version = 2.0
94 operations = 10 * 5
95 rounds = 150000
97 def test(self):
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")
103 for i in xrange(self.rounds):
104 t < s
105 t > s
106 t == s
107 t > s
108 t < s
110 t < s
111 t > s
112 t == s
113 t > s
114 t < s
116 t < s
117 t > s
118 t == s
119 t > s
120 t < s
122 t < s
123 t > s
124 t == s
125 t > s
126 t < s
128 t < s
129 t > s
130 t == s
131 t > s
132 t < s
134 t < s
135 t > s
136 t == s
137 t > s
138 t < s
140 t < s
141 t > s
142 t == s
143 t > s
144 t < s
146 t < s
147 t > s
148 t == s
149 t > s
150 t < s
152 t < s
153 t > s
154 t == s
155 t > s
156 t < s
158 t < s
159 t > s
160 t == s
161 t > s
162 t < s
164 def calibrate(self):
166 s = unicode(join(map(str,range(10))))
167 t = unicode(join(map(str,range(10))) + "abc")
169 for i in xrange(self.rounds):
170 pass
173 class CreateUnicodeWithConcat(Test):
175 version = 2.0
176 operations = 10 * 5
177 rounds = 80000
179 def test(self):
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'
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'
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'
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'
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'
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'
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'
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'
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'
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'
242 def calibrate(self):
244 for i in xrange(self.rounds):
245 pass
248 class UnicodeSlicing(Test):
250 version = 2.0
251 operations = 5 * 7
252 rounds = 140000
254 def test(self):
256 s = unicode(join(map(str,range(100))))
258 for i in xrange(self.rounds):
260 s[50:]
261 s[:25]
262 s[50:55]
263 s[-1:]
264 s[:1]
265 s[2:]
266 s[11:-11]
268 s[50:]
269 s[:25]
270 s[50:55]
271 s[-1:]
272 s[:1]
273 s[2:]
274 s[11:-11]
276 s[50:]
277 s[:25]
278 s[50:55]
279 s[-1:]
280 s[:1]
281 s[2:]
282 s[11:-11]
284 s[50:]
285 s[:25]
286 s[50:55]
287 s[-1:]
288 s[:1]
289 s[2:]
290 s[11:-11]
292 s[50:]
293 s[:25]
294 s[50:55]
295 s[-1:]
296 s[:1]
297 s[2:]
298 s[11:-11]
300 def calibrate(self):
302 s = unicode(join(map(str,range(100))))
304 for i in xrange(self.rounds):
305 pass
307 ### String methods
309 class UnicodeMappings(Test):
311 version = 2.0
312 operations = 3 * (5 + 4 + 2 + 1)
313 rounds = 10000
315 def test(self):
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)),'')
322 for i in xrange(self.rounds):
324 s.lower()
325 s.lower()
326 s.lower()
327 s.lower()
328 s.lower()
330 s.upper()
331 s.upper()
332 s.upper()
333 s.upper()
334 s.upper()
336 s.title()
337 s.title()
338 s.title()
339 s.title()
340 s.title()
342 t.lower()
343 t.lower()
344 t.lower()
345 t.lower()
347 t.upper()
348 t.upper()
349 t.upper()
350 t.upper()
352 t.title()
353 t.title()
354 t.title()
355 t.title()
357 u.lower()
358 u.lower()
360 u.upper()
361 u.upper()
363 u.title()
364 u.title()
366 v.lower()
368 v.upper()
370 v.title()
372 def calibrate(self):
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)),'')
379 for i in xrange(self.rounds):
380 pass
382 class UnicodePredicates(Test):
384 version = 2.0
385 operations = 5 * 9
386 rounds = 120000
388 def test(self):
390 data = (u'abc', u'123', u' ', u'\u1234\u2345\u3456', u'\uFFFF'*10)
391 len_data = len(data)
393 for i in xrange(self.rounds):
394 s = data[i % len_data]
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()
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()
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()
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()
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()
446 def calibrate(self):
448 data = (u'abc', u'123', u' ', u'\u1234\u2345\u3456', u'\uFFFF'*10)
449 len_data = len(data)
451 for i in xrange(self.rounds):
452 s = data[i % len_data]
454 try:
455 import unicodedata
456 except ImportError:
457 pass
458 else:
459 class UnicodeProperties(Test):
461 version = 2.0
462 operations = 5 * 8
463 rounds = 100000
465 def test(self):
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
478 for i in xrange(self.rounds):
480 c = data[i % len_data]
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)
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)
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)
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)
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)
527 def calibrate(self):
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
540 for i in xrange(self.rounds):
542 c = data[i % len_data]