… | |
… | |
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 | |
|
|
15 | sub a { |
13 | sub b { |
16 | $old = $current; |
14 | my ($self) = shift; |
17 | $current = $_[0]; |
15 | $self->{b} = shift if @_; |
|
|
16 | $self->{b}; |
18 | } |
17 | } |
19 | |
18 | |
20 | $b = new Coro sub { |
19 | $b = async { |
21 | # do a little unrolling... |
20 | # do a little unrolling... |
22 | while() { |
21 | while() { |
23 | $Coro::main->resume; $Coro::main->resume; $Coro::main->resume; |
22 | yield; yield; yield; yield; yield; |
24 | } |
23 | } |
25 | }; |
24 | }; |
26 | |
25 | |
27 | $b->resume; # the first resume is slow because it allocates all the memory |
26 | yield; |
28 | |
27 | |
29 | $main = $Coro::main; |
28 | $main = $Coro::main; |
30 | |
29 | |
31 | #*transfer = \&Coro::_transfer; |
30 | *transfer = \&Coro::State::transfer; |
32 | sub transfer { Coro::_transfer($_[0], $_[1]) } |
|
|
33 | |
31 | |
34 | $c = Coro::_newprocess { |
32 | sub doit { |
35 | while() { |
33 | while() { |
|
|
34 | # some unrolling here as well.. |
36 | transfer($c, $main); transfer($c, $main); transfer($c, $main); |
35 | transfer($c, $main); transfer($c, $main); |
|
|
36 | transfer($c, $main); transfer($c, $main); |
|
|
37 | transfer($c, $main); transfer($c, $main); |
37 | } |
38 | } |
|
|
39 | } |
|
|
40 | |
|
|
41 | $c = Coro::State::_newprocess [sub { |
|
|
42 | doit(1,2,3,4,5,6,7,8,9); |
38 | }; |
43 | }]; |
39 | |
44 | |
40 | transfer($main, $c); |
45 | transfer($main, $c); |
41 | |
46 | |
42 | timethese 100000, { |
47 | timethese 100000, { |
43 | method => '$a->a; $a->a; $a->a; $a->a', |
48 | empty => '&a; &a', |
44 | resume => '$b->resume; $b->resume', |
49 | method => '$a->b(5); $a->b(6)', |
|
|
50 | yield => 'yield', |
45 | transfer => 'transfer($main, $c); transfer($main, $c)', |
51 | transfer => 'transfer($main, $c)', |
46 | }; |
52 | }; |
|
|
53 | |