The TEMPORARY
routine is a simple way to use memory more efficiently. Statements of the form
a = a + 5
create a temporary variable to hold the intermediate result a + 5
. This temporary variable effectively doubles the amount of memory needed to store a
(but only for the time necessary to do the operation). This is not necessary if we know that the expression a + 5
will be reassigned to a
. In this case, a
itself can be used as the temporary variable. Use TEMPORARY
to indicate to IDL which variable can be used as the temporary variable:
a = temporary(a) + 5
Now, adding 5
will happen “in place” in the memory that holds a
. Incidently, for operations like the above, an even more efficient way to perform this calculation is:
a += 5
There are composite assignment forms like the above for all of IDL’s binary operators.
On the other hand, TEMPORARY
does not help for expressions where the variable to be reassigned to is used multiple times like
a = a^3 + a^2
Neither a
on the right-hand side of this statement can be marked temporary because marking a variable as temporary makes the variable undefined (until it is reassigned to again). In this case, if the a
in a^3
was marked as temporary, then the a
in a^2
would be undefined. (And vice versa.)
The argument of TEMPORARY
must be a named variable to have an effect. So
a = temporary(a + 5)
will perform its arithmetic correctly, but will create a temporary variable in doing it.
May 16th, 2006 at 10:39 pm
I often see programmers using subscripted statements in their TEMPORARY expressions, such as
a = temporary(a[*, *, i])*5
Using TEMPORARY here doesn’t save anything since a subscripted array creates an expression, which is a copy of the referenced memory.
May 31st, 2006 at 2:54 pm
What about something like this:
a = sin(temporary(a))
I’d like to do the sine calculation in place, but wouldn’t
temporary(a)
be passed by value to SIN, thereby creating a copy ofa
?June 2nd, 2006 at 10:57 pm
Yes, it would. I changed my example to something more realistic.
May 25th, 2010 at 5:22 am
It doesn’t make any difference when operating with another array which is already temporary (an expression). Such as,
a = temporary(a) + findgen(size(a,/DIM))
Here the equivalent
a += findgen(size(a,/DIM))
avoids the overhead of the call to thetemporary
function.Nevertheless, the use of the composite assignment performs worse when used with expressions involving other named variables, since the evaluation of the expression implies memory allocation for the result.
For example,
The expression
a += b + 5
implies the creation of a temporary array containingb + 5
followed by the composite assignment.The expression
a = temporary(a) + b + 5
does the whole calculation in place.Regards,
Diego
May 26th, 2010 at 9:38 am
Correct:
IDL> startMemory = memory(/current)
IDL> a = temporary(a) + findgen(100)
IDL> print, memory(/highwater) - startMemory
616
IDL> startMemory = memory(/current)
IDL> a += findgen(100)
IDL> print, memory(/highwater) - startMemory
616
and
IDL> startMemory = memory(/current)
IDL> a += b + 5
IDL> print, memory(/highwater) - startMemory
616
IDL> startMemory = memory(/current)
IDL> a = temporary(a) + b + 5
IDL> print, memory(/highwater) - startMemory
237
May 27th, 2010 at 2:22 am
And in the first case,
a = a + findgen(100)
is equivalent to both options as well, since the summation takes place in the array newly created by
findgen
.