java - Eficiency of "pre-casting" vs casting each time -


Sometimes you need a branch based on instance , and then something on the concrete type Do actions.

  if (obj instanceof IonObject) {Ion.assertRegistered (IonObject) obj); Write Mark (obj ((ion object)) .getIonMark ()); ((Ion object) OBG). Saving (this); Return; }   

In this case, make the code more efficient by creating the desired local variables, or is it only a visual improvement?

  if (Obj instanceof IonObject) {// cache valuable value IonObject iobj = (IonObject) obj; Ion.assertRegistered (iobj); writeMark (iobj.getIonMark ()); iobj.save (this); Return; }   

Casting is a small cost because it is to check that typing is appropriate ( Otherwise it does not really make any changes)

However, in this case you can assume that JIT will optimize this check so that once done after writing this code it will be done.

If the code is not run to run adequately (which is quite likely) then the code will be slightly slower, but it does not matter because it is not enough, i.e. the performance difference between jit and gitid away is high.

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -