… | |
… | |
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 | |
|
|
13 | |
|
|
14 | sub a { |
|
|
15 | } |
|
|
16 | |
14 | |
17 | sub b { |
15 | sub b { |
18 | my ($self) = shift; |
16 | my ($self) = shift; |
19 | my $num = shift; |
17 | $self->{b} = shift if @_; |
20 | return $num+1; |
18 | $self->{b}; |
21 | } |
19 | } |
22 | |
20 | |
23 | $b = async { |
21 | $b = async { |
24 | # do a little unrolling... |
22 | # do a little unrolling... |
25 | while() { |
23 | while() { |
26 | yield; yield; yield; yield; yield; |
24 | cede; cede; cede; cede; cede; |
27 | } |
25 | } |
28 | }; |
26 | }; |
29 | |
27 | |
30 | yield; |
28 | cede; |
31 | |
29 | |
32 | $main = $Coro::main; |
30 | $main = $Coro::main; |
33 | |
31 | |
34 | *transfer = \&Coro::State::transfer; |
32 | *transfer = \&Coro::State::transfer; |
35 | |
33 | |
36 | sub doit { |
34 | sub doit0 { |
37 | while() { |
35 | while() { |
38 | # some unrolling here as well.. |
36 | # some unrolling here as well.. |
39 | transfer($c, $main); transfer($c, $main); |
37 | transfer($c0, $main); transfer($c0, $main); |
40 | transfer($c, $main); transfer($c, $main); |
38 | transfer($c0, $main); transfer($c0, $main); |
41 | transfer($c, $main); transfer($c, $main); |
39 | transfer($c0, $main); transfer($c0, $main); |
42 | } |
40 | } |
43 | } |
41 | } |
44 | |
42 | |
|
|
43 | sub doit1 { |
|
|
44 | while() { |
|
|
45 | # some unrolling here as well.. |
|
|
46 | transfer($c1, $main); transfer($c1, $main); |
|
|
47 | transfer($c1, $main); transfer($c1, $main); |
|
|
48 | transfer($c1, $main); transfer($c1, $main); |
|
|
49 | } |
|
|
50 | } |
|
|
51 | |
45 | $c = Coro::State::_newprocess [sub { |
52 | $c0 = Coro::State::_newprocess [sub { |
46 | doit(1,2,3,4,5,6,7,8,9); |
53 | doit0(1,2,3,4,5,6,7,8,9); |
47 | }]; |
54 | }]; |
48 | |
55 | |
|
|
56 | $c1 = Coro::State::_newprocess [sub { |
|
|
57 | doit1(1,2,3,4,5,6,7,8,9); |
|
|
58 | }]; |
|
|
59 | |
49 | transfer($main, $c); |
60 | transfer($main, $c0); |
|
|
61 | transfer($main, $c1); |
50 | |
62 | |
51 | timethese 100000, { |
63 | timethese 100000, { |
52 | empty => '&a; &a', |
64 | empty => '&a; &a', |
53 | method => '$a->b(5); $a->b(6)', |
65 | method => '$a->b(5); $a->b(6)', |
54 | yield => 'yield', |
66 | cede => 'cede', |
|
|
67 | transfer0=> 'transfer($main, $c0, 0)', |
55 | transfer => 'transfer($main, $c)', |
68 | transfer1=> 'transfer($main, $c1)', |
56 | }; |
69 | }; |
57 | |
70 | |