1 |
root |
1.1 |
#!/usr/bin/perl |
2 |
|
|
|
3 |
|
|
# ->resume is not exactly cheap (it saves/restores a LOT |
4 |
|
|
# of global variables), but shouldn't be slow. just to show |
5 |
|
|
# how fast it is, this little proggie compares a normal subroutine |
6 |
|
|
# call with two calls of transfer in a loop. |
7 |
|
|
|
8 |
|
|
use Coro; |
9 |
|
|
use Benchmark; |
10 |
|
|
|
11 |
|
|
$a = bless {}, main::; |
12 |
|
|
|
13 |
root |
1.6 |
sub b { |
14 |
|
|
my ($self) = shift; |
15 |
root |
1.7 |
$self->{b} = shift if @_; |
16 |
|
|
$self->{b}; |
17 |
root |
1.1 |
} |
18 |
|
|
|
19 |
root |
1.4 |
$b = async { |
20 |
root |
1.1 |
# do a little unrolling... |
21 |
|
|
while() { |
22 |
root |
1.3 |
yield; yield; yield; yield; yield; |
23 |
root |
1.1 |
} |
24 |
|
|
}; |
25 |
|
|
|
26 |
root |
1.3 |
yield; |
27 |
root |
1.1 |
|
28 |
|
|
$main = $Coro::main; |
29 |
|
|
|
30 |
root |
1.3 |
*transfer = \&Coro::State::transfer; |
31 |
root |
1.1 |
|
32 |
root |
1.6 |
sub doit { |
33 |
root |
1.1 |
while() { |
34 |
root |
1.5 |
# some unrolling here as well.. |
35 |
|
|
transfer($c, $main); transfer($c, $main); |
36 |
root |
1.3 |
transfer($c, $main); transfer($c, $main); |
37 |
|
|
transfer($c, $main); transfer($c, $main); |
38 |
root |
1.1 |
} |
39 |
root |
1.6 |
} |
40 |
|
|
|
41 |
|
|
$c = Coro::State::_newprocess [sub { |
42 |
|
|
doit(1,2,3,4,5,6,7,8,9); |
43 |
root |
1.4 |
}]; |
44 |
root |
1.1 |
|
45 |
|
|
transfer($main, $c); |
46 |
|
|
|
47 |
|
|
timethese 100000, { |
48 |
root |
1.6 |
empty => '&a; &a', |
49 |
|
|
method => '$a->b(5); $a->b(6)', |
50 |
|
|
yield => 'yield', |
51 |
root |
1.5 |
transfer => 'transfer($main, $c)', |
52 |
root |
1.1 |
}; |
53 |
root |
1.5 |
|