APM: Sparse arrays
jeremy
jeremyb at univista.com
Thu Jul 10 09:28:56 CDT 2003
I simply increment a counter at the bottom of the loop.
I've added a print section so you can see what happens.
$ary[100] = 1;
$ary[100000] = 2;
$ary[1000000] = 3;
$count = 0;
foreach $elt (@ary) {
$some_value = $elt;
$the_index = count; # how do I get this
information?
if( $some_value ){
print "\$ary[ $count ] -> $some_value\n";
}
$count++;
}
As far as this little section of code being a CPU hog ....I don't know
what to say. Does it really take days to complete? On any modern
system this ought to finish in a couple of seconds ...maybe 3. In
fact, on my machine it took 1.315s according to the time function. The
machine is a 1.4 gig P4 with 921 MB SDRAM. Is you machine very old?
Jeremy
On Thu, 2003-07-10 at 08:00, Brian Michalk wrote:
> Let's say I have a sparse array:
> $ary[100]=1;
> $ary[100000]=2;
> $ary[1000000]=3;
>
> Now, I want to iterate over the array, either from lowest to highest array
> index, and still retain the index of the element returned. How do I do
> that?
>
> foreach $elt (@ary) {
> $some_value = $elt;
> $the_index = ????????; # how do I get this information?
> }
>
>
> This algorithm is chewing up CPU time, causing the process to take days to
> complete... the dataset is several gigabytes large.
>
> Using a hash, and sorting the keys will not give me a performance benefit.
>
> _______________________________________________
> Austin mailing list
> Austin at mail.pm.org
> http://mail.pm.org/mailman/listinfo/austin
More information about the Austin
mailing list