Interesting Array Initialization Typo

When initializing for example : [ [1, 2], [2, 3], [3, 2] ]

You may forget about one colon, you will get:

[ [1, 2] [2, 3], [3, 2] ]
=> [[], [3, 2]]

No syntax error:) No warnings here, beware! :slight_smile:

On Jul 25, 2008, at 8:58 AM, Maciej Tomaka wrote:

it’s not a syntax error, it’s the same as this

cfp:~ > ruby -e’ p( ( array = [1,2] )[ index = [2,3] ] ) ’

which is the same as

cfp:~ > ruby -e’ array = [1,2]; index = [2,3]; p array[*index]’
[]

[1, 2] [2, 3] is the array ‘[1,2]’ indexed by ‘2,3’

a @ http://codeforpeople.com/

Ara Howard wrote:

On Jul 25, 2008, at 8:58 AM, Maciej Tomaka wrote:

it’s not a syntax error, it’s the same as this

cfp:~ > ruby -e’ p( ( array = [1,2] )[ index = [2,3] ] ) ’

which is the same as

cfp:~ > ruby -e’ array = [1,2]; index = [2,3]; p array[*index]’
[]

[1, 2] [2, 3] is the array ‘[1,2]’ indexed by ‘2,3’

a @ http://codeforpeople.com/

I undestand what is going on here.

It just might be tricky to figure what’s wrong in code with such typo.

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? "

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

For less trivial cases I tend to:

a = []
a << [1,2]
a << [2,3]
a << [3,4]

Absolutely no chance of misreading that.

Dave B. wrote:

a << [3,4]
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

Le 29 juillet à 21:46, Justin C. a écrit :

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)

Note that I tried to add this to your test :

t.report("=") do
10000.times do
a = Array.new(100)
100.times do |x|
a[x] = x
end
end
end

And…

Rehearsal --------------------------------------
<< 0.304688 0.007812 0.312500 ( 0.313559)
= 0.304688 0.000000 0.304688 ( 0.306314)
[] 0.031250 0.000000 0.031250 ( 0.027802)
----------------------------- total: 0.648438sec

     user     system      total        real

<< 0.304688 0.000000 0.304688 ( 0.309506)
= 0.296875 0.000000 0.296875 ( 0.299347)
[] 0.023438 0.007812 0.031250 ( 0.028271)

Surprising !

(ruby 1.8.6 (2007-09-24 patchlevel 111) [amd64-freebsd7])

Fred

The correct benchmark is:
(look how long x.times takes :slight_smile:
require ‘benchmark’
include Benchmark

bmbm do |t|
t.report("<<") do
100_000.times do
a = []
a << [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
a << [ 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
a << [ 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
a << [ 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]
a << [ 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
a << [ 50, 51, 52, 53, 54, 55, 56, 57, 58, 59]
a << [ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69]
a << [ 70, 71, 72, 73, 74, 75, 76, 77, 78, 79]
a << [ 80, 81, 82, 83, 84, 85, 86, 87, 88, 89]
a << [ 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
end
end

t.report("[]") do
    100_000.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

t.report("NO") do
    100_000.times do
        100.times do |x|
        end
    end
end

end

Rehearsal --------------------------------------
<< 1.020000 0.020000 1.040000 ( 1.047729)
[] 0.730000 0.010000 0.740000 ( 0.741970)
NO 4.270000 1.510000 5.780000 ( 5.808074)
----------------------------- total: 7.560000sec

     user     system      total        real

<< 1.000000 0.020000 1.020000 ( 1.032165)
[] 0.740000 0.010000 0.750000 ( 0.743470)
NO 5.070000 1.560000 6.630000 ( 6.634026)

So << and [] are comparable.

Shadowfirebird has good solution inmho w/o significant perfomance
impact.

tested again on 1.8.6,
previous was on 1.8.4 (2005-12-24) [i486-linux] 1.8.4)

Rehearsal --------------------------------------
<< 0.742188 0.000000 0.742188 ( 0.748359)
[] 0.570312 0.000000 0.570312 ( 0.567802)
NO 0.007812 0.000000 0.007812 ( 0.009996)
----------------------------- total: 1.320312sec

     user     system      total        real

<< 0.742188 0.000000 0.742188 ( 0.747654)
[] 0.570312 0.000000 0.570312 ( 0.567690)
NO 0.007812 0.000000 0.007812 ( 0.009741)

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

Maciej Tomaka wrote:

tested again on 1.8.6,
previous was on 1.8.4 (2005-12-24) [i486-linux] 1.8.4)

Rehearsal --------------------------------------
<< 0.742188 0.000000 0.742188 ( 0.748359)
[] 0.570312 0.000000 0.570312 ( 0.567802)
NO 0.007812 0.000000 0.007812 ( 0.009996)
----------------------------- total: 1.320312sec

     user     system      total        real

<< 0.742188 0.000000 0.742188 ( 0.747654)
[] 0.570312 0.000000 0.570312 ( 0.567690)
NO 0.007812 0.000000 0.007812 ( 0.009741)

ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-freebsd6]

The ‘NO’ test was wrong.
This is retest with code that was pasted before:

Rehearsal --------------------------------------
<< 0.750000 0.000000 0.750000 ( 0.749043)
[] 0.570312 0.000000 0.570312 ( 0.571285)
NO 1.054688 0.000000 1.054688 ( 1.056768)
----------------------------- total: 2.375000sec

     user     system      total        real

<< 0.750000 0.000000 0.750000 ( 0.745733)
[] 0.562500 0.000000 0.562500 ( 0.567207)
NO 1.039062 0.000000 1.039062 ( 1.040605)

Le 30 juillet à 11:24, Maciej Tomaka a écrit :

The correct benchmark is:

/…/

In both cases, your arrays are nested ; you’re not testing the same
thing…

Fred

F. Senault wrote:

Le 30 juillet � 11:24, Maciej Tomaka a �crit :

The correct benchmark is:

/…/

In both cases, your arrays are nested ; you’re not testing the same
thing…

Fred

Please re-read the posts. We were measuring difference between
initializations:

a = [ [1,2,3], [2,3,4], [5,6,7] ]

and

a = []

a << [1,2,3]
a << [2,3,4]
a << [5,6,7]

And that is what I was testing.

Best Regards.

Maciej