> I tried to duplicate your results, but didn't succeed.
> Not knowing what a Task object might be I simply used int[]
[quoted text clipped - 18 lines]
> flow should be handled without exceptions; use exceptions
> only for the truly exceptional situations.
Hi, thanks for your swift reply.
Thank you for putting so much effort in, i feel really bad now, and im
really grateful!!
I understand what you're saying about the try/catch, however the "if"
method seemed to make it slower rather than faster for me (probably
because it has to to the comparison every cycle, most of which times it
will be false?)
A task is a tuple (can you say that in java?) of a string and an int, so
assuming it as an int is ok.
ive chopped down my code to just the insert and next functions (insert
inserts a new node, then performs an upHeap operation, next returns the
root, then replaces with the last and downHeaps) and ive written a quick
tester program to demonstrate what i mean. Below are links to it (ive
uploaded them because posting them here just messes with the formatting
- i hope you dont mind)
http://www.190385.com/java/myHeap.java
http://www.190385.com/java/Task.java
http://www.190385.com/java/Tester2.java
I'd be really appreciative if you could help me out some more because
this is really bugging me.
Many thanks in advance,
Andrew Bullock
Stefan Schulz - 27 Nov 2004 08:55 GMT
> I understand what you're saying about the try/catch, however the "if"
> method seemed to make it slower rather than faster for me (probably
> because it has to to the comparison every cycle, most of which times it
> will be false?)
Since the array index is checked anyway (otherwise, how would the Exception
ever be generated?) you do not lose much by an explicit if. Maybe your JIT
will even see that you checked the array size already, and omit the later
check.
If the horrendous control flow doesn't convice you, consider this:
Exceptions are
_expensive_. They cost over a thousand times more runtime then a simple
if-then-else. The Exception needs to be constructed, the normal method
needs to
be destroyed, the corresponding catch-block needs to be looked up, the
stack
squashed to that level, the old method reanimated...

Signature
Whom the gods wish to destroy they first call promising.
Eric Sosman - 29 Nov 2004 22:34 GMT
> I understand what you're saying about the try/catch, however the "if"
> method seemed to make it slower rather than faster for me (probably
> because it has to to the comparison every cycle, most of which times it
> will be false?)
Please refer to my recent unseemly diatribe on essentially
the same topic over in comp.lang.java.programmer, in a thread
titled "Performance of null tests?" (Not self-promotion: it's
just that I'm trying to cut down on writing and re-writing the
same diatribe -- bad for the blood pressure.)
(For extra credit, spot the error in my broadside -- not
in the assumptions, which are intentionally optimistic to the
point of bending over backwards, but in the conclusions I drew
from them. Hint: Spaces are not lines.)
"It stands to reason" that your option a should be slower,
and when I replicated your experiment (based on an incomplete
description), that's exactly what happened. You're seeing
the opposite -- and I strongly suspect that it has nothing
whatever to do with the `if', but everything to do with some
other part of your code, or with a sloppy time measurement.
Seek elsewhere for the source of your troubles -- but don't
seek harder than the troubles themselves are worth. Remember
that you'll need to realize your 100ms savings 36,000 times
to recoup just one hour spent achieving it ...

Signature
Eric.Sosman@sun.com