… | |
… | |
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 | |
|
|
14 | sub a { |
15 | sub b { |
15 | $n++; # do something to taint the benchmark results ;) |
16 | my ($self) = shift; |
|
|
17 | $self->{b} = shift if @_; |
|
|
18 | $self->{b}; |
16 | } |
19 | } |
17 | |
20 | |
18 | $b = async { |
21 | $b = async { |
19 | # do a little unrolling... |
22 | # do a little unrolling... |
20 | while() { |
23 | while() { |
21 | yield; yield; yield; yield; yield; |
24 | cede; cede; cede; cede; cede; |
|
|
25 | cede; cede; cede; cede; cede; |
22 | } |
26 | } |
23 | }; |
27 | }; |
24 | |
28 | |
25 | yield; |
29 | cede; |
26 | |
|
|
27 | $main = $Coro::main; |
|
|
28 | |
30 | |
29 | *transfer = \&Coro::State::transfer; |
31 | *transfer = \&Coro::State::transfer; |
30 | |
32 | |
31 | $c = Coro::State::_newprocess [sub { |
33 | sub doit0 { |
32 | while() { |
34 | while() { |
33 | # some unrolling here as well.. |
35 | # some unrolling here as well.. |
34 | transfer($c, $main); transfer($c, $main); |
36 | transfer($c0, $main); transfer($c0, $main); |
35 | transfer($c, $main); transfer($c, $main); |
37 | transfer($c0, $main); transfer($c0, $main); |
36 | transfer($c, $main); transfer($c, $main); |
38 | transfer($c0, $main); transfer($c0, $main); |
|
|
39 | transfer($c0, $main); transfer($c0, $main); |
37 | } |
40 | } |
38 | }]; |
41 | } |
39 | |
42 | |
40 | transfer($main, $c); |
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 | transfer($c1, $main); transfer($c1, $main); |
|
|
50 | } |
|
|
51 | } |
41 | |
52 | |
42 | timethese 100000, { |
53 | $c0 = new Coro::State sub { |
43 | method => '$a->a; $a->a', |
54 | doit0(1,2,3,4,5,6,7,8,9); |
44 | resume => 'yield', |
|
|
45 | transfer => 'transfer($main, $c)', |
|
|
46 | }; |
55 | }; |
47 | |
56 | |
|
|
57 | $c1 = new Coro::State sub { |
|
|
58 | doit1(1,2,3,4,5,6,7,8,9); |
|
|
59 | }; |
|
|
60 | |
|
|
61 | #$c0->save (0); |
|
|
62 | #$c1->save (-1); |
|
|
63 | |
|
|
64 | #Coro::State::enable_times 1; |
|
|
65 | #use Coro::Debug; Coro::Debug::command "ps";#d# |
|
|
66 | #(async { |
|
|
67 | $main = $Coro::current; |
|
|
68 | |
|
|
69 | transfer($main, $c0); |
|
|
70 | transfer($main, $c1); |
|
|
71 | |
|
|
72 | timethese 5000000, { |
|
|
73 | function => 'a(5); a(6)', |
|
|
74 | method => '$a->b(5); $a->b(6)', |
|
|
75 | cede => 'cede', |
|
|
76 | transfer0 => 'transfer($main, $c0)', |
|
|
77 | transfer1 => 'transfer($main, $c1)', |
|
|
78 | }; |
|
|
79 | #})->join; |
|
|
80 | #use Coro::Debug; Coro::Debug::command "ps";#d# |
|
|
81 | |
|
|
82 | |