0
import java.math.BigDecimal; public class testtest { public static final BigDecimal TWO = new BigDecimal(2); public static final int digits = 1000; public static final BigDecimal TOLERANCE = BigDecimal.ONE.scaleByPowerOfTen(-digits); public static double MidpointMethod = 0; public static long MidpointMethod(int n) { BigDecimal k = new BigDecimal(n); BigDecimal a = BigDecimal.ONE; // set a to be one BigDecimal b = k; // set b to be two long start = System.nanoTime(); // start the timer while(a.multiply(a).subtract(k).abs().compareTo(TOLERANCE) >= 0) { // while our decimals aren't close enough to the square root of two if(a.multiply(a).subtract(k).abs().compareTo(b.multiply(b).subtract(k).abs()) > 0) // if a is farther from the square root of two than b a = a.add(b).divide(TWO); // set a to be the average of a and b else // if a is closer to the square root of two than b b = a.add(b).divide(TWO); // set b to be the average of a and b } return System.nanoTime() - start; // return the time taken } public static void main(String[] args) { System.out.println(MidpointMethod(2)/10e6); } } 

This program outputs 6224.5209, but when I ran it it took way, way over 20 seconds to run. Why does it display 6 seconds when it actually took more than 20 seconds?

is the 6 seconds an accurate and precise measure of how long the program took?

6
  • 1
    Did you read the javadoc? It's not nanosecond accuracy (or related to system or wall-time). Commented Dec 21, 2013 at 23:06
  • Should be second accuracy, though. Commented Dec 21, 2013 at 23:07
  • @MichaelPetrotta Maybe... depends on what the system is doing. Commented Dec 21, 2013 at 23:07
  • I had lots of programs running in the background, and that probably decreased the time, but still the runtime and the displayed time should be the same right Commented Dec 21, 2013 at 23:11
  • Please fix the syntax error, and make sure we're running the same code that you are. Commented Dec 21, 2013 at 23:12

2 Answers 2

4

To convert nanoseconds to milliseconds (which I assume you were trying), divide by 1e6, not 10e6. You are off by a factor of 10.

Sign up to request clarification or add additional context in comments.

1 Comment

On the other hand, 63 seconds is definitely way, way over 20 seconds. So you made a good call there. Just remember in the future, the e is shorthand for *10^, so powers of ten should generally be in the form 1.21e9. The only time you want more than one digit before the decimal is if want to use "engineering notation" for clarity, where the magnitude after the e is always a multiple of three (thousands, millions, billions, thousandths, etc.).
0

The Syste.nanoTime() is fully accurate given that you work on a decent PC, which I'll assume you are. The problem is any kind of initialisation before you call for the first time the method, including JVM start up, stack set up, heap set up, the Big decimals initialisation takes some time. Also if you are using a lot of your RAM and it is almost full that boot up time can go even more.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.