Recently me and my friends were benchmarking different programming languages. As I'm learning Haskell lately, I wanted to show that a functional language will perform nearly as good as C with simpler code. But the code I pasted below, compiled with GHC's -O3 option, was executing about 1.6 s on my machine. Equivalent scripts in Python and Ruby executed faster (it was a simple for loop).
import System.IO
saveLine fh x = hPutStrLn fh $ show x ++ "\t" ++ show (x^2)
main = do
fh <- openFile "haskell.txt" WriteMode
mapM (saveLine fh) [1..999999]
hClose fh
You can check out codes in other languages here (I wrote only the ones in Python and Ruby).
The question is - how to make it run faster?