Bryan Sant wrote:
On 3/20/06, Nicholas Leippe <[EMAIL PROTECTED]> wrote:

Very interesting that C++ lost to a scripting language...
Way to go Python devs!


Agreed.  Shane's submission was brilliant.  I was blown away at how
fast python was at this task.  It scales well too.  The python code
ran very fast against small files and large files (like the one used
for this test).

Thanks, but the credit goes to the people who have optimized Python dictionaries so well. Sometimes I wish such optimized code were available in plain C/C++.

There's still more fine tuning possible. I've attached a version that shaves another second from the kjv100 test.

Shane
#!/usr/bin/python2.4

import sys

def main():
    words_fname = 'words.i'
    if len(sys.argv) > 1:
        source = open(sys.argv[1])
    else:
        source = sys.stdin

    words = {}
    for line in open(words_fname):
        words[line.rstrip()] = 0

    freq = {}
    for line in source:
        for word in line.split():
            if word in freq:
                freq[word] += 1
            elif word in words:
                freq[word] = 1

    for word, count in freq.iteritems():
        print word, count

main()
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/

Reply via email to