- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
So I guess you expect the compiler to somehow predict whether each and every result it puts on the stack (e.g. the value of "i" before incrementation) gets used. And if it doesn't get used, then there's no need to worry about evaluating said result correctly, right?
I realize that a lot of compilers do exactly that, but I am less than enthusiastic about it. It seems like a great deal more work for the compiler (and its designers) to "predict the future" than it would be for you to simply use "++i" instead of "i++" if that's what you mean.
Besides, in a real language like C, your code is not executing in a "sandbox." You can't assume that everything that might be affected by your process' memory is right there in the source code being compiled.
Admin
The code snippet does not define the array in question, therefore we and the compiler cannot know whether it is a local or instance variable. If it is an instance variable, the compiler must prepare for another thread changing the array completely (ie, replacing the array with a different length array).
So, if the array is shared, then the bounds check must be done every time the array is accessed. This kind of try-catch-kludgery will be faster in that case. But if the array is local, then the compiler can move the bounds check outside the loop and then a regular for-loop will be faster.
How much you ask?
try-catch with shared array: 115ms
for with shared array: 120ms
try-catch with local array: 115ms
for with local array: 95ms
This with 10000000 (10 million) integers. Insignificant. No-one should ever use the try-catch-kludgery.
Admin
That is completely incorrect. You may assume so but since we do not know where the array is defined, that assumption is not valid. If the array is an instance variable there is nothing that prevents the displayProductInfo or possibly some other thread from accessing the array variable from elsewhere and changing it into a different length array.
In this case, as I wrote above, the try-catch-kludge would be faster by a completely insignificant margin.
This pattern does come from Java performance tuning handbooks. I can't believe how many performance tuning books still repeat crap like this. This belongs to the same category as "object creation is slow so objects should be pooled" and "synchronization is slow".
Admin
int i = x;
for (; --i != 0;) { statements; }
I think checking if one number is bigger than second is slower than checking if number is equal to 0.
Admin
Correct. Java specificies separate opcodes for comparison with zero, so you save the cost of loading a local onto the stack. Not a big savings, however.
Admin
I would notice that this way to go through an array is most effective, since check of bounds is included already violate you it or not, but no any additional check applied
thus i would not lough at the code - it's just cool
Admin
The iterator contract in Python relies on that pattern. http://en.wikipedia.org/wiki/Iterator#Python
Admin
No, it's not, because you can't delete the reference of a passed object.
Admin
No, this is how a median can be interpreted. Average is statistical function constructed different way, so with different possible interpretation.
@see http://en.wikipedia.org/wiki/Median @see http://en.wikipedia.org/wiki/Median