… | |
… | |
8 | use Coro; |
8 | use Coro; |
9 | use Benchmark; |
9 | use Benchmark; |
10 | |
10 | |
11 | $a = bless {}, main::; |
11 | $a = bless {}, main::; |
12 | |
12 | |
13 | # do something similar, switch two global vars and return something |
|
|
14 | |
13 | |
15 | sub a { |
14 | sub a { |
16 | $old = $current; |
15 | $n++; # do something to taint the benchmark results ;) |
17 | $current = $_[0]; |
|
|
18 | } |
16 | } |
19 | |
17 | |
20 | $b = new Coro sub { |
18 | $b = async { |
21 | # do a little unrolling... |
19 | # do a little unrolling... |
22 | while() { |
20 | while() { |
23 | $Coro::main->resume; $Coro::main->resume; $Coro::main->resume; |
21 | yield; yield; yield; yield; yield; |
24 | } |
22 | } |
25 | }; |
23 | }; |
26 | |
24 | |
27 | $b->resume; # the first resume is slow because it allocates all the memory |
25 | yield; |
28 | |
26 | |
29 | $main = $Coro::main; |
27 | $main = $Coro::main; |
30 | |
28 | |
31 | sub transfer { |
29 | *transfer = \&Coro::State::transfer; |
32 | Coro::_transfer($_[0], $_[1]); |
|
|
33 | } |
|
|
34 | |
30 | |
35 | $c = Coro::_newprocess { |
31 | $c = Coro::State::_newprocess [sub { |
36 | while() { |
32 | while() { |
|
|
33 | # some unrolling here as well.. |
37 | transfer($c, $main); transfer($c, $main); transfer($c, $main); |
34 | transfer($c, $main); transfer($c, $main); |
|
|
35 | transfer($c, $main); transfer($c, $main); |
|
|
36 | transfer($c, $main); transfer($c, $main); |
38 | } |
37 | } |
39 | }; |
38 | }]; |
40 | |
39 | |
41 | transfer($main, $c); |
40 | transfer($main, $c); |
42 | |
41 | |
43 | timethese 100000, { |
42 | timethese 100000, { |
44 | method => '$a->a; $a->a; $a->a; $a->a', |
43 | method => '$a->a; $a->a', |
45 | resume => '$b->resume; $b->resume', |
44 | resume => 'yield', |
46 | transfer => 'transfer($main, $c); transfer($main, $c)', |
45 | transfer => 'transfer($main, $c)', |
47 | }; |
46 | }; |
|
|
47 | |