Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Efficiency of map vs. more verbose basic/fundamental code

by Marshall (Prior)
on Oct 05, 2012 at 03:17 UTC ( #997364=note: print w/ replies, xml ) Need Help??


in reply to Efficiency of map vs. more verbose basic/fundamental code

I think that Version (2) and (1) do exactly the same thing.
Version 2 is easier to understand. And I think that it is as least as fast if not faster. If we are benchmarking things, then we need to to "apples to apples".

#!/usr/bin/perl -w use strict; use Data::Dumper; ## version 1.... my $num =0; my %h = map{$_, $num++}('A'..'Z','a'..'z'); print Dumper \%h; ## version 2... my %hash; foreach my $letter (('A'..'Z','a'..'z')) { $hash{$letter}++; } print Dumper \%hash; foreach (sort keys %h) { print "$_ OK\n" if $hash{$_}; }
$VAR1 = { 'S' => 18, 'a' => 26, 'T' => 19, 'N' => 13, 'K' => 10, 'd' => 29, 'Y' => 24, 'E' => 4, 'j' => 35, 'y' => 50, 'Z' => 25, 'u' => 46, 'k' => 36, 'g' => 32, 't' => 45, 'e' => 30, 'J' => 9, 'W' => 22, 'v' => 47, 's' => 44, 'B' => 1, 'H' => 7, 'c' => 28, 'q' => 42, 'b' => 27, 'D' => 3, 'I' => 8, 'G' => 6, 'z' => 51, 'U' => 20, 'w' => 48, 'F' => 5, 'r' => 43, 'x' => 49, 'V' => 21, 'Q' => 16, 'h' => 33, 'M' => 12, 'C' => 2, 'L' => 11, 'f' => 31, 'i' => 34, 'A' => 0, 'n' => 39, 'O' => 14, 'X' => 23, 'P' => 15, 'm' => 38, 'l' => 37, 'p' => 41, 'R' => 17, 'o' => 40 }; $VAR1 = { 'S' => 18, 'a' => 26, 'T' => 19, 'N' => 13, 'K' => 10, 'd' => 29, 'Y' => 24, 'E' => 4, 'j' => 35, 'y' => 50, 'Z' => 25, 'u' => 46, 'k' => 36, 'g' => 32, 't' => 45, 'e' => 30, 'J' => 9, 'W' => 22, 'v' => 47, 's' => 44, 'B' => 1, 'H' => 7, 'c' => 28, 'q' => 42, 'b' => 27, 'D' => 3, 'I' => 8, 'G' => 6, 'z' => 51, 'U' => 20, 'w' => 48, 'F' => 5, 'r' => 43, 'x' => 49, 'V' => 21, 'Q' => 16, 'h' => 33, 'M' => 12, 'C' => 2, 'L' => 11, 'f' => 31, 'i' => 34, 'A' => 0, 'n' => 39, 'O' => 14, 'X' => 23, 'P' => 15, 'm' => 38, 'l' => 37, 'p' => 41, 'R' => 17, 'o' => 40 }; A OK B OK C OK D OK E OK F OK G OK H OK I OK J OK K OK L OK M OK N OK O OK P OK Q OK R OK S OK T OK U OK V OK W OK X OK Y OK Z OK a OK b OK c OK d OK e OK f OK g OK h OK i OK j OK k OK l OK m OK n OK o OK p OK q OK r OK s OK t OK u OK v OK w OK x OK y OK z OK Process completed successfully


Comment on Re: Efficiency of map vs. more verbose basic/fundamental code
Select or Download Code
Re^2: Efficiency of map vs. more verbose basic/fundamental code
by marquezc329 (Scribe) on Oct 05, 2012 at 03:56 UTC
    Can you explain what you mean by "apples to apples"? You're not the first to mention it. I honestly have never run a benchmark before and did it on the code that was in the passage only to test the statement the author made about the comparative efficiency.
    I was thinking that if both pieces of code did the exact same thing and I benchmarked them I could figure out if speed was being sacrificed by the use of the more brief coding technique. BrowserUk explained that the speed on this particular operation was trivial, but the sacrifice could potentially be in memory if we were to apply this procedure on a larger scale. So I thought I had my answer. Am I approaching this problem in the wrong way? In this case what are my apples and oranges and how would I go about doing a proper benchmark? I apologize if I'm asking too many questions.

      Two important things in running a benchmark are:

      1) Make sure your subroutines are doing the same thing with the same data. I made the mistake once of setting up the data outside the subroutine and then editing it in place, so that the second routine to run was working with different data than the first. Make sure each one starts with the same thing and has the same result.

      2) Avoid anything that adds overhead which could overshadow the time used by the function that you're actually trying to benchmark. If you're comparing for and map, just include them and as little else as possible. Especially avoid any system calls, IO like printing, etc. Print the results once to make sure your outputs match, but then remove those statements. A difference of nanoseconds won't show through the random noise if you're calling functions that take milliseconds.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

        I like this answer.
        In recent Perl, a "for" and a "map" statement will be almost identical in performance.

        Disk I/O statements especially can confuse benchmarks. That is because the Disk/File System has caching algorithms. I have one program that takes 7 seconds for the first search. The subsequent searches take like <1 second.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997364]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-09-02 00:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls