Matlab: Unused part of a switch slowing down the M-file script


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


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


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

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.