# will the same result be evaluated twice per evaluation?

here is the silly question of mine,

if mathematica takes 3 seconds to evaluate the function f then I plug the this value f into a new function g[f,f], Let's ignore the evaluation time for the new function g, how long does it take for g[f,f], 3 seconds or 6 seconds?

if it is 6 seconds, how can I make mathematica temporarily to remember the value of f

thanks

Use With to define temporary constants.

With[{a=f}, g[a,a]]


Memoization is also worth mentioning but keep in mind that once a value is remembered, it won't be forgotten (until to explcitly clear it). I.e. when you use memoization, Mathematica will remember permanently (not temporarily).

With keeps the code clear for longer cases but sometimes I'd do:

g[#,#] & @ f

Clear["Global*"]
ClearSystemCache[];
\$RecursionLimit = Infinity;
fibonacci = 1; fibonacci = 1;
fibonacci[i_] := fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
fibonacci; // AbsoluteTiming
fibonacci; // AbsoluteTiming
(*{0.135008, Null}*)
(*{0., Null}*)
`