Posted on stackoverflow, what an irony...
Sum of 1 to 1000000000 in different programming languages
41–50 of 83 posts
Re: Sum of 1 to 1000000000 in different programming languages
#42According 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 :)
Re: Sum of 1 to 1000000000 in different programming languages
#43(1L to 1000000000L).sum
Re: Sum of 1 to 1000000000 in different programming languages
#44Re: Sum of 1 to 1000000000 in different programming languages
#45Re: Sum of 1 to 1000000000 in different programming languages
#46(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
#47Knowing 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?
Re: Sum of 1 to 1000000000 in different programming languages
#48According 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.
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
#49SBCL: 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
#50Someone 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…