Guest User

Untitled

a guest
Feb 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. require 'benchmark'
  2. require 'paramer'
  3.  
  4. class String
  5. def pad_to(size)
  6. spaces = size - length
  7. return self unless spaces > 0
  8. spaces.times { self << " " }
  9. self
  10. end
  11. end
  12.  
  13. def bench(name, options={})
  14. $stdout.print "#{name} ".pad_to(33)
  15. $stdout.flush
  16. result = Benchmark.measure { yield }
  17. puts "#{result.real} seconds"
  18. end
  19.  
  20. def run(name, string)
  21. puts name
  22. bench("- only scanning then injecting:") { string.scan_then_inject }
  23. bench("- scanning groups and injecting:") { string.scan_groups_then_inject }
  24. bench("- scanning then splat hashing:") { string.scan_then_splat_hash }
  25. bench("- splitting then splat hashing:") { string.split_then_splat_hash }
  26. puts
  27. end
  28.  
  29. TINY = ("foo=bar&this=that&klass=eigen=_method=get&" * 500).freeze
  30. MED = (TINY * 10).freeze
  31. LONG = (MED * 10).freeze
  32.  
  33. run "Running with tiny string", TINY
  34. run "Running with medium string", MED
  35. run "Running with ridiculous string", LONG
Add Comment
Please, Sign In to add comment