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