File: performance.t

package info (click to toggle)
libarchive-tar-wrapper-perl 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 372 kB
  • sloc: perl: 782; makefile: 2
file content (160 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use warnings;
use strict;
use File::Temp qw(tempfile tempdir);
use Test::More tests => 1;
use lib 'xt/lib';
use ATWDumbbench;
use constant BATCH_SIZE => 1000;

my $bench = ATWDumbbench->new(
    target_rel_precision => 0.005,
    initial_runs         => BATCH_SIZE,
);

my $dir      = tempdir( CLEANUP => 1 );
my $template = 'foobar-XXXXXXXX';
for ( 1 .. BATCH_SIZE ) {
    my ( $fh, $filename ) = tempfile( $template, DIR => $dir );
    print $fh rand();
    close($fh);
}

my $regex = qr/^\.\.?$/;

$bench->add_instances(
    Dumbbench::Instance::PerlSub->new(
        name => 'with grep',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @top_entries = grep { $_ !~ /^\.\.?$/ } readdir $dir_h;
            closedir($dir_h);
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'with grep and compiled regex',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @top_entries = grep { $_ !~ $regex } readdir $dir_h;
            closedir($dir_h);
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'with eq',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @temp = readdir($dir_h);
            closedir($dir_h);
            my @top_entries;
            for (@temp) {
                next if ( ( $_ eq '.' ) or ( $_ eq '..' ) );
                push( @top_entries, $_ );
            }
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'with enhanced eq',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @temp = readdir($dir_h);
            closedir($dir_h);
            my @top_entries;
            for (@temp) {
                next
                  if (  ( length($_) <= 2 )
                    and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) );
                push( @top_entries, $_ );
            }
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'with enhanced eq mark2',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @temp = readdir($dir_h);
            closedir($dir_h);
            my @top_entries;
            my $counter = 0;
            my $found   = 0;

            for (@temp) {
                $counter++;

                if (    ( length($_) <= 2 )
                    and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) )
                {
                    $found++;
                    if ( $found < 2 ) {
                        next;
                    }
                    else {
                        push( @top_entries, splice( @temp, $counter ) );
                        last;
                    }

                }
                push( @top_entries, $_ );
            }
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'with enhanced eq mark3',
        code => sub {
            opendir my $dir_h, $dir or die "Cannot open $dir: $!";
            my @temp = readdir($dir_h);
            closedir($dir_h);
            my ( $first, $second );
            my $index = 0;
            my $found = 0;

            for (@temp) {

                if (    ( length($_) <= 2 )
                    and ( ( $_ eq '.' ) or ( $_ eq '..' ) ) )
                {
                    if ( $found < 1 ) {
                        $first = $index;
                        $found++;
                        $index++;
                        next;
                    }
                    else {
                        $second = $index;
                        last;
                    }

                }
                else {
                    $index++;
                }
            }

            splice( @temp, $first,          1 );
            splice( @temp, ( $second - 1 ), 1 );
        }
    ),
    Dumbbench::Instance::PerlSub->new(
        name => 'sort and shift',
        code => sub {
            opendir( my $dir_h, $dir ) or die "Cannot open $dir: $!";
            my @top_entries = readdir($dir_h);
            closedir($dir_h);
            @top_entries = sort(@top_entries);

            # removing the '.' and '..' entries
            shift(@top_entries);
            shift(@top_entries);
        }
    ),
);

note('This will take a while... hang on.');
$bench->run;
diag( $bench->report_as_text );
my $method_name = 'with enhanced eq mark3';

cmp_ok(
    $bench->measurements(),
    '<=',
    $bench->get_measure($method_name),
    "'$method_name' is the fastest method."
);