I think a lot of these functions are implemented in hardware, so making interpreted software variants may not help. -- Kawigi |
I think a lot of these functions are implemented in hardware, so making interpreted software variants may not help. -- Kawigi Yeah, square roots should be handled in the math units in your processor. I recall reading somewhere they took about 60-70 cycles on a Pentium II era processor (don't have a source for this), which is slow for hardware, but still blowing away anything you'll ever do in software. This means picking a faster algorithm helps jack all if you're moving it from a one-step in pure hardware to a series of math calls in a software virtual machine :p If this were C or something then we could probably still talk, but in java I think this is just go-kart effect that traps some developers (oo.. I wrote a faster algorithm I can save cycles.. uh.. let's discount the fact that I'm probably spending the 70 cycles the original took in overhead for the while statement on it's own). -- Kuuran |
// very fast square root approximation public final static int sqrt( long val ) { long temp, g = 0, b = 0x8000, bshft = 15; do { if( val >= ( temp = ( ( ( g << 1 ) + b ) << bshft-- ) ) ) { g += b; val -= temp; } } while( ( b >>= 1 ) > 0 ); return (int)g; }
Hang on... that casts the answer into an int. That's a little TOO approximate for my liking. -- Tango
It's also about 10 times slower than Math.sqrt() on my machine... Maybe this whole idea was bad. :-p --David Alves
I think a lot of these functions are implemented in hardware, so making interpreted software variants may not help. -- Kawigi
Yeah, square roots should be handled in the math units in your processor. I recall reading somewhere they took about 60-70 cycles on a Pentium II era processor (don't have a source for this), which is slow for hardware, but still blowing away anything you'll ever do in software. This means picking a faster algorithm helps jack all if you're moving it from a one-step in pure hardware to a series of math calls in a software virtual machine :p If this were C or something then we could probably still talk, but in java I think this is just go-kart effect that traps some developers (oo.. I wrote a faster algorithm I can save cycles.. uh.. let's discount the fact that I'm probably spending the 70 cycles the original took in overhead for the while statement on it's own). -- Kuuran