Skip to content

Instantly share code, notes, and snippets.

@palexander
Last active January 4, 2016 07:59
Show Gist options
  • Select an option

  • Save palexander/8592640 to your computer and use it in GitHub Desktop.

Select an option

Save palexander/8592640 to your computer and use it in GitHub Desktop.

Revisions

  1. palexander revised this gist Jan 24, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ruby_optional_arguments_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -33,3 +33,7 @@ def set_with_keywords(name: "default name", email: "[email protected]")
    end
    end
    end

    user system total real
    hash: 8.130000 0.290000 8.420000 ( 8.420061)
    keywords: 13.720000 0.540000 14.260000 ( 14.275596)
  2. palexander created this gist Jan 24, 2014.
    35 changes: 35 additions & 0 deletions ruby_optional_arguments_benchmark.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    require 'benchmark'

    COUNT = 10_000_000
    NAME = "Test Name"
    EMAIL = "[email protected]"

    class Person
    attr_accessor :name, :email

    def set_with_hash(options = {})
    @name = options[:name] || "default name"
    @email = options[:email] || "[email protected]"
    end

    def set_with_keywords(name: "default name", email: "[email protected]")
    @name = name
    @email = email
    end
    end

    Benchmark.bm(10) do |x|
    x.report("hash:") do
    COUNT.times do
    p = Person.new
    p.set_with_hash(name: NAME, email: EMAIL)
    end
    end

    x.report("keywords:") do
    COUNT.times do
    p = Person.new
    p.set_with_keywords(name: NAME, email: EMAIL)
    end
    end
    end