Skip to content

Instantly share code, notes, and snippets.

@kiennt
Last active December 13, 2015 20:08
Show Gist options
  • Save kiennt/4967466 to your computer and use it in GitHub Desktop.
Save kiennt/4967466 to your computer and use it in GitHub Desktop.
Pre define method for array sort
require 'benchmark'
Benchmark.bm do |x|
names = ["matz", "rossum", "ryal", "ritchie", "brendan"]
n = 50000
x.report "method missing" do
class Array
def method_missing name, *argv
super unless name =~ /^sort_by_(\w+)_(asc|desc)$/
condition = ($2 == "asc" ? "{ |a, b| a.%s <=> b.%s }" : "{ |a, b| b.%s <=> a.%s }") % [$1, $1]
instance_eval "sort #{condition}"
end
end
n.times { names.sort_by_length_asc }
end
x.report "predefine method" do
class Array
def method_missing name, *argv
super unless name =~ /^sort_by_(\w+)_(asc|desc)$/
condition = ($2 == "asc" ? "{ |a, b| a.%s <=> b.%s }" : "{ |a, b| b.%s <=> a.%s }") % [$1, $1]
Array.class_eval <<-METHOD
def #{name}
sort #{condition}
end
METHOD
instance_eval "#{name}"
end
end
n.times { names.sort_by_length_asc }
end
end
@huydx
Copy link

huydx commented Feb 16, 2013

Thread ID: 2151954760
Total: 0.148856
Sort by: self_time

 %self      total      self      wait     child     calls  name
 56.63      0.120     0.084     0.000     0.035     5000   Array#sort 
 23.79      0.035     0.035     0.000     0.000    30000   Fixnum#<=> 
 11.44      0.137     0.017     0.000     0.120     5000   Array#sort_by_length_asc 
  7.99      0.149     0.012     0.000     0.137        1   Integer#times 
  0.05      0.149     0.000     0.000     0.149        1   Global#[No method] 
  0.03      0.000     0.000     0.000     0.000        1   Module#class_eval 
  0.02      0.000     0.000     0.000     0.000        1   Array#method_missing 
  0.01      0.000     0.000     0.000     0.000        1   BasicObject#instance_eval 
  0.01      0.000     0.000     0.000     0.000        1   String#% 
  0.01      0.000     0.000     0.000     0.000        1   Symbol#=~ 
  0.01      0.000     0.000     0.000     0.000        2   Symbol#to_s 
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added 

* indicates recursively called methods
Thread ID: 2151954760
Total: 0.438752
Sort by: self_time

 %self      total      self      wait     child     calls  name
 37.86      0.310     0.166     0.000     0.144     5000   BasicObject#instance_eval 
 24.72      0.144     0.108     0.000     0.035     5000   Array#sort 
 14.90      0.423     0.065     0.000     0.358     5000   Array#method_missing 
  8.08      0.035     0.035     0.000     0.000    30000   Fixnum#<=> 
  6.98      0.031     0.031     0.000     0.000     5000   Symbol#=~ 
  3.86      0.017     0.017     0.000     0.000     5000   String#% 
  3.60      0.439     0.016     0.000     0.423        1   Integer#times 
  0.01      0.439     0.000     0.000     0.439        1   Global#[No method] 

* indicates recursively called methods

@huydx
Copy link

huydx commented Feb 16, 2013

require 'benchmark'
require 'ruby-prof' 

names = ["matz", "rossum", "ryal", "ritchie", "brendan"]
n = 5000 
class Array
  def method_missing name, *argv
    super unless name =~ /^sort_by_(¥w+)_(asc|desc)$/
    condition = ($2 == "asc" ? "{ |a, b| a.%s <=> b.%s }" : "{ |a, b| b.%s <=> a.%s }") % [$1, $1]

    Array.class_eval <<-METHOD
      def #{name}
        sort #{condition}
      end
    METHOD

    instance_eval "#{name}"
  end
end

RubyProf.start
n.times { names.sort_by_length_asc }
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)

Array.send :remove_method, :sort_by_length_asc

class Array
  def method_missing name, *argv
    super unless name =~ /^sort_by_(¥w+)_(asc|desc)$/
    condition = ($2 == "asc" ? "{ |a, b| a.%s <=> b.%s }" : "{ |a, b| b.%s <=> a.%s }") % [$1, $1]
    instance_eval "sort #{condition}"
  end
end

RubyProf.start
n.times { names.sort_by_length_asc }
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment