Justin Collins
7/29/2008 7:47:00 PM
Dave Bass wrote:
> Regarding typos: Ruby can't be expected to know what you meant to type,
> so if it's syntactically correct it will let it through -- as with any
> language! To do otherwise would be as annoying as Microsoft's Clippy: "I
> see you're trying to set up an array. Did you just make a typo? <wink>"
>
> Shadowfirebird wrote:
>
>> a = []
>> a << [1,2]
>> a << [2,3]
>> a << [3,4]
>>
>
> Without any actual understanding of what's going on inside Ruby
> (sorry!), I suspect this code will be slower, as the array has to be
> grown three times, rather than springing into existence fully
> initialised. Or is this a negligible overhead? Or does the code get
> optimised by the compiler/interpreter? Maybe someone knowledgeable would
> like to comment.
>
> In particular it would be interesting to know if putting array pushes,
> pops, shifts and unshifts inside a big loop is inefficient.
>
> Dave
>
Here's what I got:
Rehearsal --------------------------------------
<< 0.990000 0.290000 1.280000 ( 1.331386)
[] 0.060000 0.000000 0.060000 ( 0.070595)
----------------------------- total: 1.340000sec
user system total real
<< 0.940000 0.320000 1.260000 ( 1.302148)
[] 0.060000 0.010000 0.070000 ( 0.070150)
With
ruby 1.8.6 (2008-03-03 patchlevel 114) [x86_64-linux-gnu]
and
require 'benchmark'
include Benchmark
bmbm do |t|
t.report("<<") do
10000.times do
a = []
100.times do |x|
a << x
end
end
end
t.report("[]") do
10000.times do
a = [0, 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, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
end
end
end
-Justin