heres a cool trick if you need to loop a very large collection: seems about 10-15% faster than checking the limit every iteration Code: int len = 300000000; ushort[] a = new ushort[len]; try { uint ac = 0; while (true) { a[ac] = 100; ac++; } } catch (IndexOutOfRangeException e2) { } // wow that was fast
please tell me im missing something.... your forcing it to bassically crash instead of doing proper bounds checking? I certainly hope we are never working on the same code.
Yeah, because it's never possible that you might overflow into some memory that shouldn't be touched, right?
I'm guessing that's C? In javascript, the "minus-minus" trick works the best: Code: for( var i = data.length - 1; i >= 0; i-- ) { tmp = data[i]; }; Assuming you have a large variable called data
It'd never work in straight C, there's no bounds checking. I would guess the OP is a Java "programmer".
its a valid trick. there is no danger because it is managed. incrementing from length to 0 with -- is also a speed increase as mentioned above.
there is a point where it goes from being slower to faster. in your situation you can always try both and see.
Indeed. As we were taught, exception handling is for just what the name suggests - exceptional circumstances. Not just where it makes things easy. This sort of approach was used in one of the finalists from the OMGWTF calculator contest. It scares me that people are suggesting it as a way of writing 'proper' code.
All you provide is a claim from authority that you were 'told' not to use it. This is fine, but be sure to find the specific reasoning as well. You can write a whole event based program on exception handling. it will work every time. It is similar, in some ways, to the CALLBACK system used by windows XP (not sure Vista yet). but i wouldnt recommend THAT.
Callbacks are not similar to exceptions. An exception is raised when an error condition is encountered - and your program logic should either handle it, or die. A callback is raised when a given worker thread has completed its processing and is indicating that the next stage should commence. If an callback encounters an error, it will raise an Exception, as will its parent thread. Exceptions are stack-based whereas callbacks are effectively a "goto"
I certainly agree with bugayev - don't use exception handling to check for the end of a loop, it is bad practice. It comes down to this: code readability vs performance - code readability should always win.
i never said they were. callbacks are an events system. exceptions are mechanisms you can use to build an events system.
Hear hear! Good practices exist and are recommended for a reason. There are some exceptions however, anything that is highly performance dependant - such as database sorting or 3d graphics apps - will usually benefit more from fast code than pretty code. That's definately the minority of programs though, of my code at least spends all its time waiting for user input
so if you HAD to write a scientific app you wouldnt ever use this technique? i for one welcome our Exception Overlords.