Skip to content

Make in_use? 200x faster #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

aasmith
Copy link

@aasmith aasmith commented Mar 2, 2011

It appears that the current implementation of BenString.in_use? is slow, per the following benchmark results:

[andy@hermit ~/ben_string]$ ruby -Ilib perf.rb 
Rehearsal --------------------------------------------------------
original each_object   0.453125  44.117188  44.570312 ( 45.130141)
proposed each_object   0.000000   0.109375   0.109375 (  0.112148)
---------------------------------------------- total: 44.679688sec

                           user     system      total        real
original each_object   0.078125  20.171875  20.250000 ( 20.553962)
proposed each_object   0.000000   0.101562   0.101562 (  0.102011)

The code for the benchmark is:

require 'benchmark'
require 'ben_string'

# Instantiate some objects.
100.times { [String, Array, Hash, BenString].each { |c| c.new } }

N = 1000

Benchmark.bmbm do |bm|
  bm.report 'original each_object' do
    N.times do
      counts = Hash.new(0)
      ObjectSpace.each_object { |o| counts[o.class] += 1 }
    end
  end

  bm.report 'proposed each_object' do
    N.times do
      ObjectSpace.each_object(BenString).any? { |o| BenString === o }
    end
  end
end

Proposed patch is in this pull request.

@mculp
Copy link

mculp commented Mar 2, 2011

whoooosh

@zerowidth
Copy link
Contributor

+1, good work man!

@bleything
Copy link
Owner

I'm not sure what you're getting at, mculp :)

@dkubb
Copy link

dkubb commented Oct 11, 2012

Wouldn't ObjectSpace.each_object(self).any? be even faster than this? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants