Created
October 11, 2018 12:50
-
-
Save carpodaster/6afb8b9812ae43a427af5ac7230ff776 to your computer and use it in GitHub Desktop.
Benchmarking String#split and String#rpartition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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