3

I am having a little bit of hard time understanding ScheduledTask[]. This was added in V 8.

On windows 7, 64 bit, using V 8.01, when I run the following code

dt = 0.01;
c = 0;
Dynamic[c]
task = CreateScheduledTask[c += 1, {dt, 100}];
StartScheduledTask[task];

I get 'c' ending with value 51, not 100 as expected.

When I change 'dt' above to larger value, say 0.02, or any other larger value than 0.02 seconds, only then I get 100 each time.

If I make 'dt' 0.005, I end up with c=27. Make 'dt' 0.0025, c becomes 18, and so on.

So, the 'dt' limit (CPU time granularity) seems to be 0.02 to get what I want. This is 20 milliseconds.

But from other web sites, I see that windows 7 (depending on HW ofcourse) can provide cpu clock resolution much smaller than this. May be 1-10 milliseconds.

Am I missing something here, or did I misunderstand this function?

Help on this function says

creates a task that will try evaluating expr once every time seconds up to count times total

I could not find more information on this using Mathematica own documentation, but I could have overlooked it.

documentation for this function is http://reference.wolfram.com/mathematica/ref/CreateScheduledTask.html

Thanks ps. to remove task when done, the command is

RemoveScheduledTask[task]

or to clean everything:

RemoveScheduledTask[ScheduledTasks[]]

Hardware I have is: Intel i7 CPU 930, 2.8 Ghz 4 core

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Nasser
  • 12,849
  • 6
  • 52
  • 104
  • 3
    Under "More Information" in the documentation of `CreateScheduledTask` it says that "By default, if the time necessary to evaluate expr is larger than time, the next pending evaluation is skipped." so maybe that's the problem here (although I would have thought that it takes a lot less than 0.01 seconds to add 1 to c). – Heike Aug 15 '11 at 10:54
  • I think it is the clock time is the limit, not adding 1 to a number. The task clock can't run at faster rate than about 20 ms per slot, on my system, which is 0.02 seconds. And this is what I was seeing. But I agree with you, documentation on this function can be made more detailed and clear. – Nasser Aug 15 '11 at 12:02

1 Answers1

4

Although the windows timer reportedly can measure into the ns granularity the task scheduler has a much lower granularity. I found it reported (here and here) to be between 10-15ms and multiples of 20 ms, respectively. I think you should therefore use a minimum of 20 ms spacing to be sure and then you still aren't guaranteed to get a slot, so you should never depend on it.

Community
  • 1
  • 1
Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • +1, thanks. Yes, 20 ms is actually what I was seeing. So my mistake in calculation. corrected. 10-20 ms matches what I read also. this should be mentioned in the documentation under 'possible issues'. – Nasser Aug 15 '11 at 11:59