Skip to content

Instantly share code, notes, and snippets.

@nzajt
Last active August 20, 2018 23:21
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 nzajt/64eef748000b8d1e6a5ba33cd6cf88b3 to your computer and use it in GitHub Desktop.
Save nzajt/64eef748000b8d1e6a5ba33cd6cf88b3 to your computer and use it in GitHub Desktop.
Testing if a module or class is faster when only using one method. requires gem 'benchmark-ips' from https://github.com/evanphx/benchmark-ips
require 'benchmark/ips'
# test for modules
module TwoFer
# Takes name or no name and returns statement
def self.two_fer(name = 'you')
"One for #{name}, one for me."
end
end
class TwoFerC
def self.two_fer(name = 'you')
"One for #{name}, one for me."
end
end
Benchmark.ips do |x|
x.config(time: 5, warmup: 2)
x.report('Module: ') { TwoFer.two_fer('TestName') }
x.report('Class: ') { TwoFerC.two_fer('TestName') }
x.report('Module No Name:') { TwoFer.two_fer }
x.report('Class No Name:') { TwoFerC.two_fer }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment