Is Haskell fast?

#include <stdio.h> int main(int argc, char **argv) { double i, s; s = 0; for (i = 1; i < 100000000; i++) s += 1/i; printf("Almost infinity is %g

", s); }

Lennarts-Computer% gcc -O3 inf.c -o inf Lennarts-Computer% time ./inf Almost infinity is 18.9979 1.585u 0.009s 0:01.62 97.5% 0+0k 0+0io 0pf+0w

import BASIC main = runBASIC' $ do 10 LET I =: 1 20 LET S =: 0 30 LET S =: S + 1/I 40 LET I =: I + 1 50 IF I <> 100000000 THEN 30 60 PRINT "Almost infinity is" 70 PRINT S 80 END

Lennarts-Computer% ghc --make Main.hs [4 of 4] Compiling Main ( Main.hs, Main.o ) Linking Main ... Lennarts-Computer% ./Main Almost infinity is 18.9979 CPU time: 1.57s

LBB1_1: ## _L4 movsd LCPI1_0, %xmm2 movapd %xmm1, %xmm3 addsd %xmm2, %xmm3 ucomisd LCPI1_1, %xmm3 divsd %xmm1, %xmm2 addsd %xmm2, %xmm0 movapd %xmm3, %xmm1 jne LBB1_1 ## _L4

Let's do a simple benchmark comparing Haskell to C. My benchmark computes an approximation to infinity by adding up 1/n. Here is the C code:And running itAnd now the Haskell code:And running it:As you can see it's about the same time. In fact the assembly code for the loops look pretty much the same. Here's the Haskell one:

Labels: BASIC, Benchmark, DSL, Haskell, LLVM