I was surprised to realize today that all SC Clocks (SystemClock, TempoClock, AppClock) act like time simply stops during code execution, i.e. the internal time of these clocks does not advance while, nor does it appear to catch up after, code runs. These clocks will give the exact same time before and after a code block, even if it took seconds to execute that block.
Process.elapsedTime, however, correctly records the passage of time.
So, unless there’s something I’m missing, we should always expect these clocks to run slower than actual time, since all code takes some amount of time to run. This must be by design, yes? Maybe it would be worth noting in the Clock docs that SC’s clocks cannot be used to reliably measure the passage of time?
This not only affects the ability to measure the passage of time, but also scheduling, as seen in the attached example.
There’s clearly something I’m misunderstanding about the way these clocks work. Any insights are welcome.
(
var clockFuncs, startTimes, endTimes;
clockFuncs = [{Process.elapsedTime},{SystemClock.seconds},{TempoClock.beats},{AppClock.seconds}];
startTimes = clockFuncs.collect{|clockFunc| clockFunc.value};
SystemClock.sched(1.0,{
("Here's the scheduled function. It was scheduled for 1 second into the future, but"+(Process.elapsedTime-startTimes[0])+"have elapsed since it was scheduled.").postln;
});
1000000.do{ |i|
var isPrime = true;
var div = 2;
while {isPrime && (div <= sqrt(i))}
{ if(i%div == 0,{isPrime = false}); div = div+1 };
// if(isPrime, {i.postln});
};
endTimes = clockFuncs.collect{|clockFunc| clockFunc.value};
clockFuncs.do{|clockFunc, i|
clockFunc.asCompileString.postln;
(" start:"+startTimes[i]).postln;
(" end:"+endTimes[i]).postln;
(" elapsed:"+(endTimes[i] - startTimes[i])).postln;
};
)
/* Results on my computer:
{Process.elapsedTime}
start: 1629.762018209
end: 1634.669503834
elapsed: 4.9074856249999
{SystemClock.seconds}
start: 1629.761879334
end: 1629.761879334
elapsed: 0.0
{TempoClock.beats}
start: 1629.5189145
end: 1629.5189145
elapsed: 0.0
{AppClock.seconds}
start: 1629.761879334
end: 1629.761879334
elapsed: 0.0
-> [a Function, a Function, a Function, a Function]
Here's the scheduled function. It was scheduled for 1 second into the future, but 4.907576458 have elapsed since it was scheduled.
*/