File: 95benchmark.t

package info (click to toggle)
liblist-keywords-perl 0.11-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 200 kB
  • sloc: perl: 467; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

# This "test" never fails, but prints a benchmark comparison between these
# util functions and the ones provided by List::Util

use Time::HiRes qw( gettimeofday tv_interval );
sub measure(&)
{
   my ( $code ) = @_;
   my $start = [ gettimeofday ];
   $code->();
   return tv_interval $start;
}

my @nums = ( 1 .. 100 );

my $COUNT = 10_000;

my $LK_elapsed = 0;
my $LU_elapsed = 0;

# To reduce the influence of bursts of timing noise, interleave many small runs
# of each type.

foreach ( 1 .. 20 ) {
   my $overhead = measure {};

   $LK_elapsed += -$overhead + measure {
      use List::Keywords 'first';
      my $ret;
      ( $ret = first { $_ > 50 } @nums ) for 1 .. $COUNT;
   };
   $LU_elapsed += -$overhead + measure {
      use List::Util 'first';
      my $ret;
      ( $ret = first { $_ > 50 } @nums ) for 1 .. $COUNT;
   };
}

pass( "Benchmarked" );

if( $LK_elapsed > $LU_elapsed ) {
   diag( sprintf "List::Util took %.3fsec, ** this was SLOWER at %.3fsec **",
      $LU_elapsed, $LK_elapsed );
}
else {
   my $speedup = ( $LU_elapsed - $LK_elapsed ) / $LU_elapsed;
   diag( sprintf "List::Util took %.3fsec, this was %d%% faster at %.3fsec",
      $LU_elapsed, $speedup * 100, $LK_elapsed );
}

done_testing;