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?
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).