On Tue, 06 May 2008 08:28:15 +0000, Richard Heathfield
<rjh@[EMAIL PROTECTED]
> wrote:
>#include <stdio.h>
>#include <stdlib.h>
>
>int main(void)
>{
> char buf[129] = {0};
> long int sum = 0;
> while(fgets(buf, sizeof buf, stdin) != NULL)
> {
> sum += strtol(buf, NULL, 10);
> }
> printf("%ld\n", sum);
> return 0;
>}
Ok, so I compiled your version with Cygwin
g++ -pipe -Wall -O3 -fomit-frame-pointer sum.cpp -o sum.exe
and the input file was 14 MB; basically the following file made
several times larger
http://shootout.alioth.debian.org/debian/iofile.php?test=sumcol&lang=all&file=input
$ time sum.exe <sum.txt
11161 14831
real 0m1.269s
user 0m1.185s
sys 0m0.015s
this is not much faster than the original c++ version. What is
11161 14831? that's not the right answer.
Using the same file with java version I get
$ time java -server sumcol <sum.txt
2462944
real 0m0.742s
user 0m0.015s
sys 0m0.015s
the answer should be 2462944


|