Skip to content

Instantly share code, notes, and snippets.

@schneems
Last active January 2, 2019 08:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schneems/67732e18063c8c081016fd48ab0e3d6e to your computer and use it in GitHub Desktop.
Save schneems/67732e18063c8c081016fd48ab0e3d6e to your computer and use it in GitHub Desktop.
# Ruby 2.6 introduced String#split that allows it to take a block instead of just returning an array
# https://twitter.com/schneems/status/1078848803694882817
require 'benchmark/ips'
STRANG = "hello/there/hi/how/are/you/today".freeze
Benchmark.ips do |x|
x.report("normal") { first_strang = STRANG.split("/".freeze).first }
x.report("new ") {
first_strang = nil
STRANG.split("/".freeze) { |x| first_strang = x; break }
}
x.compare!
end
# Warming up --------------------------------------
# normal 95.432k i/100ms
# new 196.820k i/100ms
# Calculating -------------------------------------
# normal 1.204M (± 4.1%) i/s - 6.108M in 5.079671s
# new 3.241M (± 3.8%) i/s - 16.336M in 5.047544s
# Comparison:
# new : 3241306.0 i/s
# normal: 1204474.2 i/s - 2.69x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment