File: arrayref.pl

package info (click to toggle)
libmousex-nativetraits-perl 1.09-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 480 kB
  • sloc: perl: 5,073; makefile: 7
file content (65 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download | duplicates (4)
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
#!perl -w

use strict;
use Benchmark qw(:all);

print "Benchmark for native traits (Array)\n";

{
    package MouseStack;
    use Mouse;

    has stack => (
        is  => 'rw',
        isa => 'ArrayRef',

        traits => ['Array'],

        handles => {
            pop      => 'pop',
            push     => 'push',
            top      => [ get => -1 ],
            is_empty => 'is_empty',
        },
        default => sub{ [] },
    );

    __PACKAGE__->meta->make_immutable();
}

{
    package MooseStack;
    use Moose;

    has stack => (
        is  => 'rw',
        isa => 'ArrayRef',

        traits => ['Array'],

        handles => {
            pop      => 'pop',
            push     => 'push',
            top      => [ get => -1 ],
            is_empty => 'is_empty',
        },
        default => sub{ [] },
    );

    __PACKAGE__->meta->make_immutable();
}

my $mouse = MouseStack->new;
my $moose = MooseStack->new;

print "push && pop && is_empty\n";
cmpthese -1 => {
    Mouse => sub{
        $mouse->push($_) for 1 .. 100;
        $mouse->pop()    until $mouse->is_empty;
    },
    Moose => sub{
        $moose->push($_) for 1 .. 100;
        $moose->pop()    until $moose->is_empty;
    },
};