Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (803)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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 16754 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 »


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:

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: 145
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 ..

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.

Offline BurntPizza

« JGO Bitwise Duke »

Medals: 486
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: 518

« 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  

Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4584 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

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

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 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‑
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!