On 29.06.2009 22:38, Bill Kelly wrote:
> From: "Lloyd Linklater" <lloyd / 2live4.com>
>> I have been trying to generate a random string.
> 
> I use:
> 
> def gen_random_string(len)
>   (0...len).collect{rand(36).to_s(36)}.map{|x| (rand<0.5)?x:x.upcase}.join
> end
> 
> ..for short strings of length 64 or whatever.  For very long strings, the
> above may be a bit inefficient.  (To generate a 1_000_000 character
> string takes about 2.4 seconds on my system.)

Benchmark time!

	robert

ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
                                user     system      total        real
       1 generate_id        0.110000   0.000000   0.110000 (  0.105000)
       1 gen_random_string  0.015000   0.000000   0.015000 (  0.015000)
       1 g3                 0.016000   0.000000   0.016000 (  0.014000)
       1 g4                 0.000000   0.000000   0.000000 (  0.007000)
       2 generate_id        0.015000   0.000000   0.015000 (  0.004000)
       2 gen_random_string  0.016000   0.000000   0.016000 (  0.021000)
       2 g3                 0.031000   0.000000   0.031000 (  0.026000)
       2 g4                 0.016000   0.000000   0.016000 (  0.006000)
       4 generate_id        0.000000   0.000000   0.000000 (  0.010000)
       4 gen_random_string  0.031000   0.000000   0.031000 (  0.030000)
       4 g3                 0.047000   0.000000   0.047000 (  0.043000)
       4 g4                 0.016000   0.000000   0.016000 (  0.011000)
       8 generate_id        0.015000   0.000000   0.015000 (  0.012000)
       8 gen_random_string  0.047000   0.000000   0.047000 (  0.051000)
       8 g3                 0.078000   0.000000   0.078000 (  0.081000)
       8 g4                 0.032000   0.000000   0.032000 (  0.021000)
      16 generate_id        0.015000   0.000000   0.015000 (  0.020000)
      16 gen_random_string  0.078000   0.000000   0.078000 (  0.083000)
      16 g3                 0.157000   0.000000   0.157000 (  0.150000)
      16 g4                 0.046000   0.000000   0.046000 (  0.044000)
      32 generate_id        0.032000   0.000000   0.032000 (  0.038000)
      32 gen_random_string  0.156000   0.000000   0.156000 (  0.160000)
      32 g3                 0.281000   0.000000   0.281000 (  0.296000)
      32 g4                 0.094000   0.000000   0.094000 (  0.087000)
      64 generate_id        0.078000   0.000000   0.078000 (  0.080000)
      64 gen_random_string  0.313000   0.000000   0.313000 (  0.312000)
      64 g3                 0.562000   0.000000   0.562000 (  0.563000)
      64 g4                 0.188000   0.000000   0.188000 (  0.177000)
     128 generate_id        0.140000   0.000000   0.140000 (  0.151000)
     128 gen_random_string  0.625000   0.000000   0.625000 (  0.638000)
     128 g3                 1.156000   0.000000   1.156000 (  1.211000)
     128 g4                 0.360000   0.000000   0.360000 (  0.364000)
     256 generate_id        0.328000   0.000000   0.328000 (  0.322000)
     256 gen_random_string  1.172000   0.000000   1.172000 (  1.236000)
     256 g3                 2.172000   0.000000   2.172000 (  2.223000)
     256 g4                 0.703000   0.000000   0.703000 (  0.781000)
     512 generate_id        0.625000   0.000000   0.625000 (  0.624000)
     512 gen_random_string  2.422000   0.000000   2.422000 (  2.502000)
     512 g3                 4.406000   0.000000   4.406000 (  4.674000)
     512 g4                 1.406000   0.000000   1.406000 (  1.453000)
