tags:

views:

70

answers:

3

Hi. I am new to Matlab and programming in general, but I am curious about what I have noticed in a particular script I have made. I have a 'Switch' in a fairly complicated for loop that runs many times. When testing it on a file that uses only cases 0-4, I noticed that if I delete the switch cases 5-8 (no other changes) my time goes from 18.2 to 4.5 seconds. Was I wrong to think that cases are skipped and would not noticably affect the timing if they went unused?

PS I am a rookie at all this so it may be user error, but I tried to check over everything to make sure it was not

EDIT

Okay so thanks for the help so far. The profiler thing was very cool, but I dont think it helped me figure out what is wrong yet. The thing that was slowing down the code the most was one particular 'if' statement that actually contains the switch. It was called 3,169,449 times in each case, but took 1.22 seconds without cases 5-8 and 15 seconds with. The code is very long, but I will post a simplified version with out the actaul operations of the cases. What the profiler did tell me is that cases 5-8 were never called, and it was not that they were complex functions, each case corresponds to the actual number 0-8 as its trigger value.

        for x= 1:length(firstinSeq)
            for y= 1:length(littledataPassed-1)
                if firstinSeq(x,1)== littledataPassed(y,1) && firstinSeq(x,2)== littledataPassed(y,2) %times and flight are the same
                        switch firstinSeq(x,3)
                            case 0

                            case 1

                            case 2

                            case 3

                            case 4

                        end
                end
            end
    end

Again, the part tof the script that struggles with all 9 cases is the if statement right before the switch.

+2  A: 

You might want to run your code with MATLAB's profiler turned on. It will tell you precisely which functions got called how many times and how much time each took.

Look at

doc profile

for more information.

lindelof
Thanks, this helped me figure out where the problem was occuring and I will be using this lots in the future.
Maxwell
A: 

I really doubt that's the real issue unless you have a complicated expression evaluated at the case test, ie:

switch choice
    case 1
        %# ...
    case myfunction()
        %# ...
    case 3
        %# ...
end

where myfunction() is an expensive call. Otherwise you could be overlooking some cases where 5-8 are actually executed, so when you removed them they simply hit the otherwise case if you have one...


Since you didnt provide any code, consider this example:

%%
tic
for i=1:500
    choice = randi(2);      %# 1 or 2
    switch choice
        case 1, x = lu(rand(500));
        case 3, x = lu(rand(100000));
        case 2, x = lu(rand(500));
        case 4, x = lu(rand(100000));
        otherwise, x = [];
    end
end
toc

%%
tic
for i=1:500
    choice = randi(2);      %# 1 or 2
    switch choice
        case 1, x = lu(rand(500));
        case 2, x = lu(rand(500));
        otherwise, x = [];
    end
end
toc

The timing was not that different:

Elapsed time is 10.881236 seconds.
Elapsed time is 10.846885 seconds.
Amro
I looked into this, but I dont think that is it. again I am very new to code, and am wondering if there is a problem with the way I set up the if statement, as it will only be true for one value of y? I posted an edited down version of the code above.
Maxwell
A: 

The reason the unused part of the switch might slow down things could have something to do with the accelerator. Type feature accel off at the command line to turn of the JIT accelerator, and do the comparison again. My guess is that the timing will be slow for both versions.

Don't forget to turn the accelerator back on with feature accel on afterward!

Jonas

related questions