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