in reply to Remove line from file based on numeric column value

I wondered whether using abs to get absolute value then making one comparison rather than two would be quicker. Apparently not.

use strict; use warnings; use Benchmark qw{ cmpthese }; my @data = map { rand( 20 ) * ( 1, -1 )[ int rand 2 ] } 1 .. 1e6; cmpthese( -10, { abs => sub { my @arr = grep { abs $_ <= 10 } @data }, two => sub { my @arr = grep { $_ >= -10 and $_ <= 10 } @data }, } );
Rate abs two abs 5.11/s -- -9% two 5.62/s 10% --

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Remove line from file based on numeric column value
by Bama_Perl (Acolyte) on May 25, 2015 at 23:29 UTC
    Ha, yeah, it doesn't look like too big of a difference in terms of computation time. Interesting.