which handily told me that @y{@x}=undef; is the way to go! (notice the s/iter scores.) -- the print statement is to verify that there are at least 40000 elements in %y (not guaranteed since we use rand).use Benchmark 'cmpthese'; use strict; my @x = map join(',', split(//, rand(10000))), 0..80000; my %y; cmpthese(-2, {'@y{@x}' => sub { undef %y; @y{@x} = undef }, '%y = map' => sub { undef %y; %y = map(($_ => undef), @x) } } ); print 0+%y,$/;
When I try to use this in my real code, it takes much longer. (I'm still using strict and warnings)s/iter %y = map @y{@x} %y = map 2.45 -- -49% @y{@x} 1.25 97% --
So, from my tests I expected to see this snippet run rather quickly since the permute function is very fast for a list of 8 elements (< 1 sec). At the very least I expected the output from the two localtimes to be close together, but instead I got:# ...snip my ($n,$i,@n,@temp,%arrangements); # get size and create all permutations (comma separated) $n = 8; @n = (1..$n); $i = 0; $temp[&_fact($n)-1] = 1; # _fact := n! $|++; permute { $temp[$i++] = join(',',@n) } @n; print STDERR scalar(localtime),$/; @arrangements{@temp} = undef; print STDERR scalar(localtime),$/;
When I watch the processes in "top", the test code quickly slurps up 70M of memory, but my production code only uses 6M (and does actually work correctly). I'm not sure what the problem is and have run out of ideas for how to isolate the problem. Any suggestions would be greatly appreciated!computing 8-arrangements... Mon Sep 30 15:30:31 2002 Mon Sep 30 15:33:35 2002
Thanks,
Dean
P.S. I am actually fairly certain that Perl is the proper solution fo this problem since the rest of the program runs very quickly (tested using DProf) and would be a nightmare to write in any other language.
If we didn't reinvent the wheel, we wouldn't have rollerblades.
In reply to constructing large hashes by duelafn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |