August 21, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carney | John Carney wrote:
> As an aside, I do recall reading somewhere that in Java, the following code:
>
> for (i = 0 ; i != anArray.length ; i++)
> doSomething(anArray[i]) ;
>
> could be written faster as:
>
> try
> {
> i = 0 ;
> while (true)
> doSomething(anArray[i++]) ;
> }
> catch (ArrayBoundsException &x)
> {}
>
> Don't quote me on that - it was a long time ago and may be complete bollocks.
That doesn't seem plausible to me, but I'll confess total ignorance of the JVM. In any case, the time savings wouldn't be worth it: you'd get fired at the next code review.
-RB
|
August 21, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carney | John Carney wrote:
> "Charles Hixson" <charleshixsn@earthlink.net> wrote in message
> news:3B816D1F.80309@earthlink.net...
>
> <snip>
>...
> AFAIK the big cost in exception handling is cleaning up the stack.
>
> As an aside, I do recall reading somewhere that in Java, the following code:
>
> for (i = 0 ; i != anArray.length ; i++)
> doSomething(anArray[i]) ;
>
> could be written faster as:
>
> try
> {
> i = 0 ;
> while (true)
> doSomething(anArray[i++]) ;
> }
> catch (ArrayBoundsException &x)
> {}
>
> Don't quote me on that - it was a long time ago and may be complete
> bollocks.
>
> Cheers,
> John.
>
An excellent example (well, not quite, as the exception case is the longer, but that's because it's a trivial example.
If one is doing this, how does one decide which is the better choice? It doesn't seem to be documented anywhere. Probably it's "implementation dependant", but there don't even seem to be any reasonable guidelines. Is it reasonable to handle an EOF detect by exception? How long a file is required before that's the faster way to do things, and is there some other reason why it's undesireable?
Different languages seem to have different assumptions about this, and it tends to be quite a pain to figure it out in each one. (Java is one of the more open about this, and is also relatively tolerant of exceptions.)
|
August 22, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles Hixson |
Charles Hixson wrote:
> (Someone once told me that it took 1000 times
> as long to process an exception as to process a branch.
Depends on the implementation, but that might be a good lower bound for the simplest case :-/
Christophe
|
August 23, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christophe de Dinechin | "Christophe de Dinechin" <descubes@earthlink.net> wrote in message news:3B841CA1.8A08E9E6@earthlink.net... > > > Charles Hixson wrote: > > > (Someone once told me that it took 1000 times > > as long to process an exception as to process a branch. > > Depends on the implementation, but that might be a good lower bound for the > simplest case :-/ Bollocks! |
August 23, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Christophe de Dinechin | "Christophe de Dinechin" <descubes@earthlink.net> wrote in message news:3B841CA1.8A08E9E6@earthlink.net... > > > Charles Hixson wrote: > > > (Someone once told me that it took 1000 times > > as long to process an exception as to process a branch. > > Depends on the implementation, but that might be a good lower bound for the > simplest case :-/ With today's C++ implementations, throw/catch is pretty quick. But that's because, upon entry, each C++ function sets up exception handling stuff in the stack frame, and removes it at exit. But some application markets don't like having to pay this "penalty" when so few exceptions are actually processed. To try to appease these folk, the C++ language spec says that exceptions are the exception (yuk yuk), not the norm, and so exception handling may be arbitrarily complex. This would (theoretically) allow an implementation to do thrown exceptions the *hard* way, by comparing stack frame addresses against a link map to find exception handlers, rather than require each function to run exception set-up and take-down even when no exceptions are thrown. -- Richard Krehbiel, Arlington, VA, USA rich@kastle.com (work) or krehbiel3@home.com (personal) |
August 24, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Bornschlegel | Russell Bornschlegel wrote:
>
> John Carney wrote:
> > As an aside, I do recall reading somewhere that in Java, the following code:
> >
> > for (i = 0 ; i != anArray.length ; i++)
> > doSomething(anArray[i]) ;
> >
> > could be written faster as:
> >
> > try
> > {
> > i = 0 ;
> > while (true)
> > doSomething(anArray[i++]) ;
> > }
> > catch (ArrayBoundsException &x)
> > {}
> >
> > Don't quote me on that - it was a long time ago and may be complete bollocks.
>
> That doesn't seem plausible to me, but I'll confess total ignorance of the JVM. In any case, the time savings wouldn't be worth it: you'd get fired at the next code review.
>
> -RB
Actually, I'm scared. It makes sense to me. In the first case you are
bounds checking twice each iteration. Once in the for expression and
once in the array access. In the second one, you check once, and there
doesn't appear to be much (or any) of a stack clean up. Beside I don't
think java deallocates immediately in this case. Provided that
anArray.length is large enough this make perfect sense.
It must be time to sleep.
Dan
|
August 24, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dan Hursh | Dan Hursh wrote:
> Russell Bornschlegel wrote:
> >
> > John Carney wrote:
> > > As an aside, I do recall reading somewhere that in Java, the following code:
> > >
> > > for (i = 0 ; i != anArray.length ; i++)
> > > doSomething(anArray[i]) ;
> > >
> > > could be written faster as:
> > >
> > > try
> > > {
> > > i = 0 ;
> > > while (true)
> > > doSomething(anArray[i++]) ;
> > > }
> > > catch (ArrayBoundsException &x)
> > > {}
> > >
> > > Don't quote me on that - it was a long time ago and may be complete bollocks.
> >
> > That doesn't seem plausible to me, but I'll confess total ignorance of the JVM. In any case, the time savings wouldn't be worth it: you'd get fired at the next code review.
> >
> > -RB
>
> Actually, I'm scared. It makes sense to me. In the first case you are
> bounds checking twice each iteration. Once in the for expression and
> once in the array access.
This isn't necessarily true. A good optimizer compiler should (not saying would - just should) know that you are doing the bounds checking against the array length and thus it should disable its bounds checking code on that array access. Thus, if you are writing performance critical code - and you are using a mature D optimizer, right? - the first example should actually be as fast as the second, and it should be smaller, as the compiler does not have to include exception throwing/catching code.
|
August 24, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis |
Russ Lewis wrote:
>
> Dan Hursh wrote:
>
> > Russell Bornschlegel wrote:
> > >
> > > John Carney wrote:
> > > > As an aside, I do recall reading somewhere that in Java, the following code:
> > > >
> > > > for (i = 0 ; i != anArray.length ; i++)
> > > > doSomething(anArray[i]) ;
> > > >
> > > > could be written faster as:
> > > >
> > > > try
> > > > {
> > > > i = 0 ;
> > > > while (true)
> > > > doSomething(anArray[i++]) ;
> > > > }
> > > > catch (ArrayBoundsException &x)
> > > > {}
> > > >
> > > > Don't quote me on that - it was a long time ago and may be complete bollocks.
> > >
> > > That doesn't seem plausible to me, but I'll confess total ignorance of the JVM. In any case, the time savings wouldn't be worth it: you'd get fired at the next code review.
> > >
> > > -RB
> >
> > Actually, I'm scared. It makes sense to me. In the first case you are
> > bounds checking twice each iteration. Once in the for expression and
> > once in the array access.
>
> This isn't necessarily true. A good optimizer compiler should (not saying would - just should) know that you are doing the bounds checking against the array length and thus it should disable its bounds checking code on that array access. Thus, if you are writing performance critical code - and you are using a mature D optimizer, right?
No, the D optimizer doesn't work so well on Java.
-R
|
August 24, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russell Bornschlegel | Russell Bornschlegel wrote:
> Russ Lewis wrote:
> >
> > Dan Hursh wrote:
> >
> > > Russell Bornschlegel wrote:
> > > >
> > > > John Carney wrote:
> > > > > As an aside, I do recall reading somewhere that in Java, the following code:
> > > > >
> > > > > for (i = 0 ; i != anArray.length ; i++)
> > > > > doSomething(anArray[i]) ;
> > > > >
> > > > > could be written faster as:
> > > > >
> > > > > try
> > > > > {
> > > > > i = 0 ;
> > > > > while (true)
> > > > > doSomething(anArray[i++]) ;
> > > > > }
> > > > > catch (ArrayBoundsException &x)
> > > > > {}
> > > > >
> > > > > Don't quote me on that - it was a long time ago and may be complete bollocks.
> > > >
> > > > That doesn't seem plausible to me, but I'll confess total ignorance of the JVM. In any case, the time savings wouldn't be worth it: you'd get fired at the next code review.
> > > >
> > > > -RB
> > >
> > > Actually, I'm scared. It makes sense to me. In the first case you are
> > > bounds checking twice each iteration. Once in the for expression and
> > > once in the array access.
> >
> > This isn't necessarily true. A good optimizer compiler should (not saying would - just should) know that you are doing the bounds checking against the array length and thus it should disable its bounds checking code on that array access. Thus, if you are writing performance critical code - and you are using a mature D optimizer, right?
>
> No, the D optimizer doesn't work so well on Java.
LOL! Ok, fair enough. It's just whenever I see some discussion here, I think of its applicability to D :p
|
September 23, 2001 Re: Exception specification? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Carney | I'm going to rethink it. I do read all of the arguments <g>. -Walter John Carney wrote in message <9lntkc$1p07$1@digitaldaemon.com>... >Seriously Walter, you've got it right on so many other fronts, I'd really like you to reconsider your stance on this one. Think of it this way: if it is decided five years down the track that rigid exception specification is, after all, a good thing, you're going to break an awful lot of code if you try to tack it onto the language. On the other hand, if we in the pro-specification camp turn out to be wrong, then relaxing the rules isn't going to render anybody's code base inoperable. > >Cheers, >John Carney. > > > |
Copyright © 1999-2021 by the D Language Foundation