summaryrefslogtreecommitdiffstats
path: root/Demo/comparisons
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1995-04-10 11:40:26 (GMT)
committerGuido van Rossum <guido@python.org>1995-04-10 11:40:26 (GMT)
commit21bc15b73da1e3e5ab60ff9b93a95bfd390fea3f (patch)
tree6c138d03577d4d75c87e7ddb665dbc535fcf06ad /Demo/comparisons
parentf4449def3b0e4d7d2e197920cb7f9db4f2e9df04 (diff)
downloadcpython-21bc15b73da1e3e5ab60ff9b93a95bfd390fea3f.zip
cpython-21bc15b73da1e3e5ab60ff9b93a95bfd390fea3f.tar.gz
cpython-21bc15b73da1e3e5ab60ff9b93a95bfd390fea3f.tar.bz2
commit -- why not...
Diffstat (limited to 'Demo/comparisons')
-rw-r--r--Demo/comparisons/README60
-rwxr-xr-xDemo/comparisons/patterns4
-rwxr-xr-xDemo/comparisons/regextest.py50
-rwxr-xr-xDemo/comparisons/sortingtest.py50
-rwxr-xr-xDemo/comparisons/systemtest.py74
5 files changed, 238 insertions, 0 deletions
diff --git a/Demo/comparisons/README b/Demo/comparisons/README
new file mode 100644
index 0000000..111667c
--- /dev/null
+++ b/Demo/comparisons/README
@@ -0,0 +1,60 @@
+Subject: Re: What language would you use?
+From: Tom Christiansen <tchrist@mox.perl.com>
+Date: 6 Nov 1994 15:14:51 GMT
+Newsgroups: comp.lang.python,comp.lang.tcl,comp.lang.scheme,comp.lang.misc,comp.lang.perl
+Message-Id: <39irtb$3t4@csnews.cs.Colorado.EDU>
+References: <39b7ha$j9v@zeno.nscf.org> <39hhjp$lgn@csnews.cs.Colorado.EDU> <39hvsu$dus@mathserv.mps.ohio-state.edu>
+
+[...]
+If you're really into benchmarks, I'd love it if someone were to code up
+the following problems in tcl, python, and scheme (and whatever else you'd
+like). Separate versions (one optimized for speed, one for beauty :-) are
+ok. Post your code so we can time it on our own systems.
+
+0) Factorial Test (numerics and function calls)
+
+ (we did this already)
+
+1) Regular Expressions Test
+
+ Read a file of (extended per egrep) regular expressions (one per line),
+ and apply those to all files whose names are listed on the command line.
+ Basically, an 'egrep -f' simulator. Test it with 20 "vt100" patterns
+ against a five /etc/termcap files. Tests using more elaborate patters
+ would also be interesting. Your code should not break if given hundreds
+ of regular expressions or binary files to scan.
+
+2) Sorting Test
+
+ Sort an input file that consists of lines like this
+
+ var1=23 other=14 ditto=23 fred=2
+
+ such that each output line is sorted WRT to the number. Order
+ of output lines does not change. Resolve collisions using the
+ variable name. e.g.
+
+ fred=2 other=14 ditto=23 var1=23
+
+ Lines may be up to several kilobytes in length and contain
+ zillions of variables.
+
+3) System Test
+
+ Given a list of directories, report any bogus symbolic links contained
+ anywhere in those subtrees. A bogus symbolic link is one that cannot
+ be resolved because it points to a nonexistent or otherwise
+ unresolvable file. Do *not* use an external find executable.
+ Directories may be very very deep. Print a warning immediately if the
+ system you're running on doesn't support symbolic links.
+
+
+I'll post perl solutions if people post the others.
+
+
+--tom
+--
+Tom Christiansen Perl Consultant, Gamer, Hiker tchrist@mox.perl.com
+
+ "But Billy! A *small* allowance prepares you for a lifetime of small
+ salaries and for your Social Security payments." --Family Circus
diff --git a/Demo/comparisons/patterns b/Demo/comparisons/patterns
new file mode 100755
index 0000000..f4da846
--- /dev/null
+++ b/Demo/comparisons/patterns
@@ -0,0 +1,4 @@
+^def
+^class
+^import
+^from
diff --git a/Demo/comparisons/regextest.py b/Demo/comparisons/regextest.py
new file mode 100755
index 0000000..35ee9ee
--- /dev/null
+++ b/Demo/comparisons/regextest.py
@@ -0,0 +1,50 @@
+#! /usr/local/bin/python
+
+# 1) Regular Expressions Test
+#
+# Read a file of (extended per egrep) regular expressions (one per line),
+# and apply those to all files whose names are listed on the command line.
+# Basically, an 'egrep -f' simulator. Test it with 20 "vt100" patterns
+# against a five /etc/termcap files. Tests using more elaborate patters
+# would also be interesting. Your code should not break if given hundreds
+# of regular expressions or binary files to scan.
+
+# This implementation:
+# - combines all patterns into a single one using ( ... | ... | ... )
+# - reads patterns from stdin, scans files given as command line arguments
+# - produces output in the format <file>:<lineno>:<line>
+# - is only about 2.5 times as slow as egrep (though I couldn't run
+# Tom's test -- this system, a vanilla SGI, only has /etc/terminfo)
+
+import string
+import sys
+import regex
+from regex_syntax import *
+
+regex.set_syntax(RE_SYNTAX_EGREP)
+
+def main():
+ pats = map(chomp, sys.stdin.readlines())
+ bigpat = '(' + string.joinfields(pats, '|') + ')'
+ prog = regex.compile(bigpat)
+
+ for file in sys.argv[1:]:
+ try:
+ fp = open(file, 'r')
+ except IOError, msg:
+ print "%s: %s" % (file, msg)
+ continue
+ lineno = 0
+ while 1:
+ line = fp.readline()
+ if not line:
+ break
+ lineno = lineno + 1
+ if prog.search(line) >= 0:
+ print "%s:%s:%s" % (file, lineno, line),
+
+def chomp(s):
+ if s[-1:] == '\n': return s[:-1]
+ else: return s
+
+main()
diff --git a/Demo/comparisons/sortingtest.py b/Demo/comparisons/sortingtest.py
new file mode 100755
index 0000000..1de683b
--- /dev/null
+++ b/Demo/comparisons/sortingtest.py
@@ -0,0 +1,50 @@
+#! /usr/local/bin/python
+
+# 2) Sorting Test
+#
+# Sort an input file that consists of lines like this
+#
+# var1=23 other=14 ditto=23 fred=2
+#
+# such that each output line is sorted WRT to the number. Order
+# of output lines does not change. Resolve collisions using the
+# variable name. e.g.
+#
+# fred=2 other=14 ditto=23 var1=23
+#
+# Lines may be up to several kilobytes in length and contain
+# zillions of variables.
+
+# This implementation:
+# - Reads stdin, writes stdout
+# - Uses any amount of whitespace to separate fields
+# - Allows signed numbers
+# - Treats illegally formatted fields as field=0
+# - Outputs the sorted fields with exactly one space between them
+# - Handles blank input lines correctly
+
+import regex
+import string
+import sys
+
+def main():
+ prog = regex.compile('^\(.*\)=\([-+]?[0-9]+\)')
+ def makekey(item, prog=prog):
+ if prog.match(item) >= 0:
+ var, num = prog.group(1, 2)
+ return string.atoi(num), var
+ else:
+ # Bad input -- pretend it's a var with value 0
+ return 0, item
+ while 1:
+ line = sys.stdin.readline()
+ if not line:
+ break
+ items = string.split(line)
+ items = map(makekey, items)
+ items.sort()
+ for num, var in items:
+ print "%s=%s" % (var, num),
+ print
+
+main()
diff --git a/Demo/comparisons/systemtest.py b/Demo/comparisons/systemtest.py
new file mode 100755
index 0000000..26d9f54
--- /dev/null
+++ b/Demo/comparisons/systemtest.py
@@ -0,0 +1,74 @@
+#! /usr/local/bin/python
+
+# 3) System Test
+#
+# Given a list of directories, report any bogus symbolic links contained
+# anywhere in those subtrees. A bogus symbolic link is one that cannot
+# be resolved because it points to a nonexistent or otherwise
+# unresolvable file. Do *not* use an external find executable.
+# Directories may be very very deep. Print a warning immediately if the
+# system you're running on doesn't support symbolic links.
+
+# This implementation:
+# - takes one optional argument, using the current directory as default
+# - uses chdir to increase performance
+# - sorts the names per directory
+# - prints output lines of the form "path1 -> path2" as it goes
+# - prints error messages about directories it can't list or chdir into
+
+import os
+import sys
+from stat import *
+
+def main():
+ try:
+ # Note: can't test for presence of lstat -- it's always there
+ dummy = os.readlink
+ except AttributeError:
+ print "This system doesn't have symbolic links"
+ sys.exit(0)
+ if sys.argv[1:]:
+ prefix = sys.argv[1]
+ else:
+ prefix = ''
+ if prefix:
+ os.chdir(prefix)
+ if prefix[-1:] != '/': prefix = prefix + '/'
+ reportboguslinks(prefix)
+ else:
+ reportboguslinks('')
+
+def reportboguslinks(prefix):
+ try:
+ names = os.listdir('.')
+ except os.error, msg:
+ print "%s%s: can't list: %s" % (prefix, '.', msg)
+ return
+ names.sort()
+ for name in names:
+ if name == os.curdir or name == os.pardir:
+ continue
+ try:
+ mode = os.lstat(name)[ST_MODE]
+ except os.error:
+ print "%s%s: can't stat: %s" % (prefix, name, msg)
+ continue
+ if S_ISLNK(mode):
+ try:
+ os.stat(name)
+ except os.error:
+ print "%s%s -> %s" % \
+ (prefix, name, os.readlink(name))
+ elif S_ISDIR(mode):
+ try:
+ os.chdir(name)
+ except os.error, msg:
+ print "%s%s: can't chdir: %s" % \
+ (prefix, name, msg)
+ continue
+ try:
+ reportboguslinks(prefix + name + '/')
+ finally:
+ os.chdir('..')
+
+main()