wasm-demo/demo/ermis-f/python_m/cur/0208

46 lines
1.1 KiB
Plaintext

From: jeremy at cnri.reston.va.us (Jeremy Hylton)
Date: Thu, 15 Apr 1999 15:25:37 -0400 (EDT)
Subject: for in benchmark interested
In-Reply-To: <37157CE7.EFB470CA@inka.de>
References: <37157CE7.EFB470CA@inka.de>
Message-ID: <14102.15523.573321.443195@bitdiddle.cnri.reston.va.us>
X-UID: 208
The Python version would be faster if you used sys.stdin.read instead
of sys.stdin.readlines. I'm not sure why you need to split the input
into lines before you split it into words; it seems like an
unnecessary step.
The version below is 25% faster on my machine than your fastest Python
version. (And I'm not even an expert Python optimizer :-).
import sys
import string
def run():
dict={}
dict_get = dict.get
read = sys.stdin.read
string_split = string.split
while 1:
buf = read(500000)
if buf:
for key in string_split(buf):
dict[key] = dict_get(key, 0) + 1
else:
return dict
dict = run()
write = sys.stdout.write
for word in dict.keys():
write("%4d\t%s\n" % (dict[word], word))
Jeremy