Sometimes it can come in handy to set a lower limit for a calculation. Consider for example an animation that is generated by some infinite loop, which would run way too fast to display properly for the user. For this reason, I need a function of the type "pause at least". It would be like a little brother of TimeConstrained
.
I threw together the following small code which seems to do the trick, but I'm wondering whether there is a more efficient or elegant solution for this?
Here's my version if you're interested, it's pretty straightforward:
pauseAtLeast[calculation_, pause_] :=
Block[{start, result},
start = AbsoluteTime[];
result = calculation;
If[AbsoluteTime[] < start + pause,
Pause[start + pause - AbsoluteTime[]]
];
result
]
SetAttributes[pauseAtLeast, HoldFirst];
Usage:
(* Pauses 2 seconds *)
AbsoluteTiming@pauseAtLeast[Pause[2], 1]
(* Pauses 1 second *)
AbsoluteTiming@pauseAtLeast[Pause[.5], 1]
Comments
Post a Comment