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