in reply to Re^5: Concerning hash operations (appending, concatenating)
in thread Concerning hash operations (appending, concatenating)
The grep is superfluous and, IMO, only distracts from the code. Here's the sub to add to the above benchmark:
And the benchmarks. First, the original 500/500 overlap/new keys:TANKTALUS => sub { my %copy = %hash; @copy{keys %add} = values %add; },
As you can see, the difference is pretty insignificant compared to the extra "line noise" that needs to be parsed by the human who maintains it. I didn't think it'd change much with a larger sample, but I tried it anyway (using your 5_001..12_000 hash):Rate ZAXO NULL TANKTALUS JAPHY BASE ZAXO 12.3/s -- -23% -59% -59% -76% NULL 16.1/s 30% -- -46% -47% -68% TANKTALUS 29.8/s 142% 85% -- -2% -41% JAPHY 30.4/s 147% 89% 2% -- -40% BASE 50.9/s 313% 216% 71% 67% --
Somewhere, deep down in the lost precision, the non-grep version comes out ahead. Which we'll attribute to statistical insigificance, just as I did for the 2% in the other direction. ;-)Rate ZAXO NULL JAPHY TANKTALUS BASE ZAXO 9.48/s -- -39% -55% -55% -81% NULL 15.7/s 65% -- -26% -26% -69% JAPHY 21.1/s 122% 35% -- -0% -59% TANKTALUS 21.1/s 123% 35% 0% -- -59% BASE 51.1/s 439% 226% 142% 142% --
The other point is that your answer does not do the same thing as the rest of the examples here. In all the rest of the cases, the new hash always overrides the values in the old hash. That is, if the old hash had a => "b", while the new hash had a => "c", the behaviour of yours vs the rest is different. Which one is correct is dependant on the demands of the situation, although I usually lean towards new hashes winning over old hashes the way I write the code. In cases where I want "first come, first served" policies, then I will definitely keep your grep in mind - it's probably a lot more elegant than the way I have been doing it (if I have - I'd have to look through old code to be sure). Thanks ;-)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: Concerning hash operations (appending, concatenating)
by japhy (Canon) on Mar 24, 2005 at 18:50 UTC |