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