java - Why does processing try-catch without any exception thrown not slow down the program at all? -


today realized appeared weird me: noticed when do

try {     dosomething(); } catch (exception e) { } 

it isn't slower @ if do

dosomething(); 

so ran test , wrote down quick code prove saw, code loops on function called dosomething() lots of times, 1 time without , 1 time try-catch surrounding it. here's code if want test yourself:

public class main {  private static final long loops = 1000000l;  public static final void main(string[] args) {     system.out.println("loop without try catch: "+loopwithouttrycatch(loops));     system.out.println("loop try catch: "+loopwithtrycatch(loops)); }  public static long loopwithouttrycatch(long loops) {     long starttime = system.currenttimemillis();      (long = 0l; < loops; i++)     {         dosomething();     }      return system.currenttimemillis()-starttime; }  public static long loopwithtrycatch(long loops) {     long starttime = system.currenttimemillis();      (long = 0l; < loops; i++)     {         try {             dosomething();         } catch (exception e) {         }     }      return system.currenttimemillis()-starttime; }  public static void dosomething() {     (int = 0; < 250; i++)     {         if (i % 3 == 0)         {             i++;         }     } } } 

and received following output:

loop without try catch: 375 loop try catch: 373 

i surprised tested again , again, got similar results, both ways runs pretty in same time.

and question is: why?

i dont understand it, far know try-catch writes resources before usage in kind of table later - if exception thrown - able clean , reference values had before exception occured.

this should take @ least time, shouldn't it? thought maybe because random example choose doesnt represent properly, , in specific case in tested it doesnt slow down anything, seemed unlikely me.

then thought maybe takes such tiny amount of time isnt noticable "few" amount of executions, ran test program again total number of 10 million loopings, found prooved had found: takes pretty same time both executions.

so there logical explanation case or example-specific behaviour of try-catch?

thanks clarification in advance.

the "slowness" in throw / catch blocks comes process of throwing , catching exception, not in process of setting "traps" them. when throw exception, jvm must

  • create instance of exception
  • prepare space stack trace
  • populate stack trace prepared space
  • "unwind" stack down correct place
  • pass control exception handler.

when none of happening, jvm sticks note exception handler available @ level on stack, , continues executing actual code.

making feature penalty-free important goal language designers: programmers should not required pay things not use. otherwise, programmers tempted skip exception handling or go c ways of using status codes in order save few cpu cycles here , there, spelling end exceptions feature.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -