From: Ilmari Heikkinen Date: 2004-12-28T23:21:18+09:00 Subject: Re: help on making ruby code faster Hello, here's some precalc to speed up the lots-of-conversions case It makes the only-few-conversions case slower and by uses some more (25*36 numbers) memory. So if you have a shell script that converts a single guid, this'll just slow it down. > =================================================================== > class Guid > @@d36 = ('a'..'z').to_a + ('0'..'9').to_a > @@rd36 = {} > @@d36.each_with_index {|d, i| @@rd36[d[0]] = i} # add these @@exp_a = (0..24).to_a.reverse.map{|i| 36**i} @@exps = {} @@rd36.map{|char,index| @@exps[char] = @@exp_a.map{|e| index*e} } > def Guid.from_base36(val) > val = val.downcase > raise ArgumentError unless val =~ /\A[a-z][a-z0-9]{24}\z/ > n = 0 mult = 0 val.each_byte {|c| n += @@exps[c][mult] mult += 1 } > Guid.from_i(n) > end Got around 35% speedup in the 1000 conversions benchmark. -Ilmari