Measuring the time to create and destroy a simple object

Posted by portoalet on Stack Overflow See other posts from Stack Overflow or by portoalet
Published on 2010-04-03T14:06:43Z Indexed on 2010/04/03 14:13 UTC
Read the original article Hit count: 225

Filed under:
|
|
|
|

From Effective Java 2nd Edition Item 7: Avoid Finalizers

"Oh, and one more thing: there is a severe performance penalty for using finalizers. On my machine, the time to create and destroy a simple object is about 5.6 ns. Adding a finalizer increases the time to 2,400 ns. In other words, it is about 430 times slower to create and destroy objects with finalizers."

How can one measure the time to create and destroy an object? Do you just do:

long start = System.nanoTime();
SimpleObject simpleObj = new SimpleObject();
simpleObj.finalize();
long end = System.nanoTime();
long time = end - start;

© Stack Overflow or respective owner

Related posts about java

Related posts about finalizer