Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ToSLookup
- CACHE = {}
- def self.get number, *args
- CACHE[[number, args]] or (CACHE[[number, args]] = number.convert_to_string *args)
- end
- end
- [Bignum, Fixnum, Float].each do |klass|
- klass.class_eval do
- alias :convert_to_string :to_s
- def to_s *args
- ToSLookup.get self, *args
- end
- end
- end
- require 'benchmark'
- Benchmark.bm do |x|
- x.report('old way:') { 1000.times { -1000.upto(1000) { |i| i.convert_to_string } } }
- x.report('new way:') { 1000.times { -1000.upto(1000) { |i| i.to_s } } }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement