# Why does Dynamic output cause CPU usage when onscreen?

I write such a line in Mathematica:

{Slider[Dynamic[s]], Dynamic[f[x_] := s*x]}


After evaluation, the CPU immediately runs at full power. And if you scroll the page, it turns out that as soon as this line appear on the screen, the CPU begin to work. This really confused me a lot. Can anyone help?

-
Honestly, I believe this is a bug which probably never got detected because what you do doesn't make much sense. Slight variations which make more sense (e.g. { Slider[Dynamic[y]], Slider[Dynamic[s]], Dynamic[f[x_] := s*x; f[y]]}) show the same behavior, so you might want to report it as a possible bug... – Albert Retey Jan 6 '13 at 11:46

## 1 Answer

The spot where Dynamic[x] is on the screen only gets to do anything when this spot itself is actually visible on the screen. i.e. x gets tracked and all dependent expressions on it are updated, when the spot on the screen itself where the evaluated output of Dynamic[x] is visible. If you hide the Mathematica window with another window for example, Dynamic will not update any more.

So, when you scroll down or up, the front-end does not bother any more updating that spot on the screen.

Now why it runs at full speed. First, I really do not understand what you mean by Dynamic[f[x_] := s*x]. What exactly are you trying to do with this? One applies Dynamic on symbols and expressions, not on a delayed function definition as you are doing.

My guess on why FE goes to a loop, is that it is evaluating this expression over and over since it depends on s which is dynamic since you set the whole expression as Dynamic.

But all what you have to do is

{Slider[Dynamic[s]], Dynamic[s]}
f[x_] := s*x


and now if you type f[6] then change the slider, and retype f[6] again, then you'll see that you get a new value returned, since it used the updated value of s.

And if you type

Dynamic[y = s]


in another place, and then move the slider, you'll see that this spot on the screen update on its own, since you marked this spot as Dynamic, then FE will now refresh y automatically when s changes.

-