Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (756)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Can someone explain what is going on with this square rooting?  (Read 15461 times)
0 Members and 1 Guest are viewing this topic.
Offline danieldean

Senior Devvie


Medals: 5
Projects: 1



« Posted 2014-07-18 11:46:28 »

Hi

Dealing with vectors using floats but using Math.sqrt() and a cast to find magnitude, etc., I started to wonder if wasting the extra digits from the returned double was 'okay' and if it could be done any faster. So some reading and testing different methods and I largely realised I won't beat hardware but then some strange things happened. See here:

https://gist.github.com/danieldean/5d2abab25bc9b8041d1e

When run the method 'slow()' which I named so because I though it would be, frequently out performs Java's native methods with negligible difference in accuracy. Clearly it would stray off as numbers go higher but if it were tuned to the application is this not going to prove faster?

I probably would never bother using anything but Math.sqrt() but I'd like to know what's going on here...  Huh
Offline delt0r

JGO Wizard


Medals: 143
Exp: 18 years


Computers can do that?


« Reply #1 - Posted 2014-07-18 12:04:13 »

You really really really shouldn't try to fix performance when you don't have performance issues. About the only time sqrt is going to matter in performance is if you do software lighting and transformations. Which these days you shouldn't be doing. At least not much.

Also you are doing microbenchmarks which are almost always wrong.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #2 - Posted 2014-07-18 12:08:10 »

using approx squares is totally fine .. http://en.wikipedia.org/wiki/Fast_inverse_square_root

but using this early in the process is just annoying. Math.sqrt will always yield 'good' resulsts while hacky approximations only work in limited ranges - but they are faster in the end.

i guess your excessive use of System.nanoTime is screwing up your numbers.  Clueless
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline danieldean

Senior Devvie


Medals: 5
Projects: 1



« Reply #3 - Posted 2014-07-18 12:12:19 »

I'm not actually trying to 'fix it', this is purely out of interest. I'm aware there are probably many pitfalls which is why I've asked so that I may learn... something to do with the timing being wrong is my assumption.
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #4 - Posted 2014-07-18 18:20:40 »

nanoTime() is fairly inaccurate in most cases. It's better to use getTimeMillis(). (generally) Edit: <-- bad advice.... my bad.

Also, to get a good read on how accurate a benchmark is you'll need to run timings on millions/billions of operations, even then there are so many factors that come into play on computers, not even mentioning the JVM, that it's difficult at best.

I fell into this a while ago and finally gave up, because my square roots are negligible compare to other parts of my code.

Every village needs an idiot Cool
Offline theagentd
« Reply #5 - Posted 2014-07-18 18:28:53 »

nanoTime() is fairly inaccurate in most cases. It's better to use getTimeMillis(). (generally)
Really? I have the exact opposite experience. On some Windows machines getTimeMillis() as an actual accuracy of around 15ms, e.g. the timer only updates every 15ms, so you get horrible precision. nanoTime() has always worked reliably for me.

Myomyomyo.
Offline BurntPizza

« JGO Bitwise Duke »


Medals: 485
Exp: 7 years



« Reply #6 - Posted 2014-07-18 18:33:08 »

Maybe that was just a brainfart, but don't use getTimeMillis(). nanoTime() is superior.
Offline thedanisaur

JGO Knight


Medals: 59



« Reply #7 - Posted 2014-07-18 18:53:45 »

I think you're confusing my meaning, nanoTime() has better precision, currentTimeMillis() (I screwed up the name) has better accuracy.

Also, I wouldn't say that nanoTime() is superior as it's not thread safe and if you call nanoTime() and get 443452 then call nanoTime() a nano second later you aren't guaranteed to get 443453...

Don't get me wrong they both have their uses, but nanoTime() is not accurate. Also if you're measuring differences in performance by nano seconds you're doing it wrong anyway.

Edit: I lied! I forgot about including the JVM, use nanoTime() unless you can't....

Every village needs an idiot Cool
Offline Roquen

JGO Kernel


Medals: 517



« Reply #8 - Posted 2014-07-24 07:59:55 »

Hotspot emits 'sqrtss' if you properly write your code.  Doing anything else will be slower.
Pages: [1]
  ignore  |  Print  
 
 

 
DesertCoockie (52 views)
2018-05-13 18:23:11

nelsongames (83 views)
2018-04-24 18:15:36

nelsongames (74 views)
2018-04-24 18:14:32

ivj94 (759 views)
2018-03-24 14:47:39

ivj94 (87 views)
2018-03-24 14:46:31

ivj94 (643 views)
2018-03-24 14:43:53

Solater (102 views)
2018-03-17 05:04:08

nelsongames (184 views)
2018-03-05 17:56:34

Gornova (426 views)
2018-03-02 22:15:33

buddyBro (1086 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!