Skip to content

Instantly share code, notes, and snippets.

@carpodaster
Created October 11, 2018 12:50
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 carpodaster/6afb8b9812ae43a427af5ac7230ff776 to your computer and use it in GitHub Desktop.
Save carpodaster/6afb8b9812ae43a427af5ac7230ff776 to your computer and use it in GitHub Desktop.
Benchmarking String#split and String#rpartition
# frozen_string_literal: true
require 'benchmark'
input = "This::Is::One::Heavily::Nested::Module"
n = 1_000_000
Benchmark.bm do |benchmark|
benchmark.report("String#split") do
n.times do
input.split("::").last
end
end
benchmark.report("String#rpartition") do
n.times do
input.rpartition("::").last
end
end
end
=begin
$ ruby benchmark.rb
user system total real
String#split 0.840000 0.000000 0.840000 ( 0.845646)
String#rpartition 0.500000 0.000000 0.500000 ( 0.510527)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment