It cost too much time to run a crystal file
I write a crystal file to compare with other languages in finding primes
It show that it only costed 04.03ms, but I wait several seconds to see the ouput.
[~] crystal run --release Programming/Benchmarks/prime.cr 20:12:44
primes 9.61 (104.03ms) (卤 2.92%) 66292569 B/op fastest
Crystal is a compiled language, compile once, then run this binary how many times you want. That's what crystal run does.
Crystal is a compiled language, compile once, then run this binary how many times you want. That's what
crystal rundoes.
Yes, I know, but It is slower then I thought. It spent several seconds to show the report, though it said that the process only costed 104.04ms.
[~] crystal build --release Programming/Benchmarks/prime.cr
[~] ./prime
primes 9.61 (104.04ms) (卤 5.91%) 66292494 B/op fastest
This looks like the benchmark/ips output. The 104ms is the average runtime, the library runs it in a loop until it gets a somewhat stable value, including a couple of times before even measuring to warm up any caches.
To be exact, Benchmark#ips does 2 seconds warmup + 5 seconds calculation by default.
It would also help if you provide some code that we can run, and what times you expect.
It would also help if you provide some code that we can run, and what times you expect.
@asterite Thank you. I would be pleased that you can review https://github.com/Freakwill/benchmarks
@Freakwill Replace the last 3 lines of primes with this (translated from the Go code):
p = [2]
i = 0
while i < (n - 1) / 2
x = 3 + 2 * i
p << x if f[x] == 1
i += 1
end
p
In my case it goes from 113.37ms per call to 98.22ms per call.
But yeah, you get a slow execution because Benchmark.ips will run your code many times to benchmark it, as others said. If you don't want to do that, just do:
n = 10000000
time = Time.measure do
primes(n)
end
puts time
(instead of using benchmark)
Most helpful comment
This looks like the
benchmark/ipsoutput. The 104ms is the average runtime, the library runs it in a loop until it gets a somewhat stable value, including a couple of times before even measuring to warm up any caches.