in reply to Request for further enlightenment

Heres a good way to test:
#!/usr/bin/perl -w use Benchmark ; use strict; my $count = shift || "-3" ; # set the test counter my @array = (); #load up an array of crap for ( my $i = 0; $i < 1000; $i++) { push @array, rand(256); } open NULL, "+/dev/null" or die "$!"; timethese ( $count, { 'join' => '&print_join', 'map' => '&print_map', 'field sep' => '&print_field' } ); # use join to print each array elemnet. sub print_join { print NULL join("\n", @array) ; } # use map to print each array elemnt, sub print_map { print NULL map{ $_ .= "\n"} @array ; } # use map to print each array elemnt, sub print_field { local $,="\n"; print NULL @array ; }
Ran for default 3 CPU seconds:
./time_print 
Benchmark: running field sep, join, map, each for at least 3 CPU seconds...
 field sep:  3 wallclock secs ( 3.15 usr +  0.00 sys =  3.15 CPU) @ 53645.71/s (n=168984)
      join:  3 wallclock secs ( 3.05 usr +  0.01 sys =  3.06 CPU) @ 1256.86/s (n=3846)
       map:  5 wallclock secs ( 4.56 usr +  0.04 sys =  4.60 CPU) @ 119.78/s (n=551)
looks like the field sepperator is the fastest with 53,645.71 /sec
hope this helps

Replies are listed 'Best First'.
Re: Re: Request for further enlightenment
by Juerd (Abbot) on Jun 05, 2002 at 08:06 UTC

    You are being unfair to map.

    #!/usr/bin/perl -w use Benchmark; use strict; my $count = shift || -3 ; # set the test counter my @array; #load up an array of crap push @array, rand(256) for 1 .. 1000; open NULL, ">/dev/null" or die $!; timethese ( $count, { 'join' => \&print_join, 'map' => \&print_map, 'list map' => \&print_map_list, 'field sep' => \&print_field, 'for' => \&print_for } ); # use join to print each array elemnet. sub print_join { print NULL join("\n", @array); } # use map to print each array elemnt, sub print_map { print NULL map{ $_ .= "\n"} @array; } # same but a no concat sub print_map_list { print NULL map { ($_, "\n") } @array; } # use map to print each array elemnt, sub print_field { local $, = "\n"; print NULL @array; } # use a loop. sub print_for { print NULL $_, "\n" for @array; }
    Yields:
    Benchmark: running field sep, for, join, list map, map, each for at least 9 CPU seconds...
     field sep: 11 wallclock secs ( 9.60 usr +  0.05 sys =  9.65 CPU) @ 1121.04/s (n=10818)
    
           for: 11 wallclock secs ( 9.28 usr +  0.02 sys =  9.30 CPU) @ 621.40/s (n=5779)
          join: 11 wallclock secs ( 9.20 usr +  0.02 sys =  9.22 CPU) @ 2888.07/s (n=26628)
      list map: 11 wallclock secs ( 9.07 usr +  0.04 sys =  9.11 CPU) @ 268.94/s (n=2450)
           map: 32 wallclock secs (26.02 usr +  0.26 sys = 26.28 CPU) @ 57.95/s (n=1523)
    
    What happened with your field sep test? I can't get it as fast as you claim it to be. You should also note that join does not put a newline after the final item.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Still unfair...
        
      sub print_map_interp { print NULL map "$_\n", @array; } ### RESULTS ### Benchmark: running field sep, for, interp map, join, list map, map, ea +ch for at least 3 CPU seconds... field sep: 3 wallclock secs ( 3.09 usr + 0.05 sys = 3.14 CPU) @ 11 +76.75/s (n=3695) for: 3 wallclock secs ( 3.14 usr + 0.03 sys = 3.17 CPU) @ 81 +4.63/s (n=2584) interp map: 3 wallclock secs ( 3.09 usr + 0.02 sys = 3.11 CPU) @ 74 +5.90/s (n=2319) join: 4 wallclock secs ( 3.05 usr + 0.17 sys = 3.22 CPU) @ 36 +80.02/s (n=11846) list map: 3 wallclock secs ( 3.00 usr + 0.02 sys = 3.02 CPU) @ 44 +6.62/s (n=1347) map: 6 wallclock secs ( 6.19 usr + 0.17 sys = 6.36 CPU) @ 61 +.17/s (n=389) Tool completed successfully
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
      I lookoed at the code for using the field sep and it tests fine. I ran the program a few more times:
       ./time_print 
      Benchmark: running field sep, join, map, each for at least 3 CPU seconds...
       field sep:  4 wallclock secs ( 3.15 usr +  0.01 sys =  3.16 CPU) @ 53475.95/s (n=168984)
            join:  4 wallclock secs ( 3.15 usr +  0.00 sys =  3.15 CPU) @ 1300.00/s (n=4095)
             map:  3 wallclock secs ( 3.12 usr +  0.00 sys =  3.12 CPU) @ 302.88/s (n=945)
      
      
      each time on my maching the field sep is really fast.
      I changed the map to map {"$_\n"} @array for these runs