schmid@spica.cbmvax.cbm.commodore.com (Bob Schmid) (07/05/90)
I'm using eval to create a large number of associative arrays based on different, variable dependent, names. Eval seems to take about 1 millisecond extra per eval on a Sparcstation1 compared with explicitly typing out the variable names. This seems to me like a good tradeoff between execution speed and manageable coding complexity (I really need that extra array index!). Any comments on eval efficiency or better ways to skin the cat will be most appreciated.... The following sample program uses integer indexing. I usually use strings (hence the need for associative arrays). for $cell (1..100) { for $i (1..100) { eval("\$junk_$cell{\$i} = \$i"); } } for $cell (1..100) { while (($key, $val) = eval("each \%junk_$cell")) { print "\$key=$key \$val=$val"; } } ------------------------------------------------------ R. Schmid <uunet!cbmvax!schmid> <schmid@cbmvax.cbm.commodore.com> Commodore Technology 1200 Wilson Drive West Chester, PA 19380 ------------------------------------------------------
merlyn@iwarp.intel.com (Randal Schwartz) (07/06/90)
In article <13041@cbmvax.commodore.com>, schmid@spica (Bob Schmid) writes: | I'm using eval to create a large number of associative arrays based on | different, variable dependent, names. Eval seems to take about 1 | millisecond extra per eval on a Sparcstation1 compared with explicitly | typing out the variable names. This seems to me like a good tradeoff | between execution speed and manageable coding complexity (I really | need that extra array index!). | | Any comments on eval efficiency or better ways to skin the cat will be | most appreciated.... | | The following sample program uses integer indexing. I usually use | strings (hence the need for associative arrays). | | for $cell (1..100) | { | for $i (1..100) | { | eval("\$junk_$cell{\$i} = \$i"); | } | } | for $cell (1..100) | { | while (($key, $val) = eval("each \%junk_$cell")) | { | print "\$key=$key \$val=$val"; | } | } Do you *really* need all of those as separate variables? I prefer things like: for $cell (1..10) { for $i (1..10) { $data{"$cell $i"} = $i; } } for $cell (1..10) { for $key (sort grep(/^$cell /,keys data)) { print "$key = $data{$key}\n"; } } If you don't care to access them by group, leave the 'grep' out. Actually, I don't know which is more efficient. Larry could probably wake me up on this one (as usual). What we're comparing is the speed of parsing a string, and looking up something in the symbol table, with looking up a hashed key in an assoc array. I would think that the second beats the first. But intuition is a funny thing. $_="cker,rl haer PeanothJust ";1 while s/(.{5})(.{5})?/$_{$2}=$1,$2/e;print while $_=$_{$_}; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Welcome to Portland, Oregon, home of the California Raisins!"=/