File: mouse.pl

package info (click to toggle)
libhash-fieldhash-perl 0.15-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 1,249; ansic: 207; makefile: 10
file content (85 lines) | stat: -rw-r--r-- 1,758 bytes parent folder | download | duplicates (7)
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
#!perl -w
use strict;
use Benchmark qw(:all);

BEGIN{
	package M;
	use Mouse;

	has foo => (
		is => 'rw',
	);
	has bar => (
		is => 'rw',
	);
	has baz => (
		is => 'rw',
	);
	__PACKAGE__->meta->make_immutable;
}
BEGIN{
	package HF;
	use Hash::FieldHash qw(:all);

	fieldhash my %foo => 'foo';
	fieldhash my %bar => 'bar';
	fieldhash my %baz => 'baz';
	sub new{ my $o; bless \$o, shift }

#	sub new{
#		my $class = shift;
#		my $self  = bless do{ \my $o }, $class;
#		return Hash::FieldHash::from_hash($self, @_);
#	}
}
BEGIN{
	package HUF;
	use Hash::Util::FieldHash::Compat qw(:all);
	fieldhashes \my(%foo, %bar, %baz);

	sub new{ my $o; bless \$o, shift }

	sub foo{
		@_ > 1 ? ($foo{$_[0]} = $_[1]) : $foo{$_[0]}
	}
	sub bar{
		@_ > 1 ? ($bar{$_[0]} = $_[1]) : $bar{$_[0]}
	}
	sub baz{
		@_ > 1 ? ($baz{$_[0]} = $_[1]) : $baz{$_[0]}
	}
}
printf "Perl %vd on $^O\n", $^V;

foreach my $count(1, 100){
	print "new, and access(read:write 11:3)*$count\n";
	cmpthese timethese -1 => {
		'H::F' => sub{
			my $o = HF->new();
			for(1 .. $count){
				$o->foo($_);
				$o->bar($o->foo + $o->foo + $o->foo + $o->foo + $o->foo);
				$o->baz($o->bar + $o->bar + $o->bar + $o->bar + $o->bar);
				$o->baz == ($_ * 5 * 5) or die $o->baz;
			}
		},
		'H::U::F' => sub{
			my $o = HUF->new();
			for(1 .. $count){
				$o->foo($_);
				$o->bar($o->foo + $o->foo + $o->foo + $o->foo + $o->foo);
				$o->baz($o->bar + $o->bar + $o->bar + $o->bar + $o->bar);
				$o->baz == ($_ * 5 * 5) or die $o->baz;
			}
		},
		'Mouse' => sub{
			my $o = M->new();
			for(1 .. $count){
				$o->foo($_);
				$o->bar($o->foo + $o->foo + $o->foo + $o->foo + $o->foo);
				$o->baz($o->bar + $o->bar + $o->bar + $o->bar + $o->bar);
				$o->baz == ($_ * 5 * 5) or die $o->baz;
			}
		},
	};
}