Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Voila!, one benchmark:</p> <pre><code>require 'benchmark' n = 1_000_000 Benchmark.bm(11) do |b| b.report('%w') { n.times { %w[a b c d e f g h i j k l m n o p q r s t u v w x y z] } } b.report('explicit') { n.times { ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] } } b.report('numerics') { n.times { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] } } end user system total real %w 2.590000 0.000000 2.590000 ( 2.591225) explicit 2.590000 0.000000 2.590000 ( 2.584781) numerics 0.300000 0.000000 0.300000 ( 0.309161) user system total real %w 2.590000 0.000000 2.590000 ( 2.591516) explicit 2.590000 0.000000 2.590000 ( 2.584155) numerics 0.300000 0.000000 0.300000 ( 0.308896) user system total real %w 2.590000 0.000000 2.590000 ( 2.592848) explicit 2.590000 0.000000 2.590000 ( 2.585558) numerics 0.300000 0.000000 0.300000 ( 0.308570) </code></pre> <p>I added the "numerics" array test because I suspected that <code>%w</code> is faster than using explicit strings due to testing for stringiness. <code>%w</code> doesn't need to do that, because it assumes everything is a string. After running it three times it's a wash as far as dealing with strings. Numbers rule, strings drool, and all that.</p> <hr> <p>The previous benchmarks were run using Ruby 1.9.3-p286 on my system at work. I tested again using my old MacBook Pro at home, using Ruby 1.8.7-p358, so the following numbers are slower due to the differences in the hosting hardware, plus running an older Ruby:</p> <pre><code> user system total real %w 3.070000 0.000000 3.070000 ( 3.080983) explicit 3.100000 0.000000 3.100000 ( 3.093083) numerics 0.950000 0.040000 0.990000 ( 0.990535) user system total real %w 3.080000 0.010000 3.090000 ( 3.076787) explicit 3.090000 0.000000 3.090000 ( 3.089246) numerics 0.950000 0.030000 0.980000 ( 0.989579) user system total real %w 3.080000 0.000000 3.080000 ( 3.073877) explicit 3.090000 0.000000 3.090000 ( 3.091576) numerics 0.950000 0.030000 0.980000 ( 0.989132) </code></pre> <p>On 1.8.7, <code>%w</code> was a tiny bit faster consistently, which probably gave rise to the speed-rumors.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload