Skip to content

Instantly share code, notes, and snippets.

@HParker
Created October 31, 2014 21:51
Show Gist options
  • Save HParker/8a2c88d604d3c2c3fd34 to your computer and use it in GitHub Desktop.
Save HParker/8a2c88d604d3c2c3fd34 to your computer and use it in GitHub Desktop.
String operations in ruby
puts "double assignment:"
puts Benchmark.measure {
100_000_000.times { "abc" }
}
# double assignment:
# 9.320000 0.010000 9.330000 ( 9.316967)
puts "double comparisons:"
x = "a"
y = "b"
puts Benchmark.measure {
100_000_000.times { x <=> y }
}
# double comparisons:
# 6.900000 0.000000 6.900000 ( 6.912507)
puts "single assignment:"
puts Benchmark.measure {
100_000_000.times { 'abc' }
}
# single assignment:
# 9.360000 0.010000 9.370000 ( 9.363096)
puts "single comparison:"
x = 'a'
y = 'b'
puts Benchmark.measure {
100_000_000.times { x <=> y }
}
# single comparison:
# 6.980000 0.000000 6.980000 ( 6.981026)
puts "double with interpolation:"
puts Benchmark.measure {
100_000_000.times { double_interpolated = "ab#{'c'}" }
}
# double with interpolation:
# 9.820000 0.010000 9.830000 ( 9.832024)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment