1

I'm using MATLAB R2014a and writing a program that will have to process millions of data points. The problem is that run time increases to an absurd amount once it gets past the hundreds of thousands mark, and even then it's longer than it should be. This is because I have two if statements that both contain an or circuit. I tested it out with a simple code and found that the or circuit takes an incredibly long time compared to breaking the or circuit into two different if statments.

The following code is pretty fast and uses two if statements instead of the or circuit:

    dataBlock = 500000;
    num_loops = 1;
    while num_loops <= 2000000
        if num_loops ==200000
            disp('200000');
        end
        if num_loops == dataBlock
             disp('num_loops = dataBlock');
             dataBlock = dataBlock + 500000;
        end
        num_loops = num_loops+1;
    end

The following code is the same as the one above, but uses an or circuit instead of two if statements. It runs incredibly slow even though I have ensured the more frequent case is written first:

    dataBlock = 500000;
    num_loops = 1;
    while num_loops <= 2000000
        if num_loops == dataBlock|| num_loops == 200000
           disp('entered or cuircit');
            if num_loops == dataBlock
                disp('num_loops = dataBlock');
                dataBlock = dataBlock + 500000;
            end
            num_loops = num_loops+1;
        end
    end

When I realized that splitting the circuit into two if statements was faster, I tried that in my original code but it didn't seem to have any affect. Possibly because the code I'm using is more complicated and lengthy than the ones I used for testing and provided here.

Does anyone know why the length of time required to run the program with the or circuit is so long? Any ideas for a possible alternative?

Kylie Knox
  • 13
  • 2
  • FYI the "circuit" you are referring to should be called a logical OR operation. You might have confused it with [short-circuiting behavior](http://www.mathworks.com/help/matlab/ref/logicaloperatorsshortcircuit.html#bt_0nai-1). – Setsu Apr 15 '15 at 20:50
  • Also, the two versions don't do the same thing; the second version calls `disp` more times than the first. Removing the `disp` calls gave me timings that were roughly equivalent. Also, don't call `disp` when you are timing stuff; writing to the command window is non-trivial and will affect the performance significantly. – Setsu Apr 15 '15 at 21:05

1 Answers1

1

You increment num_loops inside your first if. This variable is always equals to 1. Rewrite this line: num_loops = num_loops+1; just before last end. Otherwise your second algorithm has super-loop (no end).

Rafał P
  • 252
  • 1
  • 8
  • Thanks for pointing out that stupid mistake. I meant to write it the way you described. I was trying to quickly write a simple code that did something similar to what my complicated code does in order to use that as an example. Rewriting my sample code gives what you'd expect: short run times. Which only makes me wonder why my original code is spending so much time on the circuit if I can't even reproduce the problem! Do you think it might be because in my original code the values used in the statement are handles to values, so MATLAB has to spend extra time recalling those values? thanks! – Kylie Knox Apr 15 '15 at 21:33
  • I doubt recalling would make so difference in time. – Rafał P Apr 15 '15 at 21:44