this `drift` calculation seems overly complicated, can't you just do `target_time += wait_time`, and it'll be taken care of, without touching `now`?
@LowByteProductions
Жыл бұрын
You wanted to wait 1 seconds but you ended up actually waiting 1.1 seconds. You reset and try to wait another second, again drifting by .1 seconds.over time, you're accruing a bunch of error that isn't being cancelled out anywhere. If that's not a problem for your application, great! No need to apply it. But there are definitely times where you want to be more or less on your target time with respect to the workload.
@meiskam
Жыл бұрын
@@LowByteProductions no, it wouldn't. you're adding 1 second from the ideal trigger time, not 1.1 seconds. in your code, the `now` cancels out with itself.
@LowByteProductions
Жыл бұрын
I'm not sure I follow your objection. `drift` in this example would end up being 0.1. So we set `target_time = (now + timer->wait_time) - drift`; 1 second in the future, minus the 0.1 second drift, for a total wait time this iteration of 0.9 seconds. If the next time around, we're able to hit that target, we've cancelled out the error, and are back on the 2 second mark. The idea is to try to wait, over time, for the actual amount of time you're aiming for. I hope that's clear 👍
@ax13h
Жыл бұрын
@@LowByteProductions If: drift = now - target, and: target = now + wait - drift, then: target = target + wait You never need to explicitly calculate the drift because target is always the ideal time, incrementing by the period each time.
@LowByteProductions
Жыл бұрын
Indeed, I see what you're both saying now
@evugar
Жыл бұрын
Wouldn't it be more appropriate to rename "simple_timer_has_elapsed" function to something like "simple_timer_event_happened"?
Пікірлер: 12