Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Re (2): Hash/Array of Regular Expressions? (code)

by Aighearach (Initiate)
on Jun 25, 2001 at 01:37 UTC ( [id://91117]=note: print w/replies, xml ) Need Help??


in reply to Re (2): Hash/Array of Regular Expressions? (code)
in thread Hash/Array of Regular Expressions?

Map has lower overhead than many other list changing algorithms... this is mostly because it uses better, faster, fewer temporary variables. Lets comapare using our good old friend Devel::OpProf.

#!/usr/bin/perl use warnings; use strict; use Devel::OpProf qw'profile print_stats zero_stats'; my @source = ( 1..10_000 ); my @dest = (); #measure the map profile(1); @dest = map { $_ * 10 } @source; profile(0); print "*** map ***\n"; print_stats(); zero_stats(); @dest = (); #measure the foreach profile(1); foreach(@source){ @dest = $_ * 10; } profile(0); print "\n*** foreach ***\n"; print_stats(); zero_stats(); @dest = (); #measure the for profile(1); push @dest, $_ * 10 for @source; profile(0); print "\n*** for ***\n"; print_stats();

The output:

*** map ***
null operation           10005
constant item            10001
scalar variable          10000
map iterator             10000
multiplication (*)       10000
block                    10000
pushmark                 4
next statement           2
private array            2
list assignment          1
map                      1
subroutine entry         1
glob value               1

*** foreach ***
null operation           20005
pushmark                 20002
next statement           20002
glob value               10002
logical and (&&)         10001
private array            10001
constant item            10001
foreach loop iterator    10001
iteration finalizer      10000
multiplication (*)       10000
scalar dereference       10000
list assignment          10000
foreach loop entry       1
subroutine entry         1
loop exit                1

*** for ***
next statement           10003
glob value               10002
pushmark                 10002
logical and (&&)         10001
private array            10001
constant item            10001
foreach loop iterator    10001
multiplication (*)       10000
push                     10000
iteration finalizer      10000
scalar dereference       10000
null operation           5
foreach loop entry       1
subroutine entry         1
loop exit                1

So we see that, a map has less action than a foreach, and stuffing the for in the push is almost as good as a map, and with many of the same operations going on.
--
Snazzy tagline here

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-23 21:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found