Skip to content
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

Add benchmark script #14

Merged
merged 2 commits into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)

task default: :spec

task :benchmark do
require_relative "./benchmark"
end
46 changes: 46 additions & 0 deletions benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require "bundler/setup"
Bundler.setup

require "benchmark"
require "benchmark/ips"
require "benchmark/memory"

puts "```ruby"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this puts stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You just copy the full output, paste it in GitHub, for example — that's all.

Example:

bundle exec rake benchmark

# frozen_string_literal: true

require "bundler/setup"
Bundler.setup

require "benchmark"
require "benchmark/ips"
require "benchmark/memory"

puts "```ruby"
puts File.read(__FILE__)
puts "```"
puts
puts "### Output"
puts
puts "```"

require_relative "lib/memery"

class Foo
  class << self
    include Memery

    def base_find(char)
      ("a".."k").find { |letter| letter == char }
    end

    memoize def find_new(char)
      base_find(char)
    end
  end
end

def test_memery
  Foo.find_new("d")
end

Benchmark.ips do |x|
  x.report("test_memery") { test_memery }
end

Benchmark.memory do |x|
  x.report("test_memery") { 100.times { test_memery } }
end

puts "```"

Output

Warming up --------------------------------------
         test_memery    33.193k i/100ms
Calculating -------------------------------------
         test_memery    386.577k (± 2.4%) i/s -      1.958M in   5.069159s
Calculating -------------------------------------
         test_memery    16.000k memsize (     0.000  retained)
                       400.000  objects (     0.000  retained)
                         3.000  strings (     0.000  retained)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bench script itself and bench results in one place, Markdown-formatted.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

puts File.read(__FILE__)
puts "```"
puts
puts "### Output"
puts
puts "```"

require_relative "lib/memery"

class Foo
class << self
include Memery

def base_find(char)
("a".."k").find { |letter| letter == char }
end

memoize def find_new(char)
base_find(char)
end
end
end

def test_memery
Foo.find_new("d")
end

Benchmark.ips do |x|
x.report("test_memery") { test_memery }
end

Benchmark.memory do |x|
x.report("test_memery") { 100.times { test_memery } }
end

puts "```"
2 changes: 2 additions & 0 deletions memery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
end
spec.require_paths = ["lib"]

spec.add_development_dependency "benchmark-ips"
spec.add_development_dependency "benchmark-memory"
spec.add_development_dependency "bundler"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "pry"
Expand Down