Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^2: Count and List Items in a List

by ikegami (Pope)
on Nov 01, 2005 at 16:16 UTC ( #504632=note: print w/ replies, xml ) Need Help??


in reply to Re: Count and List Items in a List
in thread Count and List Items in a List

Except map is painfully slow in void context:

use strict; use warnings; use Benchmark qw( cmpthese ); my @list = map { '' . int(rand(100)) } 1..1000; sub map_test { my %counts = (); map { ++$counts{$_} } @list; 1; } sub foreach_test { my %counts = (); ++$counts{$_} for @list; 1; } cmpthese(-3, { map => \&map_test, foreach => \&foreach_test, });
This is perl, v5.6.1 built for MSWin32-x86-multi-thread Rate map foreach map 953/s -- -25% foreach 1278/s 34% --
This is perl, v5.8.0 built for MSWin32-x86-multi-thread Rate map foreach map 1705/s -- -25% foreach 2288/s 34% --
This is perl, v5.8.0 built for i386-freebsd Rate map foreach map 847/s -- -29% foreach 1190/s 40% --

This may have been fixed since.


Comment on Re^2: Count and List Items in a List
Select or Download Code
Re^3: Count and List Items in a List
by Tanktalus (Canon) on Nov 01, 2005 at 17:51 UTC
    This is perl, v5.8.6 built for i686-linux Rate foreach map foreach 47168/s -- -2% map 48260/s 2% --

    That said, I always prefer code that does what it says and says what it does. To me, for/foreach evaluates code foreach element in a list, while map maps (or transforms) one list into another. If you want to iterate over a list, use for or foreach. If you want to transform from one list to another, use map.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-09-19 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (140 votes), past polls