Now current run time in 2 seconds. There are no exceptions thrown! If I comment out the throw line then run time drops to .5 seconds. How can such a drop occur when the exception was never being thrown? |
If you comment out the throw line, the if block becomes empty and the compiler can optimize it, and the 4-part bound check, out of the program.
In other words it's not the exception that's slowing you down, it's probably the bounds checking.
Try doing this and see how it compares:
1 2 3 4
|
if(0)
{
throw ....
}
|
If my hunch is right, this will run as fast as when you have the exception commented out.
Also this performance drag exception handling appears to cause is a worry. Should I just go back to returning integer values. |
This is why classes like vector don't do bounds checking in their [] operator. The performance hit would be too severe in some situations.
If you're concerned with this class's performance, you might want to consider removing the bounds checking entirely.
And lastly, if I have a = b*c*d, that will be calculated as a = b*(c*d) right? |
No. Operators are executed left to right in order of precedence. So it would be a = (b*c)*d.
But really it shouldn't matter. If you need it happen in a specific order I would use parenthesis even if they technically aren't necessary.