Created
October 18, 2019 00:09
-
-
Save vrurg/aa7eb27dd56b6faa898062fe4a0ec5fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Bench; | |
my $rcnt = 0; | |
my $wcnt = 0; | |
class C { | |
has $.a is rw; | |
has $.b is rw; | |
submethod TWEAK { | |
my $a; | |
$!a := Proxy.new( | |
FETCH => -> $ { ++$rcnt; $a }, | |
STORE => -> $, $val { ++$wcnt; $a = $val }, | |
); | |
} | |
method foo { | |
$!a = 42; | |
$!b = 24; | |
} | |
} | |
my $inst = C.new; | |
$inst.foo; | |
my $b = Bench.new; | |
my %tests = | |
r_proxy => sub { | |
my $v = $inst.a; | |
}, | |
r_noproxy => sub { | |
my $v = $inst.b; | |
}, | |
w_proxy => sub { | |
$inst.a = 42; | |
}, | |
w_noproxy => sub { | |
$inst.b = 24; | |
}, | |
; | |
my %res; | |
for %tests.keys.sort -> $tst { | |
my $t = now; | |
for ^5000000 { | |
%tests{$tst}.(); | |
} | |
say $tst, ": ", (%res{$tst} = now - $t); | |
} | |
say "read ratio : ", %res<r_proxy> / %res<r_noproxy>; | |
say "write ratio: ", %res<w_proxy> / %res<w_noproxy>; | |
note "Counters: read:", $rcnt, " write:", $wcnt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment