Advertisement
Guest User

Anonymous

a guest
Dec 30th, 2010
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.52 KB | None | 0 0
  1. module ToSLookup
  2.     CACHE = {}
  3.     def self.get number, *args
  4.         CACHE[[number, args]] or (CACHE[[number, args]] = number.convert_to_string *args)
  5.     end
  6. end
  7.  
  8. [Bignum, Fixnum, Float].each do |klass|
  9.     klass.class_eval do
  10.         alias :convert_to_string :to_s
  11.         def to_s *args
  12.             ToSLookup.get self, *args
  13.         end
  14.     end
  15. end
  16.  
  17. require 'benchmark'
  18.  
  19. Benchmark.bm do |x|
  20.     x.report('old way:') { 1000.times { -1000.upto(1000) { |i| i.convert_to_string } } }
  21.     x.report('new way:') { 1000.times { -1000.upto(1000) { |i| i.to_s } } }
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement