I have been trying to find a computer language that runs no slower than 3 times C++. I thought that Haskell might be the solution, but, in my one test case it was about 6 times slower than C++. Can someone tell me if I am just coding this inefficiently. (Code and run times below.)
Are there any high level languages that have run times within a factor of three of C++ run times?
Cheers, Irchans
Timing for slow prime testing algorithm applied to 71378569:
Time Language 0.3 VB 6.0 Compiled 0.3 VC++ 6.0 0.4 Digital Mars C 0.7 Netbeans 6.9 Java 0.8 VB 6.0 (Interpreted strong typed) 2.5 GHC Haskell Compiled 3.7 QiII SBC 6 1992 Turbo C 7 Compiled PLT Scheme 7 VB 6.0 (Interpreted no type info) 7 Excel VBA (Iterp) 11 "Compiled" Mathematica 19 PLT Scheme 20 netbeans python 20 ruby 1.8.6 for Windows 25 QiII Clisp 117 Mathematica 185 GHC Haskell Interactive Mode
---------test code in Haskell---------------
module Main where
divQ :: Int -> Int -> Bool
divQ m n = ((mod m n) == 0)
primeQhelper :: Int -> Int -> Bool
primeQhelper n m = if ( m <= 1) then True else if (divQ n m) then False else primeQhelper n (m-1)
output2 = primeQhelper 71378569 35689285
main = print output2
-------C++ Code--------------------------- [C++]
#include <iostream>
bool primeQ(int i) { for (int j = i / 2; j > 1; j--) if (i % j == 0) return false; return true;
}
//run primeQ 10 times to get more accurate time int main() { int n = 100;
time_t t = time(NULL); for (int i = 0; i < n; i++) primeQ(71378569); printf("Time: %.3f", double(time(NULL) - t) / n);
> I have been trying to find a computer language that runs no slower > than 3 times C++. I thought that Haskell might be the solution, but, > in my one test case it was about 6 times slower than C++. Can someone > tell me if I am just coding this inefficiently. (Code and run times > below.)
I'm a bit puzzled here.
> [Haskell code]
This runs for me in under 2 seconds.
> [C++ code]
This runs for me in over 36 seconds.
How am I meant to adjust the code so that they are comparable in the way that concerns you? I assume that the code is at present simply just doing different things, I don't believe Haskell would be that much faster.
> > I have been trying to find a computer language that runs no slower > > than 3 times C++. I thought that Haskell might be the solution, but, > > in my one test case it was about 6 times slower than C++. Can someone > > tell me if I am just coding this inefficiently. (Code and run times > > below.)
> I'm a bit puzzled here.
> > [Haskell code]
> This runs for me in under 2 seconds.
> > [C++ code]
> This runs for me in over 36 seconds.
> How am I meant to adjust the code so that they are comparable in the way > that concerns you? I assume that the code is at present simply just > doing different things, I don't believe Haskell would be that much > faster.
Did you run the Haskell version 100 times as the C++ does?
irchans <infinitga...@yahoo.com> wrote: > I have been trying to find a computer language that runs no slower > than 3 times C++. I thought that Haskell might be the solution, but, > in my one test case it was about 6 times slower than C++. Can someone > tell me if I am just coding this inefficiently. (Code and run times > below.)
> Are there any high level languages that have run times within a > factor of three of C++ run times?
Which version of ghc are you using? My runtions for your code are 0.66s for the C++ version and 0.82s for the Haskell version respectively, using g++-4.4 and ghc 6.12.1
Here's a slightly more haskellish version of your code btw:
module Main where import System(getArgs)
main = do [p] <- getArgs let prime = (read p)::Int print $ isprime prime (prime `div` 2)
isprime :: Int -> Int -> Bool isprime n 1 = True isprime n m = case (mod m n == 0) of True -> False False -> isprime n (m-1)
Philip Armstrong <p...@kantaka.co.uk> wrote: > irchans <infinitga...@yahoo.com> wrote: >> I have been trying to find a computer language that runs no slower >> than 3 times C++. I thought that Haskell might be the solution, but, >> in my one test case it was about 6 times slower than C++. Can someone >> tell me if I am just coding this inefficiently. (Code and run times >> below.)
>> Are there any high level languages that have run times within a >> factor of three of C++ run times?
> Which version of ghc are you using? My runtions for your code are > 0.66s for the C++ version and 0.82s for the Haskell version
Philip Armstrong <p...@kantaka.co.uk> wrote: > irchans <infinitga...@yahoo.com> wrote: >> I have been trying to find a computer language that runs no slower >> than 3 times C++. I thought that Haskell might be the solution, but, >> in my one test case it was about 6 times slower than C++. Can someone >> tell me if I am just coding this inefficiently. (Code and run times >> below.)
>> Are there any high level languages that have run times within a >> factor of three of C++ run times?
> Which version of ghc are you using? My runtions for your code are > 0.66s for the C++ version and 0.82s for the Haskell version > respectively, using g++-4.4 and ghc 6.12.1
> Here's a slightly more haskellish version of your code btw:
> module Main where > import System(getArgs)
> main = do > [p] <- getArgs > let prime = (read p)::Int > print $ isprime prime (prime `div` 2)
> isprime :: Int -> Int -> Bool > isprime n 1 = True > isprime n m = case (mod m n == 0) of
^^^^^^^
s/mod m n/mod n m/ to match the original code. (Pointed out by Mark)
It doesn't make any difference to the runtime though.
> I have been trying to find a computer language that runs no slower > than 3 times C++. I thought that Haskell might be the solution, but, > in my one test case it was about 6 times slower than C++. Can someone > tell me if I am just coding this inefficiently. (Code and run times > below.)
> Are there any high level languages that have run times within a > factor of three of C++ run times?
I depends really how you program Haskell: the Haskell-Cafe mailing list has had threads on how to program nearly as fast as in C/C++ by finding special constructs, avoiding slow, high-level constructs. One might use the high-level constructs for rapid development, and then optimize certain parts.
> > > I have been trying to find a computer language that runs no slower > > > than 3 times C++. I thought that Haskell might be the solution, but, > > > in my one test case it was about 6 times slower than C++. Can someone > > > tell me if I am just coding this inefficiently. (Code and run times > > > below.)
> > I'm a bit puzzled here.
> > > [Haskell code]
> > This runs for me in under 2 seconds.
> > > [C++ code]
> > This runs for me in over 36 seconds.
> > How am I meant to adjust the code so that they are comparable in the way > > that concerns you? I assume that the code is at present simply just > > doing different things, I don't believe Haskell would be that much > > faster.
> Did you run the Haskell version 100 times as the C++ does?
> regards,
> laurent.
I ran the Haskell code 10 times, and it took about 25 seconds for an average of 2.5 seconds per run. For the C-code, I put in a for loop to make the same code run 10 times. It took about 3 seconds to run, which is about 0.3 seconds per run.
On Mar 11, 11:18 am, Philip Armstrong <p...@kantaka.co.uk> wrote:
> Which version of ghc are you using? My runtions for your code are > 0.66s for the C++ version and 0.82s for the Haskell version > respectively, using g++-4.4 and ghc 6.12.1
Hi Phil,
I used version GHC 6.10.1. I will try to find 6.12.1 and run it. Thanks!
> > > > I have been trying to find a computer language that runs no slower > > > > than 3 times C++. I thought that Haskell might be the solution, but, > > > > in my one test case it was about 6 times slower than C++. Can someone > > > > tell me if I am just coding this inefficiently. (Code and run times > > > > below.)
> > > I'm a bit puzzled here.
> > > > [Haskell code]
> > > This runs for me in under 2 seconds.
> > > > [C++ code]
> > > This runs for me in over 36 seconds.
> > > How am I meant to adjust the code so that they are comparable in the way > > > that concerns you? I assume that the code is at present simply just > > > doing different things, I don't believe Haskell would be that much > > > faster.
> > Did you run the Haskell version 100 times as the C++ does?
> > regards,
> > laurent.
> I ran the Haskell code 10 times, and it took about 25 seconds for an > average of 2.5 seconds per run. For the C-code, I put in a for loop > to make the same code run 10 times. It took about 3 seconds to run, > which is about 0.3 seconds per run.
Oops. Let me fix that:
I ran the Haskell code 10 times, and it took about 25 seconds for an average of 2.5 seconds per run. For the C-code, I put in a for loop to make the same code run 100 times. It took about 28 seconds to run, which is about 0.3 seconds per run.
Sorry about the confusion here.
Thank you all for your suggestions. I wrote my first Haskell program a couple of days ago, so I figured there must be a faster way to code this prime checking program.
irchans <infinitga...@yahoo.com> writes: > On Mar 11, 11:18 am, Philip Armstrong <p...@kantaka.co.uk> wrote: >> Which version of ghc are you using? My runtions for your code are >> 0.66s for the C++ version and 0.82s for the Haskell version >> respectively, using g++-4.4 and ghc 6.12.1
> I used version GHC 6.10.1. I will try to find 6.12.1 and run it. > Thanks!
Phil, also, what command-line options do you use to compile it? Just -O2 or something?
The -O2 option on the compile solved all my problems. After adding this flag to the compile command the Haskell code ran about 50% slower than C which works fine for me. (The Haskell code ran about 5 times faster after I put the -O2 in the compile command line.)
irchans <infinitga...@yahoo.com> writes: > On Mar 11, 3:42 pm, "Mark T. B. Carroll" >> Just -O2 or something?
> The -O2 option on the compile solved all my problems. After adding > this flag to the compile command the Haskell code ran about 50% slower > than C which works fine for me. (The Haskell code ran about 5 times > faster after I put the -O2 in the compile command line.)
Great! Also, you'll sometimes get a small gain by using,
module Main (main) where
so that the system knows it can safely specialize and inline and whatnot the functions in the module other than main, because nothing else will be importing them.
>> On Mar 11, 11:18 am, Philip Armstrong <p...@kantaka.co.uk> wrote: >>> Which version of ghc are you using? My runtions for your code are >>> 0.66s for the C++ version and 0.82s for the Haskell version >>> respectively, using g++-4.4 and ghc 6.12.1
>> I used version GHC 6.10.1. I will try to find 6.12.1 and run it. >> Thanks!
> Phil, also, what command-line options do you use to compile it? > Just -O2 or something?
Just -O2.
Exotic optimisation options don't make a lot of difference, at least not the ones I tried.
> I have been trying to find a computer language that runs no slower > than 3 times C++. I thought that Haskell might be the solution, but, > in my one test case it was about 6 times slower than C++. Can someone > tell me if I am just coding this inefficiently. (Code and run times > below.)
> Are there any high level languages that have run times within a > factor of three of C++ run times?
> Cheers, > Irchans
> Timing for slow prime testing algorithm applied to 71378569:
...
I can't comment on the Haskell issues, because I'm a Haskell beginner, but I do have some strong opinions on this style of benchmarking.
Testing performance on a single kernel, if done carefully, may tell you something about how fast that kernel runs. It is useful if, and only if, the workload whose performance you are trying to project is dominated by that kernel. For example, testing the performance of linear equation solution by matrix factorization may be useful, if your workload is dominated by solving large systems of linear equations.
It is very unlikely that any workload is dominated by slow prime testing. If that were the performance critical loop in some workload I would strongly recommend algorithm improvement before considering any other approach to performance improvement.
Doing the same computation several times in the same program run may be primarily a test of inter-procedure optimization. In particular, how can you be sure some compilers are not going to detect the fact that the prime number test is a pure function, and only run it once? Indeed, a compiler might manage to work out that, in the case of your C++ code, the prime number test is side effect free code whose result is never used, and scrap the whole loop.
> irchans wrote: > > Are there any high level languages that have run > > times within a > > factor of three of C++ run times?
> > Timing for slow prime testing algorithm applied to 71378569:
> [snip] > but I do have some strong opinions on this style of benchmarking.
> Testing performance on a single kernel, > if done carefully, may tell you > something about how fast that kernel runs. > It is useful if, and only if, > the workload whose performance you are trying > to project is dominated by > that kernel. For example, testing the > performance of linear equation > solution by matrix factorization may be > useful, if your workload is > dominated by solving large systems of > linear equations.
I guess I am just looking for a new language to learn that can 1) handle many problems with concise code, and 2) runs rather quickly. Although, I do have a project in mind that is dominated by searching through lists to find the number of occurrences of particular elements, I am really hoping to find a language that I can use for many projects in the future. (I am also hoping I can easily link into LAPACK for Singular Value Decomposition and Matrix Inversion.)
> It is very unlikely that any workload is > dominated by slow prime > testing.
> If that were the performance critical loop > in some workload I > would strongly recommend algorithm improvement > before considering any > other approach to performance improvement.
> Doing the same computation several times > in the same program run may be > primarily a test of inter-procedure > optimization. In particular, how can > you be sure some compilers are not going > to detect the fact that the > prime number test is a pure function, and > only run it once? Indeed, a > compiler might manage to work out that, > in the case of your C++ code, > the prime number test is side effect free > code whose result is never > used, and scrap the whole loop.
> Patricia
Hi Patricia,
As I said above, my main purpose is to find a fairly high level language that runs rather fast. Usually, I combine Mathematica with C+ +, but the interface is painful, so I wanted to try something else. I saw that Haskell did well in the computer language shootout (http:// shootout.alioth.debian.org/), so I decided to try it (and several other language) on a toy problem. I am under the impression that if a compiler is capable of producing code that runs as fast as C without too much programmer effort, then it will run rather fast on a simple toy problem.
I was unhappily surprised when the first GHC program for prime testing ran about 8 times slower than C on my system. From what I had read, I had expected it to run faster and that is why I posted here. Fortunately, the -O2 optimization made everything fast.
> In particular, how can > you be sure some compilers are not going to > detect the fact that the > prime number test is a pure function, and only > run it once?
Funny that you should mention that. I ran into exactly that problem, so I had to change the slow prime testing program to a new program that listed all of the primes between 71378569 and 71378669. That seemed to foil the compiler's attempt to trick me.
I do find that it can still be a bit of effort to get my Haskell code performing well, the lazy evaluation still sometimes surprises me. Of course, GHC's profiling helps a lot with that. Apart from that, Haskell is quite nice to code mathematical things in.
> I guess I am just looking for a new language to learn that can 1) > handle many problems with concise code, and 2) runs rather quickly. > Although, I do have a project in mind that is dominated by searching > through lists to find the number of occurrences of particular > elements, I am really hoping to find a language that I can use for > many projects in the future. (I am also hoping I can easily link into > LAPACK for Singular Value Decomposition and Matrix Inversion.)
...
In that case, you really need to look at kernels that involve memory access, especially linear algebra, as well as computation.
Incidentally, my dissertation research involved singular value decomposition. I was working in Java for most of the program, but transfered the data to a Matlab script for the linear algebra parts.
> I guess I am just looking for a new language to learn that can 1) > handle many problems with concise code, and 2) runs rather quickly. > Although, I do have a project in mind that is dominated by searching > through lists to find the number of occurrences of particular > elements, ...
Check if you can use a multi-map or hash-table, which stores equal elements on the same entry. Then time complexity is O(log n) resp. O(1). Haskell has a such similar types, but it is the data type, not language, that matters.
> ...I am really hoping to find a language that I can use for > many projects in the future. (I am also hoping I can easily link into > LAPACK for Singular Value Decomposition and Matrix Inversion.)
Haskell is a bit tricky to get working in imperative settings, due to its primary lazy evaluation model - imperative stuff is typically expressed through monads. Guile has a different evaluation model, and so works more easily with imperative constructs.
Haskell, though, has a good syntax. So I suggest trying out Haskell a bit just because of that. The interpreter Hugs is very convenient for trying that out.