in reply to Re: Request for further enlightenment
in thread Request for further enlightenment

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.

Replies are listed 'Best First'.
(MeowChow) Re3: Request for further enlightenment
by MeowChow (Vicar) on Jun 05, 2002 at 08:21 UTC
    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
Re: Re: Re: Request for further enlightenment
by yodabjorn (Monk) on Jun 05, 2002 at 18:22 UTC
    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