# "spawn: run work in parallel": four CPU-bound jobs, first one after the# other, then each in its own spawn. Timings depend on the machine.use io;
usetask;
use time;
funcwork(n asint) {
def acc asintinit0;
for (def i in0..600000) {
$acc = ($acc + $i * $n) % 1000003;
}
return$acc;
}
def t0 asintinittime.unixMillis(time.now());
def serial aslistofintinit [];
for (def n in1..5) {
$serial[] = work($n);
}
def t1 asintinittime.unixMillis(time.now());
def tasks aslistoftaskofintinit [];
for (def n in1..5) {
$tasks[] = spawn { returnwork($n); };
}
def results aslistofintinittask.waitAll($tasks);
def t2 asintinittime.unixMillis(time.now());
io.printf("serial %d ms, spawned %d ms, same: %v\n",
$t1 - $t0, $t2 - $t1, $results == $serial);