GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cpython/Tools/stringbench/stringbench.py at 3.11 · python/cpython Expand file tree/ Copy pathstringbench.pyMore file actions 1482 lines (1264 loc) · 43 KB / Copy pathstringbench.pyFile metadata and controls1482 lines (1264 loc) · 43 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 # Various microbenchmarks comparing unicode and byte string performance # Please keep this file both 2.x and 3.x compatible! import timeit import itertools import operator import re import sys import datetime import optparse VERSION = '2.0' def p(*args): sys.stdout.write(' '.join(str(s) for s in args) + '\n') if sys.version_info >= (3,): BYTES = bytes_from_str = lambda x: x.encode('ascii') UNICODE = unicode_from_str = lambda x: x else: BYTES = bytes_from_str = lambda x: x UNICODE = unicode_from_str = lambda x: x.decode('ascii') class UnsupportedType(TypeError): pass p('stringbench v%s' % VERSION) p(sys.version) p(datetime.datetime.now()) REPEAT = 1 REPEAT = 3 #REPEAT = 7 if __name__ != "__main__": raise SystemExit("Must run as main program") parser = optparse.OptionParser() parser.add_option("-R", "--skip-re", dest="skip_re", action="store_true", help="skip regular expression tests") parser.add_option("-8", "--8-bit", dest="bytes_only", action="store_true", help="only do 8-bit string benchmarks") parser.add_option("-u", "--unicode", dest="unicode_only", action="store_true", help="only do Unicode string benchmarks") _RANGE_1000 = list(range(1000)) _RANGE_100 = list(range(100)) _RANGE_10 = list(range(10)) dups = {} def bench(s, group, repeat_count): def blah(f): if f.__name__ in dups: raise AssertionError("Multiple functions with same name: %r" % (f.__name__,)) dups[f.__name__] = 1 f.comment = s f.is_bench = True f.group = group f.repeat_count = repeat_count return f return blah def uses_re(f): f.uses_re = True ####### 'in' comparisons @bench('"A" in "A"*1000', "early match, single character", 1000) def in_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") for x in _RANGE_1000: s2 in s1 @bench('"B" in "A"*1000', "no match, single character", 1000) def in_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") for x in _RANGE_1000: s2 in s1 @bench('"AB" in "AB"*1000', "early match, two characters", 1000) def in_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") for x in _RANGE_1000: s2 in s1 @bench('"BC" in "AB"*1000', "no match, two characters", 1000) def in_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") for x in _RANGE_1000: s2 in s1 @bench('"BC" in ("AB"*300+"C")', "late match, two characters", 1000) def in_test_slow_match_two_characters(STR): s1 = STR("AB" * 300+"C") s2 = STR("BC") for x in _RANGE_1000: s2 in s1 @bench('s="ABC"*33; (s+"E") in ((s+"D")*300+s+"E")', "late match, 100 characters", 100) def in_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*300 + m+e s2 = m+e for x in _RANGE_100: s2 in s1 # Try with regex @uses_re @bench('s="ABC"*33; re.compile(s+"D").search((s+"D")*300+s+"E")', "late match, 100 characters", 100) def re_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*300 + m+e s2 = m+e pat = re.compile(s2) search = pat.search for x in _RANGE_100: search(s1) #### same tests as 'in' but use 'find' @bench('("A"*1000).find("A")', "early match, single character", 1000) def find_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("A"*1000).find("B")', "no match, single character", 1000) def find_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("AB"*1000).find("AB")', "early match, two characters", 1000) def find_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("AB"*1000).find("BC")', "no match, two characters", 1000) def find_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("AB"*1000).find("CA")', "no match, two characters", 1000) def find_test_no_match_two_character_bis(STR): s1 = STR("AB" * 1000) s2 = STR("CA") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("AB"*300+"C").find("BC")', "late match, two characters", 1000) def find_test_slow_match_two_characters(STR): s1 = STR("AB" * 300+"C") s2 = STR("BC") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('("AB"*300+"CA").find("CA")', "late match, two characters", 1000) def find_test_slow_match_two_characters_bis(STR): s1 = STR("AB" * 300+"CA") s2 = STR("CA") s1_find = s1.find for x in _RANGE_1000: s1_find(s2) @bench('s="ABC"*33; ((s+"D")*500+s+"E").find(s+"E")', "late match, 100 characters", 100) def find_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*500 + m+e s2 = m+e s1_find = s1.find for x in _RANGE_100: s1_find(s2) @bench('s="ABC"*33; ((s+"D")*500+"E"+s).find("E"+s)', "late match, 100 characters", 100) def find_test_slow_match_100_characters_bis(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*500 + e+m s2 = e+m s1_find = s1.find for x in _RANGE_100: s1_find(s2) #### Same tests for 'rfind' @bench('("A"*1000).rfind("A")', "early match, single character", 1000) def rfind_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("A"*1000).rfind("B")', "no match, single character", 1000) def rfind_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("AB"*1000).rfind("AB")', "early match, two characters", 1000) def rfind_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("AB"*1000).rfind("BC")', "no match, two characters", 1000) def rfind_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("AB"*1000).rfind("CA")', "no match, two characters", 1000) def rfind_test_no_match_two_character_bis(STR): s1 = STR("AB" * 1000) s2 = STR("CA") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("C"+"AB"*300).rfind("CA")', "late match, two characters", 1000) def rfind_test_slow_match_two_characters(STR): s1 = STR("C" + "AB" * 300) s2 = STR("CA") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('("BC"+"AB"*300).rfind("BC")', "late match, two characters", 1000) def rfind_test_slow_match_two_characters_bis(STR): s1 = STR("BC" + "AB" * 300) s2 = STR("BC") s1_rfind = s1.rfind for x in _RANGE_1000: s1_rfind(s2) @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rfind("E"+s)', "late match, 100 characters", 100) def rfind_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = e+m + (d+m)*500 s2 = e+m s1_rfind = s1.rfind for x in _RANGE_100: s1_rfind(s2) @bench('s="ABC"*33; (s+"E"+("D"+s)*500).rfind(s+"E")', "late match, 100 characters", 100) def rfind_test_slow_match_100_characters_bis(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = m+e + (d+m)*500 s2 = m+e s1_rfind = s1.rfind for x in _RANGE_100: s1_rfind(s2) #### Now with index. # Skip the ones which fail because that would include exception overhead. @bench('("A"*1000).index("A")', "early match, single character", 1000) def index_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_index = s1.index for x in _RANGE_1000: s1_index(s2) @bench('("AB"*1000).index("AB")', "early match, two characters", 1000) def index_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_index = s1.index for x in _RANGE_1000: s1_index(s2) @bench('("AB"*300+"C").index("BC")', "late match, two characters", 1000) def index_test_slow_match_two_characters(STR): s1 = STR("AB" * 300+"C") s2 = STR("BC") s1_index = s1.index for x in _RANGE_1000: s1_index(s2) @bench('s="ABC"*33; ((s+"D")*500+s+"E").index(s+"E")', "late match, 100 characters", 100) def index_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*500 + m+e s2 = m+e s1_index = s1.index for x in _RANGE_100: s1_index(s2) #### Same for rindex @bench('("A"*1000).rindex("A")', "early match, single character", 1000) def rindex_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_rindex = s1.rindex for x in _RANGE_1000: s1_rindex(s2) @bench('("AB"*1000).rindex("AB")', "early match, two characters", 1000) def rindex_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_rindex = s1.rindex for x in _RANGE_1000: s1_rindex(s2) @bench('("C"+"AB"*300).rindex("CA")', "late match, two characters", 1000) def rindex_test_slow_match_two_characters(STR): s1 = STR("C" + "AB" * 300) s2 = STR("CA") s1_rindex = s1.rindex for x in _RANGE_1000: s1_rindex(s2) @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rindex("E"+s)', "late match, 100 characters", 100) def rindex_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = e + m + (d+m)*500 s2 = e + m s1_rindex = s1.rindex for x in _RANGE_100: s1_rindex(s2) #### Same for partition @bench('("A"*1000).partition("A")', "early match, single character", 1000) def partition_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_partition = s1.partition for x in _RANGE_1000: s1_partition(s2) @bench('("A"*1000).partition("B")', "no match, single character", 1000) def partition_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_partition = s1.partition for x in _RANGE_1000: s1_partition(s2) @bench('("AB"*1000).partition("AB")', "early match, two characters", 1000) def partition_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_partition = s1.partition for x in _RANGE_1000: s1_partition(s2) @bench('("AB"*1000).partition("BC")', "no match, two characters", 1000) def partition_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_partition = s1.partition for x in _RANGE_1000: s1_partition(s2) @bench('("AB"*300+"C").partition("BC")', "late match, two characters", 1000) def partition_test_slow_match_two_characters(STR): s1 = STR("AB" * 300+"C") s2 = STR("BC") s1_partition = s1.partition for x in _RANGE_1000: s1_partition(s2) @bench('s="ABC"*33; ((s+"D")*500+s+"E").partition(s+"E")', "late match, 100 characters", 100) def partition_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*500 + m+e s2 = m+e s1_partition = s1.partition for x in _RANGE_100: s1_partition(s2) #### Same for rpartition @bench('("A"*1000).rpartition("A")', "early match, single character", 1000) def rpartition_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_rpartition = s1.rpartition for x in _RANGE_1000: s1_rpartition(s2) @bench('("A"*1000).rpartition("B")', "no match, single character", 1000) def rpartition_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_rpartition = s1.rpartition for x in _RANGE_1000: s1_rpartition(s2) @bench('("AB"*1000).rpartition("AB")', "early match, two characters", 1000) def rpartition_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_rpartition = s1.rpartition for x in _RANGE_1000: s1_rpartition(s2) @bench('("AB"*1000).rpartition("BC")', "no match, two characters", 1000) def rpartition_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_rpartition = s1.rpartition for x in _RANGE_1000: s1_rpartition(s2) @bench('("C"+"AB"*300).rpartition("CA")', "late match, two characters", 1000) def rpartition_test_slow_match_two_characters(STR): s1 = STR("C" + "AB" * 300) s2 = STR("CA") s1_rpartition = s1.rpartition for x in _RANGE_1000: s1_rpartition(s2) @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rpartition("E"+s)', "late match, 100 characters", 100) def rpartition_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = e + m + (d+m)*500 s2 = e + m s1_rpartition = s1.rpartition for x in _RANGE_100: s1_rpartition(s2) #### Same for split(s, 1) @bench('("A"*1000).split("A", 1)', "early match, single character", 1000) def split_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_split = s1.split for x in _RANGE_1000: s1_split(s2, 1) @bench('("A"*1000).split("B", 1)', "no match, single character", 1000) def split_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_split = s1.split for x in _RANGE_1000: s1_split(s2, 1) @bench('("AB"*1000).split("AB", 1)', "early match, two characters", 1000) def split_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_split = s1.split for x in _RANGE_1000: s1_split(s2, 1) @bench('("AB"*1000).split("BC", 1)', "no match, two characters", 1000) def split_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_split = s1.split for x in _RANGE_1000: s1_split(s2, 1) @bench('("AB"*300+"C").split("BC", 1)', "late match, two characters", 1000) def split_test_slow_match_two_characters(STR): s1 = STR("AB" * 300+"C") s2 = STR("BC") s1_split = s1.split for x in _RANGE_1000: s1_split(s2, 1) @bench('s="ABC"*33; ((s+"D")*500+s+"E").split(s+"E", 1)', "late match, 100 characters", 100) def split_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = (m+d)*500 + m+e s2 = m+e s1_split = s1.split for x in _RANGE_100: s1_split(s2, 1) #### Same for rsplit(s, 1) @bench('("A"*1000).rsplit("A", 1)', "early match, single character", 1000) def rsplit_test_quick_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("A") s1_rsplit = s1.rsplit for x in _RANGE_1000: s1_rsplit(s2, 1) @bench('("A"*1000).rsplit("B", 1)', "no match, single character", 1000) def rsplit_test_no_match_single_character(STR): s1 = STR("A" * 1000) s2 = STR("B") s1_rsplit = s1.rsplit for x in _RANGE_1000: s1_rsplit(s2, 1) @bench('("AB"*1000).rsplit("AB", 1)', "early match, two characters", 1000) def rsplit_test_quick_match_two_characters(STR): s1 = STR("AB" * 1000) s2 = STR("AB") s1_rsplit = s1.rsplit for x in _RANGE_1000: s1_rsplit(s2, 1) @bench('("AB"*1000).rsplit("BC", 1)', "no match, two characters", 1000) def rsplit_test_no_match_two_character(STR): s1 = STR("AB" * 1000) s2 = STR("BC") s1_rsplit = s1.rsplit for x in _RANGE_1000: s1_rsplit(s2, 1) @bench('("C"+"AB"*300).rsplit("CA", 1)', "late match, two characters", 1000) def rsplit_test_slow_match_two_characters(STR): s1 = STR("C" + "AB" * 300) s2 = STR("CA") s1_rsplit = s1.rsplit for x in _RANGE_1000: s1_rsplit(s2, 1) @bench('s="ABC"*33; ("E"+s+("D"+s)*500).rsplit("E"+s, 1)', "late match, 100 characters", 100) def rsplit_test_slow_match_100_characters(STR): m = STR("ABC"*33) d = STR("D") e = STR("E") s1 = e + m + (d+m)*500 s2 = e + m s1_rsplit = s1.rsplit for x in _RANGE_100: s1_rsplit(s2, 1) #### Benchmark the operator-based methods @bench('"A"*10', "repeat 1 character 10 times", 1000) def repeat_single_10_times(STR): s = STR("A") for x in _RANGE_1000: s * 10 @bench('"A"*1000', "repeat 1 character 1000 times", 1000) def repeat_single_1000_times(STR): s = STR("A") for x in _RANGE_1000: s * 1000 @bench('"ABCDE"*10', "repeat 5 characters 10 times", 1000) def repeat_5_10_times(STR): s = STR("ABCDE") for x in _RANGE_1000: s * 10 @bench('"ABCDE"*1000', "repeat 5 characters 1000 times", 1000) def repeat_5_1000_times(STR): s = STR("ABCDE") for x in _RANGE_1000: s * 1000 # + for concat @bench('"Andrew"+"Dalke"', "concat two strings", 1000) def concat_two_strings(STR): s1 = STR("Andrew") s2 = STR("Dalke") for x in _RANGE_1000: s1+s2 @bench('s1+s2+s3+s4+...+s20', "concat 20 strings of words length 4 to 15", 1000) def concat_many_strings(STR): s1=STR('TIXSGYNREDCVBHJ') s2=STR('PUMTLXBZVDO') s3=STR('FVZNJ') s4=STR('OGDXUW') s5=STR('WEIMRNCOYVGHKB') s6=STR('FCQTNMXPUZH') s7=STR('TICZJYRLBNVUEAK') s8=STR('REYB') s9=STR('PWUOQ') s10=STR('EQHCMKBS') s11=STR('AEVDFOH') s12=STR('IFHVD') s13=STR('JGTCNLXWOHQ') s14=STR('ITSKEPYLROZAWXF') s15=STR('THEK') s16=STR('GHPZFBUYCKMNJIT') s17=STR('JMUZ') s18=STR('WLZQMTB') s19=STR('KPADCBW') s20=STR('TNJHZQAGBU') for x in _RANGE_1000: (s1 + s2+ s3+ s4+ s5+ s6+ s7+ s8+ s9+s10+ s11+s12+s13+s14+s15+s16+s17+s18+s19+s20) #### Benchmark join def get_bytes_yielding_seq(STR, arg): if STR is BYTES and sys.version_info >= (3,): raise UnsupportedType return STR(arg) @bench('"A".join("")', "join empty string, with 1 character sep", 100) def join_empty_single(STR): sep = STR("A") s2 = get_bytes_yielding_seq(STR, "") sep_join = sep.join for x in _RANGE_100: sep_join(s2) @bench('"ABCDE".join("")', "join empty string, with 5 character sep", 100) def join_empty_5(STR): sep = STR("ABCDE") s2 = get_bytes_yielding_seq(STR, "") sep_join = sep.join for x in _RANGE_100: sep_join(s2) @bench('"A".join("ABC..Z")', "join string with 26 characters, with 1 character sep", 1000) def join_alphabet_single(STR): sep = STR("A") s2 = get_bytes_yielding_seq(STR, "ABCDEFGHIJKLMnOPQRSTUVWXYZ") sep_join = sep.join for x in _RANGE_1000: sep_join(s2) @bench('"ABCDE".join("ABC..Z")', "join string with 26 characters, with 5 character sep", 1000) def join_alphabet_5(STR): sep = STR("ABCDE") s2 = get_bytes_yielding_seq(STR, "ABCDEFGHIJKLMnOPQRSTUVWXYZ") sep_join = sep.join for x in _RANGE_1000: sep_join(s2) @bench('"A".join(list("ABC..Z"))', "join list of 26 characters, with 1 character sep", 1000) def join_alphabet_list_single(STR): sep = STR("A") s2 = [STR(x) for x in "ABCDEFGHIJKLMnOPQRSTUVWXYZ"] sep_join = sep.join for x in _RANGE_1000: sep_join(s2) @bench('"ABCDE".join(list("ABC..Z"))', "join list of 26 characters, with 5 character sep", 1000) def join_alphabet_list_five(STR): sep = STR("ABCDE") s2 = [STR(x) for x in "ABCDEFGHIJKLMnOPQRSTUVWXYZ"] sep_join = sep.join for x in _RANGE_1000: sep_join(s2) @bench('"A".join(["Bob"]*100)', "join list of 100 words, with 1 character sep", 1000) def join_100_words_single(STR): sep = STR("A") s2 = [STR("Bob")]*100 sep_join = sep.join for x in _RANGE_1000: sep_join(s2) @bench('"ABCDE".join(["Bob"]*100))', "join list of 100 words, with 5 character sep", 1000) def join_100_words_5(STR): sep = STR("ABCDE") s2 = [STR("Bob")]*100 sep_join = sep.join for x in _RANGE_1000: sep_join(s2) #### split tests @bench('("Here are some words. "*2).split()', "split whitespace (small)", 1000) def whitespace_split(STR): s = STR("Here are some words. "*2) s_split = s.split for x in _RANGE_1000: s_split() @bench('("Here are some words. "*2).rsplit()', "split whitespace (small)", 1000) def whitespace_rsplit(STR): s = STR("Here are some words. "*2) s_rsplit = s.rsplit for x in _RANGE_1000: s_rsplit() @bench('("Here are some words. "*2).split(None, 1)', "split 1 whitespace", 1000) def whitespace_split_1(STR): s = STR("Here are some words. "*2) s_split = s.split N = None for x in _RANGE_1000: s_split(N, 1) @bench('("Here are some words. "*2).rsplit(None, 1)', "split 1 whitespace", 1000) def whitespace_rsplit_1(STR): s = STR("Here are some words. "*2) s_rsplit = s.rsplit N = None for x in _RANGE_1000: s_rsplit(N, 1) @bench('("Here are some words. "*2).partition(" ")', "split 1 whitespace", 1000) def whitespace_partition(STR): sep = STR(" ") s = STR("Here are some words. "*2) s_partition = s.partition for x in _RANGE_1000: s_partition(sep) @bench('("Here are some words. "*2).rpartition(" ")', "split 1 whitespace", 1000) def whitespace_rpartition(STR): sep = STR(" ") s = STR("Here are some words. "*2) s_rpartition = s.rpartition for x in _RANGE_1000: s_rpartition(sep) human_text = """\ Python is a dynamic object-oriented programming language that can be used for many kinds of software development. It offers strong support for integration with other languages and tools, comes with extensive standard libraries, and can be learned in a few days. Many Python programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code. Python runs on Windows, Linux/Unix, Mac OS X, Amiga, Palm Handhelds, and Nokia mobile phones. Python has also been ported to the Java and .NET virtual machines. Python is distributed under an OSI-approved open source license that makes it free to use, even for commercial products. """*25 human_text_bytes = bytes_from_str(human_text) human_text_unicode = unicode_from_str(human_text) def _get_human_text(STR): if STR is UNICODE: return human_text_unicode if STR is BYTES: return human_text_bytes raise AssertionError @bench('human_text.split()', "split whitespace (huge)", 10) def whitespace_split_huge(STR): s = _get_human_text(STR) s_split = s.split for x in _RANGE_10: s_split() @bench('human_text.rsplit()', "split whitespace (huge)", 10) def whitespace_rsplit_huge(STR): s = _get_human_text(STR) s_rsplit = s.rsplit for x in _RANGE_10: s_rsplit() @bench('"this\\nis\\na\\ntest\\n".split("\\n")', "split newlines", 1000) def newlines_split(STR): s = STR("this\nis\na\ntest\n") s_split = s.split nl = STR("\n") for x in _RANGE_1000: s_split(nl) @bench('"this\\nis\\na\\ntest\\n".rsplit("\\n")', "split newlines", 1000) def newlines_rsplit(STR): s = STR("this\nis\na\ntest\n") s_rsplit = s.rsplit nl = STR("\n") for x in _RANGE_1000: s_rsplit(nl) @bench('"this\\nis\\na\\ntest\\n".splitlines()', "split newlines", 1000) def newlines_splitlines(STR): s = STR("this\nis\na\ntest\n") s_splitlines = s.splitlines for x in _RANGE_1000: s_splitlines() ## split text with 2000 newlines def _make_2000_lines(): import random r = random.Random(100) chars = list(map(chr, range(32, 128))) i = 0 while i < len(chars): chars[i] = " " i += r.randrange(9) s = "".join(chars) s = s*4 words = [] for i in range(2000): start = r.randrange(96) n = r.randint(5, 65) words.append(s[start:start+n]) return "\n".join(words)+"\n" _text_with_2000_lines = _make_2000_lines() _text_with_2000_lines_bytes = bytes_from_str(_text_with_2000_lines) _text_with_2000_lines_unicode = unicode_from_str(_text_with_2000_lines) def _get_2000_lines(STR): if STR is UNICODE: return _text_with_2000_lines_unicode if STR is BYTES: return _text_with_2000_lines_bytes raise AssertionError @bench('"...text...".split("\\n")', "split 2000 newlines", 10) def newlines_split_2000(STR): s = _get_2000_lines(STR) s_split = s.split nl = STR("\n") for x in _RANGE_10: s_split(nl) @bench('"...text...".rsplit("\\n")', "split 2000 newlines", 10) def newlines_rsplit_2000(STR): s = _get_2000_lines(STR) s_rsplit = s.rsplit nl = STR("\n") for x in _RANGE_10: s_rsplit(nl) @bench('"...text...".splitlines()', "split 2000 newlines", 10) def newlines_splitlines_2000(STR): s = _get_2000_lines(STR) s_splitlines = s.splitlines for x in _RANGE_10: s_splitlines() ## split text on "--" characters @bench( '"this--is--a--test--of--the--emergency--broadcast--system".split("--")', "split on multicharacter separator (small)", 1000) def split_multichar_sep_small(STR): s = STR("this--is--a--test--of--the--emergency--broadcast--system") s_split = s.split pat = STR("--") for x in _RANGE_1000: s_split(pat) @bench( '"this--is--a--test--of--the--emergency--broadcast--system".rsplit("--")', "split on multicharacter separator (small)", 1000) def rsplit_multichar_sep_small(STR): s = STR("this--is--a--test--of--the--emergency--broadcast--system") s_rsplit = s.rsplit pat = STR("--") for x in _RANGE_1000: s_rsplit(pat) ## split dna text on "ACTAT" characters @bench('dna.split("ACTAT")', "split on multicharacter separator (dna)", 10) def split_multichar_sep_dna(STR): s = _get_dna(STR) s_split = s.split pat = STR("ACTAT") for x in _RANGE_10: s_split(pat) @bench('dna.rsplit("ACTAT")', "split on multicharacter separator (dna)", 10) def rsplit_multichar_sep_dna(STR): s = _get_dna(STR) s_rsplit = s.rsplit pat = STR("ACTAT") for x in _RANGE_10: s_rsplit(pat) ## split with limits GFF3_example = "\t".join([ "I", "Genomic_canonical", "region", "357208", "396183", ".", "+", ".", "ID=Sequence:R119;note=Clone R119%3B Genbank AF063007;Name=R119"]) @bench('GFF3_example.split("\\t")', "tab split", 1000) def tab_split_no_limit(STR): sep = STR("\t") s = STR(GFF3_example) s_split = s.split for x in _RANGE_1000: s_split(sep) @bench('GFF3_example.split("\\t", 8)', "tab split", 1000) def tab_split_limit(STR): sep = STR("\t") s = STR(GFF3_example) s_split = s.split for x in _RANGE_1000: s_split(sep, 8) @bench('GFF3_example.rsplit("\\t")', "tab split", 1000) def tab_rsplit_no_limit(STR): sep = STR("\t") s = STR(GFF3_example) s_rsplit = s.rsplit for x in _RANGE_1000: s_rsplit(sep) @bench('GFF3_example.rsplit("\\t", 8)', "tab split", 1000) def tab_rsplit_limit(STR): sep = STR("\t") s = STR(GFF3_example) s_rsplit = s.rsplit for x in _RANGE_1000: s_rsplit(sep, 8) #### Count characters |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |