Re: Leap Years

new topic     » goto parent     » topic index » view thread      » older message » newer message

Christopher wrote:
> Craig wrote:
> >
> > Here's a lean version of leap year testing.  I don't know how it
> > compares to others on speed.
> > ----------------
> > global function leap_year(integer y)
> > -- return 1 for leap year, 0 for not
> >    return  ( integer(y/4)*(not integer(y/100)) + integer(y/400) )
> > end function
> > ----------------
> >
> <SKIPPED>
> The one I posted actually clocked at over 5 times faster than this
> version.
> This is probably due to your use of division. Division is the most
> expensive
> in machine cycles of the simple math operators. When speed is crucial,
> it
> should be avoided if possible. Many game programming books contain
> lengthy
> discussions just on techniques to avoid division.
>
> Christopher D. Hickman

Interesting!  I would not have guessed there would be as much
difference as that between a straight division and a remainder()
call.  I guess the combination of a straight division *and* a type
check call to integer() might be what did it.  Let me check . . .

Nope.  Just ran some tests; a division all by itself is still
slower than a remainder.  That strikes me as really odd; doesn't
it have to divide anyway to _find_ the remainder?  Oh, well.
Thanks, Christopher; now I've learned another quirk of
programming.

I tried some other comparisons, though, and found that
the type check does contribute to the problem as well . . . a type
check in this situation is actually slower than the remainder() call:

a = integer(.7)
is slower than
a = remainder(y,4)

The time jumped around a lot between being about the same (but still
slower) to being about twice as long.

So, I was dragging my code down all over the place, basically, with
both the type check and the division being slower individually than
the remainder() call and thus _much_ slower together.

One last note, which would not apply to the leap year routine:
a = integer(n)
is faster than
a = remainder(b, n)

I know that seems intuitively obvious, but I wanted to make sure
I wasn't giving the impression that I thought  remainder() is faster
than a type check on a level playing field.  It is most definitely
*not*; it's just that the respective  leapyear routines ended up
comparing remainder(b, <some integer> ) to integer(<some fraction>)
with, of course, the fraction coming from a division that is itself
slowing the whole shebang down.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
J. Craig Gilbert
cgilbert at mc.peachnet.edu
"Positing infinity, the rest is easy."
Roger Zelazny, in
'Creatures of Light and Darkness'
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu