11
$\begingroup$

Is there any way to make Mathematica stop for some miliseconds?

I know about the Pause[] but it seems it only works with seconds.

$\endgroup$
4
  • 1
    $\begingroup$ "Pause is accurate only down to a granularity of at least $TimeUnit seconds." - which is 0.001 so you are lucky. $\endgroup$
    – Kuba
    Mar 11, 2014 at 18:24
  • $\begingroup$ So I can use Pause[0.5] for example? $\endgroup$
    – José D.
    Mar 11, 2014 at 19:18
  • 1
    $\begingroup$ @Trollkemada On my system {Mean@#, StandardDeviation@#} &@ Table[First@AbsoluteTiming[Pause[0.123]], {10}] returns {0.123241, 0.00003}. So, at least in some situations, it seems quite reliable. Just don't rely on it for "hard real time" applications. $\endgroup$
    – kirma
    Mar 11, 2014 at 20:08
  • $\begingroup$ @Kuba I wouldn't close the question as this is not a trivial topic. $\endgroup$
    – Szabolcs
    Mar 11, 2014 at 22:47

1 Answer 1

9
$\begingroup$

The short answer is: no, Mathematica is simply not suitable for time-critical applications.


There are several reasons for this:

  • Mathematica is a slow interpreted language and the time taken by various operations can be close to your threshold of milliseconds

  • There are several things that may interrupt the Mathematica kernel while it's doing calculations, including ScheduledTasks and other preemptive evaluations from GUI elements (see Dynamic)

  • Mathematica's time measurement functions are simply not precise, as the underlying system calls it uses are not meant for precision applications.

Regarding $TimeUnit: this variable is supposed to give the granularity of both Pause and time measurement functions such as AbsoluteTiming and Timing. One thing to be aware here is that its value varies between operating systems (I get 1/100 on OS X and 1/1000 on Windows). Another is that it is simply not precise or reliable. For example, on Windows XP with Mathematica 8 it returns 1/1000. However the true granularity on Windows XP is 15 milliseconds, i.e. 15/1000. (On Vista and later the same system function Mathematica uses really does have a granularity of 1 ms.) On OS X / Mathematica 9, the true granularity seems to be 1 ms even though $TimeUnit is 1/100.

$\endgroup$
1
  • 1
    $\begingroup$ I agree, well, imo OP was not clear enough so without anyone who could kindly elaborate vague topic it was meant to be closed. But now I;ve retracted closevote. +1. $\endgroup$
    – Kuba
    Mar 11, 2014 at 23:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.