1

So, I want to know if making the code more easy to read slows performance in Matlab.

function V = example(t, I)

a = 10;
b = 20;
c = 0.5;
V = zeros(1, length(t));
V(1) = 0;
delta_t = t(2) - t(1);
for i=1:length(t)-1
    V(i+1) = V(i) + delta_t*feval(@V_prime,a,b,c,t(i));
end;

So, this function is just an example of a Euler method. The idea is that I name constant variables, a, b, c and define a function of the derivative. This basically makes the code easier to read. What I want to know is if declaring a,b,c slows down my code. Also, for performance improvement, would be better to put the equation of the derivative (V_prime) directly on the equation instead of calling it? Following this mindset the code would look something like this.

function V = example(t, I)

V = zeros(1, length(t));
V(1) = 0;
delta_t = t(2) - t(1);
for i=1:length(t)-1
    V(i+1) = V(i) + delta_t*(((10 + t(i)*3)/20)+0.5);

Also from what I've read, Matlab performs better when the code is vectorized, would that be the case in my code?

EDIT: So, here is my actual code that I am working on:

function [V, u] = Izhikevich_CA1_Imp(t, I_amp, t_inj)

vr = -61.8;             % resting potential (mV)
vt = -57.0;             % threshold potential (mV)
c  = -65.8;             % reset membrane potential (mV)
vpeak = 22.6;           % membrane voltage cutoff
khigh = 3.3;            % nS/mV
klow  = 0.1;            % nS/mV
C = 115;                % Membrane capacitance (pA)
a = 0.0012;             % 1/ms
b = 3;                  % nS
d = 10;                 % pA
V = zeros(1, length(t));
V(1) = vr; u = 0;       % initial values

span = length(t)-1;
delta_t = t(2) - t(1);

for i=1:span
    if (V(i) <= vt)
        k = klow;
    else
        k = khigh;
    end;
    if ((t(i) >= t_inj(1)) && (t(i) <= t_inj(2)))
        I_inj = I_amp;
    else I_inj = 0;
    end;
    V(i+1) = V(i) + delta_t*((k*(V(i)-vr)*(V(i)-vt)-u(i)+I_inj)/C);
    u(i+1) = u(i) + delta_t*(a*(b*(V(i)-vr)-u(i)));
    if (V(i+1) >= vpeak)
        V(i+1) = c;
        V(i) = vpeak;
        u(i+1) = u(i+1) + d;
    end;
end;
plot(t,V);

Since I didn't have any training in Matlab (learned by trying and failing), I have my C mindset of programming, and for what I understand, Matlab code should be vectorized. Eventually I will start working with bigger functions, so performance will be a concern. Now my goal is to vectorize this code.

11
  • What are the results of your tests on the performance of the two versions of the code ? Commented Jul 31, 2014 at 17:35
  • 1
    If you are asking as a general rule for any function to be faster or not when put against a loopy code, the short answer would be - Depends. As for the code used in the question, vectorized code would be - V = cumsum([0 delta_t*(((a + t(1:end-1)*3)/b)+c)]). Commented Jul 31, 2014 at 17:45
  • I don't really have matlab right now, so that's why I am asking. First I will try to vectorize the code then. (I am working with another code, but the idea is the same) Commented Jul 31, 2014 at 17:49
  • @lhahn If you care, share with us the other code too? Commented Jul 31, 2014 at 17:55
  • @Divakar I edited the code with the actual one Commented Jul 31, 2014 at 18:04

2 Answers 2

3

Usually it is faster.

Especially if you replace looped function calls (like plot()), you will see a significant increase in performance.

In one of my past projects, I had to optimize a program. This one was made using regular program rules (for, while, etc.). Using vectorization, I reached a 10 times increase in performance, which is quite notable..

I would suggest using vectorisation instead of loops most of the time.

Sign up to request clarification or add additional context in comments.

Comments

1

On matlab you should basically forget the mindset coming from low-level C programming. In my experience the first rule for achieving performance in matlab is to avoid loops and use built-in vectorized functions as much as possible. In general, you should try to avoid direct access to array elements like array(i).

Implementing your own ODE solver inevitably leads to very slow execution because in this case there is really no way to avoid the aforementioned things, even if your implementation is per se fine (like in your case). I strongly advise to rely on matlab's ode solvers which are highly optimized blocks of compiled code and much faster than any interpreted matlab code you can write.

In my opinion this goes along with readability of the code as well, at least for the trivial reason that you get a shorter code... but I guess it is also a matter of personal taste.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.