read-file speed
Posted: Sat Feb 04, 2006 8:03 am
I"m trying to create a newLISP version of a little Ruby script that I quite liked, with the same structure and at least equivalent speed:
The hash table/symbol stuff isn't tha problem, but I've found that the way I'm using parse to build the word hash is much slower, because I'm using regular expressions.
This is what I have so far:
This sort of works, but it's so much slower than the Ruby one that I'd like to know how to make it faster. Any clues or suggestions welcome!
Code: Select all
class Histogram
def initialize
@tally = Hash.new(0)
end
def analyze(s)
s.split.each do |word|
myword = word.downcase.gsub(/[^a-z0-9]/,"")
@tally[myword] = @tally[myword] + 1 if !myword.empty?
end
@tally.sort
end
end
analysis = Histogram.new.analyze(File.new(ARGV[0]).read)
print analysis.length
This is what I have so far:
Code: Select all
(context 'HASH)
(context 'MAIN)
(dolist (file-name (2 (main-args)))
(set 'buff (parse (read-file file-name) "\s" 8))
(dolist (the-word buff)
(set 'the-word (replace "[^a-z0-9]" (lower-case the-word) "" 0)))
(if (!= (context HASH (string "_" the-word)) nil)
(begin
(set 'occurrences (context HASH (string "_" the-word)))
(if (!= occurrences nil)
(set (context HASH (string "_" the-word)) (inc 'occurrences))))
(begin
(set (context HASH (string "_" the-word)) 1))))
(sort (symbols HASH))
(dolist (s (symbols HASH))
(push (list (name (sym s HASH)) (eval (sym s HASH))) word-list -1))
(println (length word-list))