How can I run only a newly add line of code?

Hi,

I am not sure about the keywords that I should look for on this problem.

Suppose my codes can be separated into two parts.

The first part is generating a very huge Markov transition matrix and finding a solution of something to each state by NLsolver. This part takes a tremendous time, but after getting the solution, I don’t have to and want to repeat this part.

For the second part, suppose I am still working on. I change my codes a lot, and I want to see how the results change. However, when I just Ctrl+Enter, Jupyter notebook automatically runs including the first part.

How can I solve this problem? In R, outcomes are saved in a working directory, so I could run only a part of codes without affecting the other variables.

If you store the first part in a separate cell and have the result stored in a matrix or something (well you will have something like this anyways to use it in the second part?) – then put your code in a second cell. The Julia Kernel behind (or also REPL) has the metrix from the first cell available and you can restart the second computation as often as you want – just make sure not to modify the result from the first cell so you can rerun.

1 Like

Ah I see. It was so simple!
Thank you very much!

1 Like

You can also put the computationally expensive code in a separate function and place a @memoize from Memoize.jl on the function. Then, successive calls to the function will be loaded from memory. That works excellent in combination with Revise.jl as well. With this, you can call any function which depends on your expensive function.

Note that Memoize.jl, or any caching, can lead to unexpected and hard to debug behaviour. However, your problem seems to be pretty pure (less state because it is math), so memoize should work very well