Performance: Difference between revisions
(Adding syntax highlight) |
(highlighting) |
||
Line 35: | Line 35: | ||
When passing variables to a function, Octave will only make a copy of it is going to be modified. That is, unless the argument needs to be changed, the variable will be like a reference and take no extra memory or time to write. | When passing variables to a function, Octave will only make a copy of it is going to be modified. That is, unless the argument needs to be changed, the variable will be like a reference and take no extra memory or time to write. | ||
{{Code|Looking at lazy copying|< | {{Code|Looking at lazy copying|<syntaxhighlight lang="matlab" style="font-size:13px"> | ||
function read_arg (arg) | function read_arg (arg) | ||
x = arg(1); | x = arg(1); | ||
Line 52: | Line 52: | ||
t = cputime (); modi_arg (a); t_read = cputime () -t ## takes some time since a copy of a will be made to be modified | t = cputime (); modi_arg (a); t_read = cputime () -t ## takes some time since a copy of a will be made to be modified | ||
t = cputime (); chan_arg (a); t_read = cputime () -t ## takes NO time since even though arg is modifed, it's not based on the original value | t = cputime (); chan_arg (a); t_read = cputime () -t ## takes NO time since even though arg is modifed, it's not based on the original value | ||
</ | </syntaxhighlight>}} | ||
=== Global variables === | === Global variables === |
Revision as of 07:11, 22 May 2012
Things to consider when writing octave code to have good performance. They relate to how the interpreter works.
Writing functions
Default arguments
It is possible to set default values when declaring the function arguments. This defaults can be the result of a computation, maybe even using the other arguments:
Code: Defining argument defaults |
function foo (arg1, arg2 = heavyfoo (arg1))
## code
endfunction
|
One could think of setting defaults inside a condition checking the value of nargin
to avoid computing heavyfoo. However the interpreter will not generate the default value if the value is already given so it ends up being the same thing. That is, don't try to be smart, the interpreter already is, and write defaults on the list of arguments for code that is shorter and easier to read.
Code: Proof that interpreter does not compute defaults unless needed |
function defaults_at_list (arg = rand (10000))
## just a simple operation
arg = arg +1;
endfunction
function defaults_at_cond (arg)
if (nargin == 0)
arg1 = rand (10000);
endif
## just a simple operation
arg = arg +1;
endfunction
t = cputime (); a = rand (10000); t_crea = cputime () - t
t = cputime (); defaults_at_list (a); t_list = cputime () - t
t = cputime (); defaults_at_cond (a); t_cond = cputime () - t
|
Function arguments (lazy copying)
When passing variables to a function, Octave will only make a copy of it is going to be modified. That is, unless the argument needs to be changed, the variable will be like a reference and take no extra memory or time to write.
Code: Looking at lazy copying |
function read_arg (arg)
x = arg(1);
endfunction
function modi_arg (arg)
arg(1) = 1;
endfunction
function chan_arg (arg)
arg = true;
endfunction
a = rand(10000);
t = cputime (); read_arg (a); t_read = cputime () -t ## takes NO time, since a copy of arg is never made
t = cputime (); modi_arg (a); t_read = cputime () -t ## takes some time since a copy of a will be made to be modified
t = cputime (); chan_arg (a); t_read = cputime () -t ## takes NO time since even though arg is modifed, it's not based on the original value
|
Global variables
Vectorization
It's well known that loops have bad performance in Octave. Vectorization of the code involves replacing them by vector operations. The Octave manual already has a good chapter on vectorization.