Skip to content

n ** m is slow #3818

@phluid61

Description

@phluid61

Environment

Expected Behavior

** should have comparable benchmark for Integers as *

Actual Behavior

Benchmarks show ** is an order of magnitude slower than * for Integers, and Integer ** Integer is slower than Float ** Integer

test.rb:

require 'benchmark'

$r = 1_000_000

Benchmark.bmbm do |x|
  x.report('1') { $r.times { 3 ** 3 }}
  x.report('2') { $r.times { 3.0 ** 3 }}
  x.report('3') { $r.times { 3 * 3 * 3 }}
end

MRI:

ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
Rehearsal -------------------------------------
1   0.060000   0.000000   0.060000 (  0.059568)
2   0.110000   0.000000   0.110000 (  0.105355)
3   0.050000   0.000000   0.050000 (  0.055801)
---------------------------- total: 0.220000sec

        user     system      total        real
1   0.070000   0.000000   0.070000 (  0.061776)
2   0.100000   0.000000   0.100000 (  0.104227)
3   0.060000   0.000000   0.060000 (  0.054408)

jruby:

jruby 9.0.5.0 (2.2.3) 2016-04-20 fffffff OpenJDK 64-Bit Server VM 24.95-b01 on 1.7.0_95-b00 +jit [linux-amd64]
Rehearsal -------------------------------------
1   0.520000   0.020000   0.540000 (  0.323665)
2   0.150000   0.000000   0.150000 (  0.115306)
3   0.050000   0.000000   0.050000 (  0.038388)
---------------------------- total: 0.740000sec

        user     system      total        real
1   0.210000   0.020000   0.230000 (  0.191466)
2   0.100000   0.000000   0.100000 (  0.094042)
3   0.020000   0.000000   0.020000 (  0.024104)

Originally reported here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions