Code Efficiency


Code Efficiency

matlab



Computer programs (code) can be:

  • Time efficient i.e. run very quickly
  • Memory efficient i.e. use a small amount of RAM
  • Both
  • Neither

Sometimes this is important, however often it is not. Typically, readability is more important.

Time efficiency

Can be assessed directly by timing how long code takes to run. Time will vary even on the same machine, as it depends on the hardware and the load on the machine. The inbuilt tic...toc function is used to measure this, and it prints the time taken of the lines of code in-between the function. e.g.

function sumOfIntegers(N)
% ...
tic
for j = 1:N
    total = total + a(j);
end
toc
% ...
COMMAND WINDOW
>> sumOfIntegers(1000000)
Elapsed time is 0.014182 seconds.
...

The rest of this chapter will be completed soon


return  link
Written by Tobias Whetton