Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal Schwartz) Newsgroups: comp.lang.perl Subject: Re: Efficiency of eval (emulating multidimentional arrays) Message-ID: <1990Jul5.171801.3176@iwarp.intel.com> Date: 5 Jul 90 17:18:01 GMT References: <13041@cbmvax.commodore.com> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal Schwartz) Distribution: comp.lang.perl Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 59 In-Reply-To: schmid@spica.cbmvax.cbm.commodore.com (Bob Schmid) 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!"=/