Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active June 5, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/6c6a7d787e0f1666f4c6d858c8402a01 to your computer and use it in GitHub Desktop.
Save rmosolgo/6c6a7d787e0f1666f4c6d858c8402a01 to your computer and use it in GitHub Desktop.
Calling a pure function in Ruby (benchmark)
require "benchmark/ips"
# Let's say you want pure functions in Ruby.
# Which form of "packaging" is the fastest?
pure_func_proc = -> (a, b) { a + b }
pure_func_lambda = lambda { |a, b| a + b }
def pure_func_global(a, b)
a + b
end
method_obj_global = method(:pure_func_global)
module PureFunctionModule
def self.call_func(a, b)
a + b
end
PROC = -> (a, b) { a + b }
LAMBDA = lambda { |a, b| a + b }
end
class PureFunctionClass
def initialize(a, b)
@a = a
@b = b
end
def result
@a + @b
end
def self.call_func(a, b)
self.new(a, b).result
end
end
pure_function_instance = PureFunctionClass.new(3, 4)
Benchmark.ips do |x|
x.report("Local proc object") { pure_func_proc.call(3, 4) }
x.report("Local lambda object") { pure_func_lambda.call(3, 4) }
x.report("Global method") { pure_func_global(3, 4) }
x.report("Method object") { method_obj_global.call(3, 4) }
x.report("Module function") { PureFunctionModule.call_func(3, 4) }
x.report("Nested proc constant") { PureFunctionModule::PROC.call(3, 4) }
x.report("Nested lambda constant") { PureFunctionModule::LAMBDA.call(3, 4) }
x.report("Instance method") { pure_function_instance.result }
x.report("Initialize + instance method") { PureFunctionClass.new(3, 4).result }
x.report("Wrapper class method") { PureFunctionClass.call_func(3, 4) }
x.report("Inline call") { 3 + 4 }
x.compare!
end
@rmosolgo
Copy link
Author

rmosolgo commented Aug 5, 2016

My result: short of writing the code inline, use a module function

$ ruby -v
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin16]
$ ruby func_bench.rb
Warming up --------------------------------------
   Local proc object   244.235k i/100ms
 Local lambda object   244.531k i/100ms
       Global method   280.828k i/100ms
       Method object   237.446k i/100ms
     Module function   277.069k i/100ms
Nested proc constant   244.811k i/100ms
Nested lambda constant
                       247.960k i/100ms
     Instance method   273.002k i/100ms
Initialize + instance method
                       187.145k i/100ms
Wrapper class method   179.844k i/100ms
         Inline call   297.230k i/100ms
Calculating -------------------------------------
   Local proc object      6.503M (± 4.9%) i/s -     32.483M in   5.007174s
 Local lambda object      6.608M (± 4.8%) i/s -     33.012M in   5.007650s
       Global method      9.500M (± 5.1%) i/s -     47.460M in   5.008924s
       Method object      6.335M (± 5.0%) i/s -     31.818M in   5.035522s
     Module function      9.239M (± 5.3%) i/s -     46.271M in   5.021866s
Nested proc constant      6.692M (± 4.7%) i/s -     33.539M in   5.022728s
Nested lambda constant
                          6.717M (± 4.4%) i/s -     33.723M in   5.030364s
     Instance method      8.836M (± 5.2%) i/s -     44.226M in   5.019526s
Initialize + instance method
                          3.744M (± 4.0%) i/s -     18.714M in   5.007105s
Wrapper class method      3.488M (± 3.4%) i/s -     17.445M in   5.007638s
         Inline call     11.482M (± 8.8%) i/s -     57.068M in   5.017664s

Comparison:
         Inline call: 11482136.6 i/s
       Global method:  9500325.0 i/s - 1.21x  slower
     Module function:  9239475.3 i/s - 1.24x  slower
     Instance method:  8835668.2 i/s - 1.30x  slower
Nested lambda constant:  6716565.8 i/s - 1.71x  slower
Nested proc constant:  6692395.9 i/s - 1.72x  slower
 Local lambda object:  6607913.8 i/s - 1.74x  slower
   Local proc object:  6503039.4 i/s - 1.77x  slower
       Method object:  6334708.3 i/s - 1.81x  slower
Initialize + instance method:  3743729.3 i/s - 3.07x  slower
Wrapper class method:  3487944.5 i/s - 3.29x  slower

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