ruby 1.9.1p129 (2009-05-12 revision 23412) [i386-cygwin]
                                user     system      total        real
       1 generate_id        0.000000   0.000000   0.000000 (  0.002000)
       1 gen_random_string  0.015000   0.000000   0.015000 (  0.009000)
       1 g3                 0.016000   0.000000   0.016000 (  0.016000)
       1 g4                 0.016000   0.000000   0.016000 (  0.006000)
       2 generate_id        0.000000   0.000000   0.000000 (  0.002000)
       2 gen_random_string  0.000000   0.000000   0.000000 (  0.009000)
       2 g3                 0.031000   0.000000   0.031000 (  0.018000)
       2 g4                 0.000000   0.000000   0.000000 (  0.004000)
       4 generate_id        0.000000   0.000000   0.000000 (  0.003000)
       4 gen_random_string  0.016000   0.000000   0.016000 (  0.018000)
       4 g3                 0.031000   0.000000   0.031000 (  0.022000)
       4 g4                 0.000000   0.000000   0.000000 (  0.007000)
       8 generate_id        0.015000   0.000000   0.015000 (  0.005000)
       8 gen_random_string  0.032000   0.000000   0.032000 (  0.031000)
       8 g3                 0.031000   0.000000   0.031000 (  0.032000)
       8 g4                 0.016000   0.000000   0.016000 (  0.012000)
      16 generate_id        0.015000   0.000000   0.015000 (  0.013000)
      16 gen_random_string  0.063000   0.000000   0.063000 (  0.058000)
      16 g3                 0.047000   0.000000   0.047000 (  0.054000)
      16 g4                 0.016000   0.000000   0.016000 (  0.023000)
      32 generate_id        0.031000   0.000000   0.031000 (  0.029000)
      32 gen_random_string  0.109000   0.000000   0.109000 (  0.104000)
      32 g3                 0.110000   0.000000   0.110000 (  0.112000)
      32 g4                 0.031000   0.000000   0.031000 (  0.040000)
      64 generate_id        0.063000   0.000000   0.063000 (  0.060000)
      64 gen_random_string  0.218000   0.000000   0.218000 (  0.232000)
      64 g3                 0.203000   0.000000   0.203000 (  0.209000)
      64 g4                 0.094000   0.000000   0.094000 (  0.095000)
     128 generate_id        0.140000   0.000000   0.140000 (  0.151000)
     128 gen_random_string  0.407000   0.000000   0.407000 (  0.503000)
     128 g3                 0.375000   0.000000   0.375000 (  0.380000)
     128 g4                 0.187000   0.000000   0.187000 (  0.180000)
     256 generate_id        0.313000   0.000000   0.313000 (  0.365000)
     256 gen_random_string  0.812000   0.000000   0.812000 (  0.806000)
     256 g3                 0.688000   0.000000   0.688000 (  0.708000)
     256 g4                 0.359000   0.000000   0.359000 (  0.352000)
     512 generate_id        0.578000   0.000000   0.578000 (  0.599000)
     512 gen_random_string  1.547000   0.000000   1.547000 (  1.549000)
     512 g3                 1.328000   0.000000   1.328000 (  1.339000)
     512 g4                 0.735000   0.000000   0.735000 (  0.807000)


require 'benchmark'

def generate_id(len = 15)
   s = ''
   len.times { s << 97 + rand(26) }
   s.freeze
end

def gen_random_string(len)
   (0...len).collect{rand(36).to_s(36)}.map{|x| (rand<0.5)?x:x.upcase}.join
end

def g3(len)
   s = "." * len
   s.gsub!(/./) { (97 + rand(26)).chr }
   s
end

def g4 len
   s = "." * len
   len.times {|i| s[i] = (97 + rand(26)).chr}
   s
end

REP = 1000

Benchmark.bm 25 do |b|
   len = 1

   while len < 1_000

     b.report '%7d generate_id' % len do
       REP.times do
	generate_id len
       end
     end

     b.report '%7d gen_random_string' % len do
       REP.times do
	gen_random_string len
       end
     end

     b.report '%7d g3' % len do
       REP.times do
	g3 len
       end
     end

     b.report '%7d g4' % len do
       REP.times do
	g4 len
       end
     end

     len <<= 1
   end
end



-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/