in reply to Constructing a HoA from 4 separate arrays
and (or approximation - this was my version but I see it's already given above):my %ns_records = map { $_ => [ shift @addr_list, shift @ptr_list, shift @uptime_list ] } @ns_list;
Question is, which is better? The second is perhaps more easily understandable, and also leaves the original arrays intact in case you want them later, but I discovered when doing speed tests (1,000,000 iterations) that the first only takes 15 seconds vs 19 seconds for the second. The exact same results are given if you reverse everything and use pop:for (0..$#ns_list) { $ns_records{$ns_list[$_]} = [$addr_list[$_], $ptr_list[$_], $uptim +e_list[$_]]; }
But what if there's larger numbers of items? Using arrays containing 1000 items each and looping 10,000 times, I got 55 seconds for map / shift, 55 seconds for map / pop, and 45 seconds accessing the arrays directly. Apparently the effort of modifying the arrays takes up additional processing time at larger numbers of items.my %ns_records = map { $_ => [ pop @addr_list, pop @ptr_list, pop @uptime_list ] } reverse @ns_list;
Bottom line though, it doesn't really matter from an efficiency standpoint which way you go. Use whichever method is more readable :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Constructing a HoA from 4 separate arrays
by davido (Cardinal) on Oct 18, 2004 at 05:38 UTC |