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 |
$n++; # do something to taint the benchmark results ;) |
16 |
} |
17 |
$|=1; |
18 |
|
19 |
$b = async { |
20 |
# do a little unrolling... |
21 |
while() { |
22 |
yield; yield; yield; yield; yield; |
23 |
} |
24 |
}; |
25 |
|
26 |
yield; |
27 |
|
28 |
$main = $Coro::main; |
29 |
|
30 |
*transfer = \&Coro::State::transfer; |
31 |
|
32 |
$c = Coro::State::_newprocess [sub { |
33 |
while() { |
34 |
transfer($c, $main); transfer($c, $main); |
35 |
transfer($c, $main); transfer($c, $main); |
36 |
} |
37 |
}]; |
38 |
|
39 |
transfer($main, $c); |
40 |
|
41 |
timethese 100000, { |
42 |
method => '$a->a; $a->a; $a->a; $a->a', |
43 |
resume => 'yield; yield', |
44 |
transfer => 'transfer($main, $c); transfer($main, $c)', |
45 |
}; |