Live data from Hacker News

Sum of 1 to 1000000000 in different programming languages

stackoverflow.com

41–50 of 83 posts

Re: Sum of 1 to 1000000000 in different programming languages

#42
post #21

According to the time it took to my macbook air to calculate it in ruby, it would be pretty interesting to have someone generate benchmarks for different languages :)

sum(range(1, 100000001)) took ~3.25 seconds on my air under python3.

Re: Sum of 1 to 1000000000 in different programming languages

#44

Posted on stackoverflow, what an irony...

There's no stack overflow or even numeric overflow here. This is a floating point precision problem.

Ted Hopp thinks it is transformed to float in order to avoid an SO. Unless you know better, the irony is valid!

Re: Sum of 1 to 1000000000 in different programming languages

#46
SBCL: Commenter postfuturist said that this code:

(time (let ((sum 0)) (loop :for x :from 1 :to 1000000000 :do (incf sum x)) sum))

took about 3 seconds from his REPL with SBCL, with about 8.5 billion CPU cycles and 0 bytes consed.

Does anyone know why the same code on my version of SBCL (1.0.55.0-abb03f9) on a Mac took 156 billion cycles and consed 24 billion bytes?

Re: Sum of 1 to 1000000000 in different programming languages

#47
post #28
post #22

Knowing how to use a language is critical to get expected results. This gives the proper result in PHP by forcing the integer cast. $sum = (int) $sum + $i;

It's been a long time since I've worked with PHP, I assume $sum += (int) $i; will still convert to float once the size of $sum gets to the requisite size?

Yes. fooyc explained it correctly. ck2 did not.

Re: Sum of 1 to 1000000000 in different programming languages

#48
post #21

According to the time it took to my macbook air to calculate it in ruby, it would be pretty interesting to have someone generate benchmarks for different languages :)

sum(range(1, 100000001)) took ~3.25 seconds on my air under python3.

Your number appears to be a factor of 10 smaller than the "one billion plus one" of the original.

I get similar times on my Mac; about 3.2 seconds for 108 and 31.4 seconds for 109. Both are about 5 times faster than Python 2.7 for me.

Re: Sum of 1 to 1000000000 in different programming languages

#49

SBCL: Commenter postfuturist said that this code: (time (let ((sum 0)) (loop :for x :from 1 :to 1000000000 :do (incf sum x)) sum)) took about 3 seconds from his REPL with SBCL, with about 8.5 billion CPU cycles and 0 bytes consed. Does anyone know why the same code on my version of SBCL (1.0.55.0-abb03f9) on a Mac took 156 billion cycles and consed 24 billion bytes?

That's an old(ish) version. They've probably done a bit of optimization. For example, with the (speed 3) optimization, and letting the compiler know that sum is a fixnum, I can get it down to http://stackoverflow.com/a/18065714/2423072

Re: Sum of 1 to 1000000000 in different programming languages

#50

Someone replied the following in there: "The key in this case is using C99's long long data type. It provides the biggest primitive storage C can manage (128-bits on 32-bit machines and 256-bits on a 64-bit machine) and it runs really, really fast." Isn't long long "typically" 64-bit? (I know the C standard doesn't actually specify any actual size). What platform does this long long type really give you the full 128…

gcc supports '__int128' and 'unsigned __int128' on 64-bit systems. They're pretty limited as there's no libc support for them on any system I know of.
Post reply on HN