Forums Register Login

assertion code overhead when disabled

+Pie Number of slices to send: Send
Consider this statement from the assertion spec.

Programmers developing applications for resource-constrained devices may wish to strip assertions out of class files entirely. While this makes it impossible to enable assertions in the field, it also reduces class file size, possibly leading to improved class loading performance. In the absence of a high quality JIT, it could lead to decreased footprint and improved runtime performance.


Assertions disabled, compared to the absence of assertion code, could degrade performance. Is that right?
Does your favorite exam prep book say assertions disabled do not cause any overhead?
+Pie Number of slices to send: Send
According to the Sun documentation I have seen, running with assertions disabled should cause no performance degradation.
I ran a test with 1,000,000 iterations and the difference between no assertions and assertions turned off was 1 millisecond. YMMV.
+Pie Number of slices to send: Send
Marlene, I'm moving this discussion to the Performance forum as the SCJP exam does not deal with performance issues.
You can continue this discussion there. Thank you
+Pie Number of slices to send: Send
Alas, this question does not belong in the performance forum.
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.
[ June 16, 2003: Message edited by: Marlene Miller ]
+Pie Number of slices to send: Send
The assert statement is syntatic sugar for this:

So that is how assertions when disabled could affect performance, depending on the compiler.
Oh well, since I am here... I am curious to know what a footprint is.
+Pie Number of slices to send: Send
One of the key phrases in that statement is: "In the absence of a high quality JIT."
There is no unnecessary footprint with a half-decent JIT compiler when assertions are disabled. Since assertions cannot be turned on or off in a class that's already been loaded, the JIT can either omit or include them at class loading time and never think about it again. In fact, the optimization is so simple that even a half-decent interpretter could do it.
I supposed a JVM could translate "assert Expression1 : Expression2" so that it's fundamentally equivalent to
if ($assertionEnabled && !Expression1)
throw new java.lang.AssertionError(Expression2);
but there's no reason to do so unless that JVM offers the ability to change $assertionEnabled's value for an already loaded class at runtime, which would be very non-standard.
+Pie Number of slices to send: Send
 

Originally posted by Marlene Miller:
Alas, this question does not belong in the performance forum.
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.


As was previously stated, performance questions such as these are not on the exam since performance is highly dependent on JVM implementation.
+Pie Number of slices to send: Send
Thank you David. Very helpful. It did not occur to me the JIT would actually strip out the assert statements.
Thank you Thomas. I blew it again. I'll try not to post out-of-scope questions. But if I do, you can reply Out of Scope, if you would like.
+Pie Number of slices to send: Send
Marlene,
I was trying to show people studying for the SCJP that the spec says one thing and the certification books say another.
I am worried I will get the answer wrong on the exam if I answer according to the spec.

Always trust the spec as it is the ultimate authoritative document
I'm sorry if you think that I should not have moved your post. No hard feelings
+Pie Number of slices to send: Send
Thank you Valentin. I need some more practice writing crystal clear posts. I also need a better filter for out-of-scope questions.
Your mind is under my control .... your will is now mine .... read this tiny ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1052 times.
Similar Threads
Assertions in Java 1.4
enabling & disabling assertions programatically
Assertions
Doubt in K&B SCJP 6: topic: AssertionError Source
disabled assertion & performance
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:47:06.