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:

路路路

--
Posted via http://www.ruby-forum.com/.

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/

路路路

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

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:
-- Posted via http://www.ruby-forum.com/.

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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.

--
Posted via http://www.ruby-forum.com/.

For less trivial cases I tend to:

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

Absolutely no chance of misreading that.

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

路路路

--
Posted via http://www.ruby-forum.com/.

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

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

路路路

Le 29 juillet 脿 21:46, Justin Collins 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)

--
The Admins are the priesthood of an irrational, anarchistic, random,
pseudo-religious hardware platform, trying to impose their will on
people who would rather be using us to avoid real work.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽(Joe Moore in the SDM)

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.

路路路

--
Posted via http://www.ruby-forum.com/.

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]

路路路

--
Posted via http://www.ruby-forum.com/.

/.../

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

Fred

路路路

Le 30 juillet 脿 11:24, Maciej Tomaka a 茅crit :

The correct benchmark is:

--
As far as the laws of mathematics refer to reality, they are not
certain; and as far as they are certain, they do not refer to reality.
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽(Albert Einstein)

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)

路路路

--
Posted via http://www.ruby-forum.com/.

F. Senault wrote:

Le 30 juillet 锟 11:24, Maciej Tomaka a 锟絚rit :

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

路路路

--
Posted via http://www.ruby-forum.com/.