Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ and Haskell codes differs in execution time on different machines

I want to ask you what can cause this difference. If I compile the following programs and run THE SAME BINARIES - on some platforms the one resulted from C++ code is much faster than the Haskell one, on other the situation is opposite.

Additional there is a big difference in the performance of final binaries according to which platform they were built on. (Each platform uses the same flags and the same versions of LVM and clang)

The codes are optimized and should work with simmilar performance - see: Can Haskell optimize function calls the same way Clang / GCC does?.

I want to ask you, how is it possible.

C++ code:

#include <cstdio>
#include <cstdlib>

int b(const int x){
    return x+5;
}

int c(const int x){
    return b(x)+1;
}

int d(const int x){
    return b(x)-1;
}

int a(const int x){
    return c(x) + d(x);
}

int main(int argc, char* argv[]){
    printf("Starting...\n");
    long int iternum = atol(argv[1]);
    long long int out = 0;
    for(long int i=1; i<=iternum;i++){
        out += a(iternum-i);
    }
    printf("%lld\n",out);
    printf("Done.\n");
}

compiled with clang++ -O3 main.cpp

haskell code:

module Main where
import qualified Data.Vector as V
import System.Environment
b :: Int -> Int
b x = x + 5
c x = b x + 1
d x = b x - 1
a x = c x + d x
main = do
   putStrLn "Starting..."
   args <- getArgs
   let iternum = read (head args) :: Int in do
      putStrLn $ show $ V.foldl' (+) 0 $ V.map (\i -> a (iternum-i))
         $ V.enumFromTo 1 iternum
      putStrLn "Done."

compiled with ghc -O3 --make -fforce-recomp -fllvm ghc-test.hs

RESULTS (testing THE SAME binaries on different platforms)

// binaries compiled on Ubuntu:
Ubuntu x64 @ Intel i7-3610QM CPU @ 2.30GHz : C++:0.775s, GHC:1.01s
Gentoo x64 @ Intel i7-Q720   CPU @ 1.6GHz  : C++:3.6s,   GHC:2.1s

// binaries compiled on Gentoo:
Ubuntu x64 @ Intel i7-3610QM CPU @ 2.30GHz : C++:0.782s, GHC:1.01s
Gentoo x64 @ Intel i7-Q720   CPU @ 1.6GHz  : C++:2.3s,   GHC:1.3s
like image 239
Wojciech Danilo Avatar asked Jul 01 '13 19:07

Wojciech Danilo


1 Answers

If I compile the following programs and run THE SAME BINARIES - on some platforms the one resulted from C++ code is much faster than the Haskell one, on other the situation is opposite.

Additional there is a big difference in the performance of final binaries according to which platform they were built on. (Each platform uses the same flags and the same versions of LVM and clang)

You are seeing the impact of the pesky operational details of real computers:

  • linker optimizations
  • different versions of dynamically loaded libraries
  • quality of assembly code generation for a given micro architecture
  • access to specialized instructions
  • cache sizes
  • operating system scheduler, allocator, ...
  • memory latencies

There's a massive amount of code and hardware that differ between the two platforms, that means you end up measuring different things.

There is no reason to expect the performance to be the same, or even in the same ratio. For micro-benchmarks it is not unusual to flip relative orderings when moving platforms.

like image 137
Don Stewart Avatar answered Oct 27 '22 01:10

Don Stewart