Haskell vs Python vs Ruby(ファイルの中身の文字数、ワード数、行数を表示するコマンド)
ファイルの中身の文字数、ワード数、行数を表示するコマンド(他の言語の投稿求む) - しんちゃんの日記のコードをPython/Rubyでも作り、ベンチマーク取りました。
読み込ませたファイルは、スーパーπで生成できる一番大きなπ。3355万桁を収めたpi.datがテキストデータだったので、それを今回は食わせてみました。
まずは、各言語のコードを見てみましょう。
import System.Environment main = getArgs >>= \fnames -> mapM readFile fnames >>= mapM_ (\(x,y) -> putStrLn $ concat [x, "\nchars = ", show.length.unwords $ words y, " words = ", show.length $ words y, " lines = ", show.length $ lines y, "\n"]).zip fnames
import sys for arg in sys.argv[1:]: print(str(arg)) content = open(arg).read() words = content.split() chars = "" for word in words: chars = chars + word + " " lines = content.split('\n') print("chars = ",str(len(chars)-1)," words = ",str(len(words))," lines = ",str(len(lines))) print()
Ruby
>|ruby|
ARGV.each do |arg|
puts arg
content = open(arg).read
words = content.split()
chars = ""
words.each {|word| chars = chars + word + " " }
lines = content.split("\n")
print "chars = ", chars.length - 1 , " words = ", words.size ," length = ", lines.size
puts "\n" * 2
end
|