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
In reply to Re: Re (2): Hash/Array of Regular Expressions? (code)
by Aighearach
in thread Hash/Array of Regular Expressions?
by irom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |