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 |
root |
1.12 |
sub a($) { } |
12 |
root |
1.8 |
|
13 |
root |
1.1 |
$a = bless {}, main::; |
14 |
|
|
|
15 |
root |
1.6 |
sub b { |
16 |
|
|
my ($self) = shift; |
17 |
root |
1.7 |
$self->{b} = shift if @_; |
18 |
|
|
$self->{b}; |
19 |
root |
1.1 |
} |
20 |
|
|
|
21 |
root |
1.4 |
$b = async { |
22 |
root |
1.1 |
# do a little unrolling... |
23 |
|
|
while() { |
24 |
root |
1.8 |
cede; cede; cede; cede; cede; |
25 |
root |
1.16 |
cede; cede; cede; cede; cede; |
26 |
root |
1.1 |
} |
27 |
|
|
}; |
28 |
|
|
|
29 |
root |
1.8 |
cede; |
30 |
root |
1.1 |
|
31 |
root |
1.3 |
*transfer = \&Coro::State::transfer; |
32 |
root |
1.1 |
|
33 |
root |
1.8 |
sub doit0 { |
34 |
|
|
while() { |
35 |
|
|
# some unrolling here as well.. |
36 |
root |
1.15 |
transfer($c0, $main); transfer($c0, $main); |
37 |
|
|
transfer($c0, $main); transfer($c0, $main); |
38 |
|
|
transfer($c0, $main); transfer($c0, $main); |
39 |
|
|
transfer($c0, $main); transfer($c0, $main); |
40 |
root |
1.8 |
} |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
sub doit1 { |
44 |
root |
1.1 |
while() { |
45 |
root |
1.5 |
# some unrolling here as well.. |
46 |
root |
1.15 |
transfer($c1, $main); transfer($c1, $main); |
47 |
|
|
transfer($c1, $main); transfer($c1, $main); |
48 |
|
|
transfer($c1, $main); transfer($c1, $main); |
49 |
|
|
transfer($c1, $main); transfer($c1, $main); |
50 |
root |
1.1 |
} |
51 |
root |
1.6 |
} |
52 |
|
|
|
53 |
root |
1.11 |
$c0 = new Coro::State sub { |
54 |
root |
1.8 |
doit0(1,2,3,4,5,6,7,8,9); |
55 |
root |
1.11 |
}; |
56 |
root |
1.8 |
|
57 |
root |
1.11 |
$c1 = new Coro::State sub { |
58 |
root |
1.8 |
doit1(1,2,3,4,5,6,7,8,9); |
59 |
root |
1.11 |
}; |
60 |
root |
1.1 |
|
61 |
root |
1.17 |
#$c0->save (0); |
62 |
|
|
#$c1->save (-1); |
63 |
root |
1.15 |
|
64 |
root |
1.20 |
#Coro::State::enable_times 1; |
65 |
|
|
#use Coro::Debug; Coro::Debug::command "ps";#d# |
66 |
root |
1.19 |
#(async { |
67 |
root |
1.22 |
$main = $Coro::current; |
68 |
|
|
|
69 |
|
|
transfer($main, $c0); |
70 |
|
|
transfer($main, $c1); |
71 |
|
|
|
72 |
|
|
timethese 5000000, { |
73 |
|
|
function => 'a(5); a(6)', |
74 |
|
|
method => '$a->b(5); $a->b(6)', |
75 |
|
|
cede => 'cede', |
76 |
|
|
transfer0 => 'transfer($main, $c0)', |
77 |
|
|
transfer1 => 'transfer($main, $c1)', |
78 |
|
|
}; |
79 |
root |
1.19 |
#})->join; |
80 |
root |
1.20 |
#use Coro::Debug; Coro::Debug::command "ps";#d# |
81 |
root |
1.5 |
|
82 |
root |
1.13 |
|