Skip to content

Instantly share code, notes, and snippets.

@dmjio
Last active December 20, 2015 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmjio/6166076 to your computer and use it in GitHub Desktop.
Save dmjio/6166076 to your computer and use it in GitHub Desktop.
HaskellVsC

Haskell vs. C perf test

Summing 1 billion consecutive integers

http://research.microsoft.com/en-us/um/people/simonpj/papers/ndp/haskell-beats-C.pdf

C

#include <stdio.h> 
int main(int argc, char *argv[])
 {
  long a = 1000000000, b=0, i=0;
  for (i; i <= a; ++i) b += i;
  printf ("%lu\n",b);
  return 0;
 }

Haskell

import qualified Data.Vector as V
main = print (V.foldl' (+) 0 (V.enumFromTo 1 1000000000) :: Int)
-- See stream fusion article above

Compilation options

gcc -O2 cbil.c -o cbil
ghc -O2 -fllvm hbil.hs
time ./cbil && time ./hbil

Specs:

MacBook Pro Retina, 8 core, 2.3Ghz Intel i7

Results

C Performance...

answer: 500000000500000000

real  0m0.003s
user  0m0.001s
sys   0m0.001s

Haskell Performance...

answer: 500000000500000000

real  0m0.003s
user  0m0.001s
sys	  0m0.001s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment