Created
March 11, 2014 09:12
-
-
Save araslanov-e/9482129 to your computer and use it in GitHub Desktop.
The fastest and most readable way to remove the first character from a string
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
require 'benchmark' | |
N = 1_000_000 | |
puts RUBY_VERSION | |
STR = "[12,23,987,43" | |
Benchmark.bm(7) do |b| | |
b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } } | |
b.report('sub') { N.times { "[12,23,987,43".sub(/^\[+/, "") } } | |
b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } } | |
b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } } | |
b.report('slice') { N.times { "[12,23,987,43".slice!(0) } } | |
b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment