Admin Tensor wrote in post #1123558:
> Well, in some other languages, such as JavaScript and Lua, there is no
> Array; "array" is constructed using a Hash.  Of course, if you are used
> to with languages such as C, you always think that in a compiled
> language, Array (direct access) should perform better than Hash.

Given that this is a Ruby forum, and we're talking about a Ruby script, 
with Ruby snippets, it could be reasonable to assume that I meant the 
Ruby "Array" class.  Specifically, Array as implemented in ruby 2.0-p247 
and a recent 2.1-trunk.

I've not spent much time digging through array.c but I'm pretty sure it 
does follow the C paradigm for arrays (i.e. contiguous fixed-sized 
memory blocks), and I assume that each element of the array would be a 
VALUE (fixnum, object pointer, etc.) or some equivalent.  As such, I'd 
expect the lookup time to be smaller than for Hash; therefore I guess 
(but didn't have much time to dig into it) that the performance hit, 
such as it was, came from somewhere else. Either some other 
under-the-hood overhead, or a glitch in my implementation (I don't have 
the code here to inspect), or since we're talking microseconds' or even 
nanoseconds' difference when calculating 50!, maybe even memory caches. 
It might even be the fact that Array doesn't have a default_proc, so I 
had to wrap the Array with another object.

I'm also aware that Array does some sort of aggressive expansion, but to 
be sure there's only a single allocation, I tried something like this:

  m = ary.length
  if n > m
    ary[n] = nil
  end
  (m..n).each{|i| ary[i] = ... }

The difference for 51! and fib(50) was, as you'd expect, negligible.

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