#!/usr/bin/perl

# ->resume is not exactly cheap (it saves/restores a LOT
# of global variables), but shouldn't be slow. just to show
# how fast it is, this little proggie compares a normal subroutine
# call with two calls of transfer in a loop.

use Coro;
use Benchmark;

$a = bless {}, main::;


sub a {
   $n++; # do something to taint the benchmark results ;)
}
$|=1;

$b = async {
   # do a little unrolling...
   while() {
      yield; yield; yield; yield; yield;
   }
};

yield;

$main = $Coro::main;

*transfer = \&Coro::State::transfer;

$c = Coro::State::_newprocess [sub {
   while() {
      transfer($c, $main); transfer($c, $main);
      transfer($c, $main); transfer($c, $main);
   }
}];

transfer($main, $c);

timethese 100000, {
   method   => '$a->a; $a->a; $a->a; $a->a',
   resume   => 'yield; yield',
   transfer => 'transfer($main, $c); transfer($main, $c)',
};
