Given the long-term costs of executing “statements” and exceptions

I always thought that using "if" is much better (in terms of performance) than catching an exception. For example, by doing this:

User u = Users.getUser("Michael Jordan"); if(u!=null) System.out.println(u.getAge()); 

vs.

 User u = Users.getUser("Michael Jordan"); try{ System.out.println(u.getAge()); }catch(Exception e){ //Do something with the exception } 

If we compare this, it is obvious that the first fragment is faster than the second. This is what I always thought. But yesterday the guy told me something like this:

Have you ever thought about what happens in thousands of executions of your programs? Each time you perform it goes through your “if” you have a small (very small, but still something) cost of execution. Except this does not happen. Liked it never arises.

To make this clear: thousands of times have performed "if" against one exception.

I think this makes sense, but has no evidence.

Can you help me?

Thanks!!

+4
source share
4 answers

No. More than once JIT kicks. Tell him to read the trace caches .

Dynamic tracing ("trace path") contains only instructions whose results are actually used, and excludes instructions following accepted branches (since they are not executed); dynamic tracing can be a concatenation of several base blocks. This allows the processor command fetch block to retrieve several base blocks without worrying about branches in the execution thread.

In principle, if the same branches are taken every time through the body of the loop, then this body ends as one trace in the trace cache. The processor will not incur the cost of receiving additional instructions on branching (if this does not push the base unit to the limit of what can be stored in the trace cache, it is unlikely) and you do not need to wait for the test result before starting the following instructions.

+8
source

Never, NEVER, sacrifice your code quality for performance, until you have proven that it is indeed in doubt. I highly doubt that executing the if () statement will become the bottleneck of your program. If so, you should rewrite it in C. On Java lands 99% of the time, the bottleneck is an I / O disk and / or network.

+9
source

With the possible exception of machine exceptions, any exception that you caught is preceded by some kind of conditional if . Even a NullPointerException was thrown after if(something == null) down in the JVM. Don't worry about the performance of the if . Don't worry about try/catch performance, as the error supposedly should occur much less frequently than successful execution. I don’t think you need to optimize how quickly your errors will be thrown. :-)

+2
source

You have no evidence, because you have no business. This is, of course, an interesting abstract question, if you have a piece of code that was a bottleneck due to which you could eliminate if, because one side of the condition was very rare, then this could mean something. But abstractly, doing this with exceptions makes the code harder to read and maintain, so you shouldn't do it without a real problem for you. In the real world, this can cause an exception in 50% of cases. You do not know as long as you have a real-world scenario.

+1
source

Source: https://habr.com/ru/post/1344998/


All Articles