It doesn't matter how you dress it up it's still broken. Infact, it's worse because now you explicitly differentiate between real and false exceptions. Woe unto ye poor soul that tries to reuse this code. Either use the tool properly or find something more suitable that doesn't hinder your work.
Why, because everyone thinks you're shit? I think this has been a really fascinating discussion on different implementations to a common solution and how (in some ways) *not* to solve a problem. I'd be disappointed to see it closed.
I just tool around with piddly little projects, but even I know when you're using an interpreter you can only go as fast as the interpreter is capable of. Why use an intermediary when you can hit the direct route and just write in assembly?
one not so obvious reason being because you cant write assembly as well or as fast as the run-time compiler will do for you AUTOMATICALLY.
Getting a bit defensive there, lad. You're getting all kinds of free advise here from people who know what theyre doing (not me, I'm just a copy/paste/edit hack) and dismissing their experience because it doesnt mesh with yours. What will you do when your exception-throwing throws an exception you arent prepared for?
not defensive, just witty. dont you get defensive i dont have to worry if it throws an exception that im not prepared for, because ill only catch the ones I want. everything else goes through the normal channels.
If you have a reasonable amount of assembly and processor internals knowledge, and you task is as simple as the examples you've given, you can easily outdo the compiler and run-time.
my code is as far as you will get in managed code. if you want to spent the time writing assembly that is your perogative. The team leader probably wont authorise it any way.
So you imagine a project where it's necessary to abuse exception handling to save a few cycles per loop, but you can't do real optimizations that could make real differences?
Like i said its a trick. You say 'abuse' like the code has feelings. Language is a tool we should make the most of it.
I have a new trick for u Coders dont optimise their code at all anymore. CPUs are really fast and programs are getting more complex. The best way to speed up ur program is to figure out elegant solutions to complex problems. The stuff your doing is done automatically if its needed.
um no, because cpus have billions of cycles a second we dont have to optimise our add from 3 cycles to 2 using low-level assembly hacks. If the programming language says "var = a * b;" you can assume that however that happens is gonna be fast enough and thats the best you need to do. That said, there are times when you do need to optimise, a good example is the executable for running games, its usually optimised in assembly, and also when you use collections using efficient ADTs can create serious speed increases. Unrolling your loop manually or disabling bounds checking is not such a useful optimisation.
some people cant or arent allowed to program low level, so they have to use the designated language. if this is the case, and it often is for me, then you need to know a few of these things in a pinch.
What a bizzarely interesting thread Want to know something horrible? I recently saw this as a recommended way to improve pl/sql loop performance. Can't find it now, but I think it may have come from Oracle... mordy has a good point - most developers these days aren't writing unoptimised code because they're lazy, they're doing it because there's more business benefit in solving a new problem. Of course there are exceptions (no pun...), but it's not often I've seen us need to low-level tune a .NET app. Oracle on the other hand... if I have to replace one more 'not in' with 'minus' I might just kill someone. Ben
Why would the speed of CPU matter when it comes to optimisations? If we've got a CPU bound problem where we need the speed , surely we would want to optimise it? I guarantee that I can name a heap of problems where we'll always need more cpu speed. Some optimisations such as loop unrolling can be harmful sometimes such as when you transition from code that fits entirely within the cache to code that causes cache misses. luke212 : any speed advantage you've obtained depends entirely on the runtime environment you are testing it in , it's a silly trick anyway since a good compiler should recognise what you are doing and make the checks the same anyway.
Absolutely. The people who have criticised you as not being a very good programmer here clearly aren't very good programmers themselves. As you've said, there's no danger (for C# or Java in this example). Regarding loop speed, 0 > value is quicker than value < variable. One of the tests we give new job candidates looks a bit like this: Code: int main(String [] args) { String s; BufferedReader br = new BufferedReader(new FileReader("foo.txt")); try { while (true) { s += br.readLine().trim().toString() + "\n"; } } catch (NullPointerException e) { } } There's dozens of things any decent java programmer should pick up on, not least of which being the use of StringBuilder for linear rather than quadratic performance.