use strict; use warnings; use Benchmark qw/cmpthese/; use Test::More tests => 4; my @DATA = map { int(-100 + rand 200) } 1 ... 1000; my $g=use_grep(@DATA); diag 'grep => ', explain $g; my $c=use_comparison_ops(@DATA); diag 'comparison_ops => ', explain $c; my $s=use_spaceship_op(@DATA); diag 'spaceship_op => ', explain $s; my $r=use_hiyesthanks_regex(@DATA); diag '[hiyesthanks] regex => ', explain $r; my $m=use_marshall_regex(@DATA); diag '[Marshall] regex => ', explain $m; is_deeply $c, $g, 'verify comparison operators give same results as grep'; is_deeply $s, $g, 'verify spaceship operators give same results as grep'; is_deeply $r, $g, 'verify [hiyesthanks] regex give same results as grep'; is_deeply $m, $g, 'verify [Marshall] regex give same results as grep'; cmpthese( 0, { use_grep => sub { use_hiyesthanks_regex(@DATA) }, use_hiyesthanks_regex => sub { use_hiyesthanks_regex(@DATA) }, use_marshall_regex => sub { use_marshall_regex(@DATA) }, use_comparison_ops => sub { use_comparison_ops(@DATA) }, use_spaceship_op => sub { use_spaceship_op(@DATA) }, }); sub use_hiyesthanks_regex { my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $count_of_others ) = ( 0, 0, 0, 0 ); for my $num ( @_ ) { if ( $num =~ /^[0].{0}/ ) { $count_of_zeros++; } elsif ( $num =~ /^\d[0-9]{1,3}$/ ) { $count_of_positives++; } else { $count_of_negatives++; } } return { count_of_positives => $count_of_positives, count_of_negatives => $count_of_negatives, count_of_zeros => $count_of_zeros, count_of_others => $count_of_others, total_count => $count_of_positives + $count_of_negatives + $count_of_zeros + $count_of_others, }; } sub use_marshall_regex { my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $count_of_others ) = ( 0, 0, 0, 0 ); for my $num ( @_ ) { if ( $num =~ /^0$/ ) { $count_of_zeros++; } elsif ( $num =~ /^\d+$/ ) { $count_of_positives++; } elsif ( $num =~ /^-\d+$/ ) { $count_of_negatives++; } else { $count_of_others++; } } return { count_of_positives => $count_of_positives, count_of_negatives => $count_of_negatives, count_of_zeros => $count_of_zeros, count_of_others => $count_of_others, total_count => $count_of_positives + $count_of_negatives + $count_of_zeros + $count_of_others, }; } sub use_comparison_ops { my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $count_of_others ) = ( 0, 0, 0, 0 ); for my $num ( @_ ) { if ( $num < 0 ) { $count_of_negatives++; } elsif ( $num > 0 ) { $count_of_positives++; } else { $count_of_zeros++; } } return { count_of_positives => $count_of_positives, count_of_negatives => $count_of_negatives, count_of_zeros => $count_of_zeros, count_of_others => $count_of_others, total_count => $count_of_positives + $count_of_negatives + $count_of_zeros + $count_of_others, }; } sub use_spaceship_op { my ( $count_of_positives, $count_of_negatives, $count_of_zeros, $count_of_others ) = ( 0, 0, 0, 0 ); for my $num ( @_ ) { for( $num <=> 0 ) { $_ == -1 && do {$count_of_negatives++; next;}; $_ == +1 && do {$count_of_positives++; next;}; $_ == 0 && do {$count_of_zeros++; next;}; $count_of_others++; } } return { count_of_positives => $count_of_positives, count_of_negatives => $count_of_negatives, count_of_zeros => $count_of_zeros, count_of_others => $count_of_others, total_count => $count_of_positives + $count_of_negatives + $count_of_zeros + $count_of_others, }; } sub use_grep { my $count_of_positives = scalar grep { $_ > 0 } @_; my $count_of_negatives = scalar grep { $_ < 0 } @_; my $count_of_zeros = scalar grep { $_ == 0 } @_; my $count_of_others = scalar(@_) - ($count_of_positives + $count_of_negatives + $count_of_zeros); return { count_of_positives => $count_of_positives, count_of_negatives => $count_of_negatives, count_of_zeros => $count_of_zeros, count_of_others => $count_of_others, total_count => $count_of_positives + $count_of_negatives + $count_of_zeros + $count_of_others, }; }