[Israel.pm] Perl Advocacy

Issac Goldstand margol at beamartyr.net
Tue Dec 28 11:01:03 PST 2004


----- Original Message ----- 
From: "david istermann" <interdist at gmail.com>
> And, my second comment was actually a serious one. I honestly see no
> difference between indexing something by integers or by strings. The
> array and the hash; Both have scalar values, tied to indices. Both can
> be iterated. Why separate them so brutally when essentially they serve
> the same purpose?
>

Because they're not the same data structures.  A hash table uses a hash 
algorithm and buckets to store the data in memory.  An array is a sequential 
data set with records of a preset size (pointers/refs/etc have static small 
sizes), sitting in memory.  You can treat an array as a hash (eg $hash{0}, 
$hash{1}, etc) but you'll really be hashing the integers 0,1,2, etc to find 
the respective buckets.  This is (likely) slower than direct memory access 
if you already know the offset of the member you want.

The fact that PHP combines the two means that they are either hiding the 
implementation details from the user, but really storing integers in an 
array, and strings in a hash table (unlikely, since otherwise they'd need to 
convert arrays to hash tables the moment you add a string), or just storing 
everything in a hash table, which slows you down when you *really* want an 
array...

With Perl, though, if you do $ar[1000000]="oops"; you can watch your memory 
usage spike up nicely - that's a REAL array.  Usable and abusable.  And if 
you really use so many emmbers, you'll see the speed difference (someone 
want to voulenteer to benchmark?)

  Yitzchak




More information about the Perl mailing list