File: benchmark.pl

package info (click to toggle)
libreadonlyx-perl 1.04-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: perl: 577; makefile: 2
file content (76 lines) | stat: -rw-r--r-- 2,477 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use strict;
use warnings;
use lib '../lib';
use Benchmark;
my $scalar;
{

    package constant;
    use constant CONST_SCALAR => 'Fourscore and seven years ago...';
    use constant CONST_HASH   => {key => 'value'};
    use constant CONST_ARRAY  => qw[dog cat bird fish];
    sub scalar { $scalar = CONST_SCALAR; }
    sub hash   { $scalar = CONST_HASH->{key}; }
    sub array  { $scalar = (CONST_ARRAY)[1]; }
}
{

    package normal;
    my $normal_scalar = 'Fourscore and seven years ago...';
    my %normal_hash   = (key => 'value');
    my @normal_array  = (qw[dog cat bird fish]);
    sub scalar { $scalar = $normal_scalar; }
    sub hash   { $scalar = $normal_hash{key}; }
    sub array  { $scalar = $normal_array[1]; }
}
{

    package readonly;
    use namespace::clean;
    my ($normal_scalar, %normal_hash, @normal_array);
    eval <<'END';
    use Readonly;
    Readonly::Scalar $normal_scalar => 'Fourscore and seven years ago...';
    Readonly::Hash %normal_hash     => {key => 'value'};
    Readonly::Array @normal_array   => qw[dog cat bird fish];
END
    sub scalar { $scalar = $normal_scalar; }
    sub hash   { $scalar = $normal_hash{key}; }
    sub array  { $scalar = $normal_array[1]; }
}
{

    package readonlyx;
    my ($normal_scalar, %normal_hash, @normal_array);
    eval <<'END';
    use ReadonlyX;
    Readonly::Scalar $normal_scalar => 'Fourscore and seven years ago...';
    Readonly::Hash %normal_hash     => {key => 'value'};
    Readonly::Array @normal_array   => qw[dog cat bird fish];
END
    sub scalar { $scalar = $normal_scalar; }
    sub hash   { $scalar = $normal_hash{key}; }
    sub array  { $scalar = $normal_array[1]; }
}
#
my %tests = (scalar => {const     => \&constant::scalar,
                        normal    => \&normal::scalar,
                        readonlyx => \&readonlyx::scalar,
                        readonly  => \&readonly::scalar
             },
             hash => {const     => \&constant::hash,
                      normal    => \&normal::hash,
                      readonlyx => \&readonlyx::hash,
                      readonly  => \&readonly::hash
             },
             array => {const     => \&constant::array,
                       normal    => \&normal::array,
                       readonlyx => \&readonlyx::array,
                       readonly  => \&readonly::array
             }
);
#
for my $type (keys %tests) {
    print ucfirst $type . ' ';
    timethese(5_000_000, $tests{$type});
}