#! /usr/local/bin/perl use strict; use warnings; use Benchmark 'cmpthese'; my @s1 = (999, -999, map {rand(900)-900} 0..10); my @s2 = (999, -999, map {rand(900)-900} 0..100); my @s3 = (999, -999, map {rand(900)-900} 0..1000); my @s4 = (999, -999, map {rand(900)-900} 0..10000); my @s5 = (999, -999, map {rand(900)-900} 0..100000); my @s6 = (999, -999, map {rand(900)-900} 0..1000000); sub with_scan { my ( $min, $max ) = @_; for my $element ( @_ ) { $min = $element if $min > $element; $max = $element if $max < $element; } return ($min, $max); } sub with_sort { return (sort {$a <=> $b} @_)[0,-1]; } print join(',', with_scan(@s1)), $/; print join(',', with_sort(@s1)), $/; cmpthese( -3, { 'with_sort_1' => sub {with_sort(@s1)}, 'with_scan_1' => sub {with_scan(@s1)}, } ); cmpthese( -3, { 'with_sort_2' => sub {with_sort(@s2)}, 'with_scan_2' => sub {with_scan(@s2)}, } ); cmpthese( -3, { 'with_sort_3' => sub {with_sort(@s3)}, 'with_scan_3' => sub {with_scan(@s3)}, } ); cmpthese( -3, { 'with_sort_4' => sub {with_sort(@s4)}, 'with_scan_4' => sub {with_scan(@s4)}, } ); cmpthese( -3, { 'with_sort_5' => sub {with_sort(@s5)}, 'with_scan_5' => sub {with_scan(@s5)}, } ); cmpthese( -15, { 'with_sort_6' => sub {with_sort(@s6)}, 'with_scan_6' => sub {with_scan(@s6)}, } );