[Python-checkins] r46282 - sandbox/trunk/stringbench/stringbench.py

andrew.dalke python-checkins at python.org
Fri May 26 12:34:01 CEST 2006


Author: andrew.dalke
Date: Fri May 26 12:34:00 2006
New Revision: 46282

Modified:
   sandbox/trunk/stringbench/stringbench.py
Log:
Added benchmarks for split() and rsplit().  Includes human text for the huge case.

Fixed 2000 newline autogenerated text to be correct, and include more whitespace.

Handle case when no tests are run (had a divide by zero error)



Modified: sandbox/trunk/stringbench/stringbench.py
==============================================================================
--- sandbox/trunk/stringbench/stringbench.py	(original)
+++ sandbox/trunk/stringbench/stringbench.py	Fri May 26 12:34:00 2006
@@ -325,12 +325,66 @@
 
 #### split tests
 
+ at 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()
+
+ at 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()
+
+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, OS/2, 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.
+"""*50
+human_text_unicode = unicode(human_text)
+def _get_human_text(STR):
+    if STR is unicode:
+        return human_text_unicode
+    if STR is str:
+        return human_text
+    raise AssertionError
+
+ at bench('human_text.split()', "split whitespace (huge)", 100)
+def whitespace_split_huge(STR):
+    s = _get_human_text(STR)
+    s_split = s.split
+    for x in _RANGE_100:
+        s_split()
+
+ at bench('human_text.rsplit()', "split whitespace (huge)", 100)
+def whitespace_rsplit_huge(STR):
+    s = _get_human_text(STR)
+    s_rsplit = s.rsplit
+    for x in _RANGE_100:
+        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
     for x in _RANGE_1000:
         s_split("\n")
+
         
 @bench('"this\\nis\\na\\ntest\\n".rsplit("\\n")', "split newlines", 1000)
 def newlines_rsplit(STR):
@@ -351,13 +405,18 @@
 def _make_2000_lines():
     import random
     r = random.Random(100)
-    s = "".join(map(str, range(32, 128)))
+    chars = 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(0, 26)
+        start = r.randrange(96)
         n = r.randint(5, 65)
-        words.append(s[start:n])
+        words.append(s[start:start+n])
     return "\n".join(words)+"\n"
 
 _text_with_2000_lines = _make_2000_lines()
@@ -861,9 +920,12 @@
                 str_total += str_time
                 uni_total += uni_time
 
-    print "%.2f\t%.2f\t%.1f\t%s" % (
-        1000*str_total, 1000*uni_total, 100.*str_total/uni_total,
-        "TOTAL")
+    if str_total == uni_total == 0.0:
+        print "That was zippy!"
+    else:
+        print "%.2f\t%.2f\t%.1f\t%s" % (
+            1000*str_total, 1000*uni_total, 100.*str_total/uni_total,
+            "TOTAL")
 
 if __name__ == "__main__":
     main()


More information about the Python-checkins mailing list