[Haskell-cafe] Re: newbie optimization question
Daniel Fischer
daniel.is.fischer at web.de
Mon Oct 29 09:27:27 EDT 2007
Am Montag, 29. Oktober 2007 13:49 schrieb Dusan Kolar:
> Hello all,
>
> just to compare the stuff, I get quite other results being on other
> OS. Thus, the result of C++ compiler may not be that interesting as I do
> not have the one presented below.
Just to chime in, my results with the code below:
dafis at linux:~> uname -a
Linux linux 2.4.20-4GB-athlon #1 Mon Mar 17 17:56:47 UTC 2003 i686 unknown
unknown GNU/Linux
on a 1200 MHz Duron
g++ is version 3.3, C++ code compiled with -O3, Haskell with -O2 (GHC 6.6.1)
dafis at linux:~> time ./mainC
6 28 496 8128
real 0m1.945s
user 0m1.910s
sys 0m0.010s
dafis at linux:~> time ./mainInt
[6,28,496,8128]
real 0m2.407s
user 0m2.300s
sys 0m0.010s
dafis at linux:~> time ./mainInt64
[6,28,496,8128]
real 0m24.009s
user 0m23.900s
sys 0m0.050s
dafis at linux:~> time ./mainInteger
[6,28,496,8128]
real 0m21.555s
user 0m20.870s
sys 0m0.010s
So Int is not so much slower than C, Int64 and Integer dramatically slower
with Integer beating Int64 here, too.
Cheers,
Daniel
>
> My machine:
> Linux 2.6.23-ARCH #1 SMP PREEMPT Mon Oct 22 12:50:26 CEST 2007 x86_64
> Intel(R) Core(TM)2 CPU 6600 @ 2.40GHz GenuineIntel GNU/Linux
>
> Compilers:
> g++ --version
> g++ (GCC) 4.2.2
> Copyright (C) 2007 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
> ghc --version
> The Glorious Glasgow Haskell Compilation System, version 6.6.1
>
> Measurement:
> compiled with ghc -O2
> time ./mainInteger
> real 0m4.866s
> user 0m4.843s
> sys 0m0.020s
>
> compiled with ghc -O2
> time ./mainInt64
> real 0m2.213s
> user 0m2.210s
> sys 0m0.003s
>
> compiled with ghc -O2
> time ./mainInt
> real 0m1.149s
> user 0m1.143s
> sys 0m0.003s
>
> compiled with g++ -O3
> time ./mainC
> real 0m0.271s
> user 0m0.270s
> sys 0m0.000s
>
> I don't know what is the reason, but the difference between Int, Int64
> and Integer is not that dramatic as in example below, nevertheless, the
> difference between GHC and GNU C++ is very bad.... :-\
>
> Dusan
> > The code:
> >
> > % cat b.hs
> > module Main (divisors, perfect, main) where
> > import Data.Int
> >
> > divisors :: Int -> [Int]
> > divisors i = [j | j<-[1..i-1], i `rem` j == 0]
> >
> > perfect :: [Int]
> > perfect = [i | i<-[1..10000], i == sum (divisors i)]
> >
> > main = print perfect
> >
> > % cat b.cpp
> > #include <iostream>
> > using namespace std;
> >
> > int main() {
> > for (int i = 1; i <= 10000; i++) {
> > int sum = 0;
> > for (int j = 1; j < i; j++)
> > if (i % j == 0)
> > sum += j;
> > if (sum == i)
> > cout << i << " ";
> > }
> > return 0;
> > }
> >
> > %
More information about the Haskell-Cafe
mailing list