Advertisement
Guest User

Ruby String Concatenation Benchmark

a guest
Jul 9th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.37 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. n = 2000
  4.  
  5. Benchmark.bmbm do |x|
  6.   x.report('+=') {
  7.     num = rand.to_s[0..10] * 100
  8.     c = ""
  9.  
  10.     n.times { c += num }
  11.   }
  12.  
  13.   x.report('<<') {
  14.     num = rand.to_s[0..10] * 100
  15.     c = ""
  16.  
  17.     n.times { c << num }
  18.   }
  19.  
  20.   x.report('#{}'){
  21.     num = rand.to_s[0..10] * 100
  22.     c = ""
  23.  
  24.     n.times { c = "#{c}#{num}" }
  25.   }
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement