… | |
… | |
6 | # call with two calls of transfer in a loop. |
6 | # call with two calls of transfer in a loop. |
7 | |
7 | |
8 | use Coro; |
8 | use Coro; |
9 | use Benchmark; |
9 | use Benchmark; |
10 | |
10 | |
|
|
11 | sub a() { } |
|
|
12 | |
11 | $a = bless {}, main::; |
13 | $a = bless {}, main::; |
12 | |
14 | |
13 | # do something similar, switch two global vars and return something |
|
|
14 | |
|
|
15 | sub a { |
15 | sub b { |
16 | $old = $current; |
16 | my ($self) = shift; |
17 | $current = $_[0]; |
17 | $self->{b} = shift if @_; |
|
|
18 | $self->{b}; |
18 | } |
19 | } |
19 | |
20 | |
20 | $b = new Coro sub { |
21 | $b = async { |
21 | # do a little unrolling... |
22 | # do a little unrolling... |
22 | while() { |
23 | while() { |
23 | $Coro::main->resume; $Coro::main->resume; $Coro::main->resume; |
24 | cede; cede; cede; cede; cede; |
24 | } |
25 | } |
25 | }; |
26 | }; |
26 | |
27 | |
27 | $b->resume; # the first resume is slow because it allocates all the memory |
28 | cede; |
28 | |
29 | |
29 | $main = $Coro::main; |
30 | $main = $Coro::main; |
30 | |
31 | |
31 | sub transfer { |
32 | *transfer = \&Coro::State::transfer; |
32 | Coro::_transfer($_[0], $_[1]); |
33 | |
|
|
34 | sub doit0 { |
|
|
35 | while() { |
|
|
36 | # some unrolling here as well.. |
|
|
37 | transfer($c0, $main); transfer($c0, $main); |
|
|
38 | transfer($c0, $main); transfer($c0, $main); |
|
|
39 | transfer($c0, $main); transfer($c0, $main); |
|
|
40 | } |
33 | } |
41 | } |
34 | |
42 | |
35 | $c = Coro::_newprocess { |
43 | sub doit1 { |
36 | while() { |
44 | while() { |
|
|
45 | # some unrolling here as well.. |
37 | transfer($c, $main); transfer($c, $main); transfer($c, $main); |
46 | transfer($c1, $main); transfer($c1, $main); |
|
|
47 | transfer($c1, $main); transfer($c1, $main); |
|
|
48 | transfer($c1, $main); transfer($c1, $main); |
38 | } |
49 | } |
|
|
50 | } |
|
|
51 | |
|
|
52 | $c0 = Coro::State::_newprocess [sub { |
|
|
53 | doit0(1,2,3,4,5,6,7,8,9); |
|
|
54 | }]; |
|
|
55 | |
|
|
56 | $c1 = Coro::State::_newprocess [sub { |
|
|
57 | doit1(1,2,3,4,5,6,7,8,9); |
|
|
58 | }]; |
|
|
59 | |
|
|
60 | transfer($main, $c0); |
|
|
61 | transfer($main, $c1); |
|
|
62 | |
|
|
63 | timethese 100000, { |
|
|
64 | empty => '&a; &a', |
|
|
65 | method => '$a->b(5); $a->b(6)', |
|
|
66 | cede => 'cede', |
|
|
67 | transfer0=> 'transfer($main, $c0, 0)', |
|
|
68 | transfer1=> 'transfer($main, $c1)', |
39 | }; |
69 | }; |
40 | |
70 | |
41 | transfer($main, $c); |
|
|
42 | |
|
|
43 | timethese 100000, { |
|
|
44 | method => '$a->a; $a->a; $a->a; $a->a', |
|
|
45 | resume => '$b->resume; $b->resume', |
|
|
46 | transfer => 'transfer($main, $c); transfer($main, $c)', |
|
|
47 | }; |
|
|