in reply to Re:{4} how do I line-wrap while copying to stdout?
in thread how do I line-wrap while copying to stdout?

Too efficient? Execution efficiency, programming efficiency, or maintenance efficiency? There are many types. Using a regex may not always be as fast (in many cases it is faster -- try using index and substr to find word space), but in most instances they are more maintainable and more readable and faster to write.
use Benchmark; my $n = 1000; open(STDERR,">/dev/null"); cmpthese (1000, { match => sub { local $_ = "abcde " x $n; print STDERR "$1\n" while /\G(.{1,80})/gs +; }, swap => sub { local $_ = "abcde " x $n; s/\G(.{1,80})/$1\n/gs; print STDERR $_; }, subst => sub { local $a = "abcde " x $n; $b=''; $b .= substr( $a, 0, 80, '')."\n" while l +ength($a) >80; print STDERR "$b$a"; }, });
Produces
Benchmark: timing 1000 iterations of match, subst, swap... match: 1 wallclock secs ( 0.97 usr + 0.00 sys = 0.97 CPU) @ 10 +30.93/s (n=1000) subst: 1 wallclock secs ( 0.58 usr + 0.00 sys = 0.58 CPU) @ 17 +24.14/s (n=1000) swap: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13 +33.33/s (n=1000) Rate match swap subst match 1031/s -- -23% -40% swap 1333/s 29% -- -23% subst 1724/s 67% 29% --
A substr method is faster this time, but if it gets any more complex than that a regex will do just fine. If done once per script will you notice the difference between 1700 per second and 1300 per second? Maybe.

Replies are listed 'Best First'.
Re:{6} how do I line-wrap while copying to stdout?
by jeroenes (Priest) on Apr 20, 2001 at 18:09 UTC
    Oh well, you are right about that: the difference is too low to be noticable for normal use. I was just picking up merlyn's glove ;-}.

    I just added the cmpthese stats when I saw your posting. Take a look at them. Some nitbits: I would call the swab 'insert'. But that can be done by substr as well... benchmarks coming up....

    Rate inssub regmap regex fixreg substr inssub 13.7/s -- -95% -97% -97% -97% regmap 260/s 1800% -- -34% -35% -47% regex 395/s 2784% 52% -- -1% -20% fixreg 398/s 2809% 53% 1% -- -19% substr 491/s 3483% 89% 24% 23% --
    That insert must be *really* inefficient :-)

    Jeroen
    "We are not alone"(FZ)

    Let me add the new code:

    use Benchmark; undef $/; open DATA, "/home/jeroen/texs/review/reviewnew.tex" or die $!; $str = <DATA>; open DUMP, ">/dev/null"; $result = timethese( -5, { 'regex' => sub { $a = $str; $b = ''; $b .= "$1\n" while $a=~/\G(.{1,80})/gs; print DUMP "$b"; }, 'regmap' => sub { $a = $str; $b = ''; print DUMP map "$_\n", $a=~/\G(.{1,80})/gs; }, 'fixreg'=> sub { $a = $str; $b = ''; $b .= "$1\n" while $a=~/\G(.{1,80})/gos; print DUMP "$b"; }, 'substr' => sub { $a = $str; $b=''; $b .= substr( $a, 0, 80, '')."\n" while length($a) >80; print DUMP "$b$a"; }, 'inssub' => sub { $a = $str; $idx = 0; substr( $a, $idx+=81, 0)="\n" while $idx< (length( $a) - 80 ); print DUMP "$a"; } }, 'none'); Benchmark::cmpthese($result);
      No worries. I've just had lots of people rant about how fast substr and index are (and they are). Which is true for simple cases (and some complex cases but you really don't want to be writing that kind of code do you?).