BigInt performance

It’s seems that some benchmark’s results are wrong. LLVM is very good at optimizing and some operations are simply not executed because the output result is not used anywhere in the program. To avoid this, simply assign the result to a variable and puts its value at the end of the program.

require "benchmark"

res_a = Bool
res_b = Bool

Benchmark.ips do |x|
  n, r = 1213335u64, 5u64
  puts "for u64 value"

  # This operation will be skipped by LLVM
  x.report("        n % r") { n % r == 0 }
  # Because the result will be used later in the program, this operation will be executed. 
  x.report("res_a = n % r") {res_a = (n % r == 0) }
  # A dumb assignation to make sure that it doesn't slow down performance.
  x.report("res_b = false") {res_b = false }
end

puts res_a
puts res_b
for u64 value
        n % r 330.19M (  3.03ns) (±12.24%)  0.0B/op        fastest
res_a = n % r  99.61M ( 10.04ns) (± 8.41%)  0.0B/op   3.31× slower
res_b = false 271.73M (  3.68ns) (±11.53%)  0.0B/op   1.22× slower
true
false
1 Like