Results 1 to 5 of 5
Thread: Prob with an exercise
 10302007, 08:44 PM #1Member
 Join Date
 Oct 2007
 Posts
 9
 Rep Power
 0
Prob with an exercise
Hi,I'm having a very strange problem.I have written code for an exercise that calculates the square root of a number either by asking the user for input or by giving it as argument when running the prog using Newtons method(a method I must create) and Math.sqrt and printing the results to see if they are the same.That is done.The next exercise is to tamper with the code and add a method that fills an array X(declared on the top) random integers so that another method I have to create will again use Math.sqrt and Newton so that it can measure the milliseconds in execution and print which method is faster.Only when I use start=System.currentTimeMillis(),the value stored in start is like this:1.193772081281E12 and is the same in both start and end variables even if i have a for() in between.And as a result it gives 0.00 ms.
Here is my code:
Java Code:package sroot2; import java.io.*; import java.util.Random; public class SRoot2 { public static int i; public static Random random = new Random(); public final static int X=1000; public static double[] numbers=new double[X]; public static double startNewton=0, endNewton=0, startMath=0, endMath=0, timeNewton=0, timeMath=0; //Here we declare the two variables that will help us get the input from the user //and parse it into an integer. static public InputStreamReader input = new InputStreamReader(System.in); static public BufferedReader keyboardInput = new BufferedReader(input); public static void main(String[] args) throws IOException { //If the string array is empty(meaning that no arguments were given upon launch of the program), //the inputdatainteger variable calls the readline function to get an input from the user and //return an integer value.Then the program runs the same way as if args wasn't empty but only for //one value at a time.Also the program asks repeatedly for more inputs from the user until the latter //gives 0 for input. if(args.length==0){ double inputdatainteger=readLine(); while(inputdatainteger!=0){ double NewtonResUs=Newtonsqrt(inputdatainteger,1); double MathResUs=Math.sqrt(inputdatainteger); double VariationUs=Math.abs(MathResUsNewtonResUs); System.out.println("Number:"+Math.round(inputdatainteger)); System.out.println("SRoot(Newton):"+Math.round(NewtonResUs)); System.out.println("SRoot(Java Math):"+Math.round(MathResUs)); System.out.println("Variation:"+Math.round(VariationUs)); System.out.println(""); inputdatainteger=readLine(); } } else{ //If the string array is not empty it runs as SRoot. double[] argumentsdouble = new double[args.length]; double[] NewtonResults= new double[args.length]; double[] MathResults= new double[ args.length]; double[] Variation= new double[args.length]; for(i=0;i<args.length;i++){ argumentsdouble[i] = Double.parseDouble(args[i]); if(argumentsdouble[i]>0){ MathResults[i]=Math.sqrt(argumentsdouble[i]); NewtonResults[i]=Newtonsqrt(argumentsdouble[i],1); Variation[i]=Math.abs(MathResults[i]NewtonResults[i]); } else{ NewtonResults[i]=MathResults[i]=1; } } for(i=0;i<argumentsdouble.length;i++){ System.out.println("Number:"+Math.round(argumentsdouble[i])); if(argumentsdouble[i]<0){ System.out.println (argumentsdouble[i]+" ERROR: Nonpositive number"); System.out.println(""); } else{ System.out.println("SRoot(Newton):"+Math.round(NewtonResults[i])); System.out.println("SRoot(Java Math):"+Math.round(MathResults[i])); System.out.println("Variation:"+Math.round(Variation[i])); System.out.println(""); } } } fillRandom(numbers); executionTimeStatistics(numbers); } //The function that calculates the square root of a given number using Newton's method. //It runs by using the guess and the number recursively until the difference between the //x and newx is smaller than the threshold.Which means that we have more or less found the square root. public static double Newtonsqrt(double B,double x){ double newX; double threshold = 0.1; newX = 0.5 * (x + B / x); if (Math.abs(x  newX) > threshold) { newX = Newtonsqrt(B, newX); } return newX; } //This function reads the input from the keyboard and parses it(as it is a string) into an integer //before returning it for storage. public static double readLine() throws IOException{ System.out.print("Input an integer: "); String ss = keyboardInput.readLine(); double inputdatainteger = Double.parseDouble(ss); return inputdatainteger; } public static void fillRandom(double[] numbers){ int rn=Math.abs(random.nextInt(300)); for(i=0;i<X;i++){ numbers[i]=(double)rn; rn=Math.abs(random.nextInt(300)); } } public static void executionTimeStatistics(double[] numbers){ startNewton = System.currentTimeMillis(); for(i=0;i<X;i++){ Newtonsqrt(numbers[i],1); } endNewton = System.currentTimeMillis(); System.out.println(startNewton+" "+endNewton); //timeNewton=endNewtonstartNewton; startMath = System.currentTimeMillis(); for(i=0;i<X;i++){ Math.sqrt(numbers[i]); } endMath = System.currentTimeMillis(); timeMath=endMathstartMath; System.out.print("Statistics for "+X+" computations.\nExecution time using Newton: "+timeNewton+" ms.\nExecution time using Math: "+timeMath+" ms.\n"); if(timeMath<timeNewton){ System.out.print("The winner is Java.Math.\n"); } else if(timeMath==timeNewton){ System.out.print("The winner is ................ none.This is a draw.\n"); } else{ System.out.print("The winner is Newton.\n"); } } }
PS:sorry if some things are a little sloppy,I'm trying several things to debug this.Last edited by JavaBean; 10302007 at 09:11 PM. Reason: Code placed inside [code] tag.
 10302007, 09:15 PM #2
Windows does not count/report every milliseconds to you. There is a granularity in reporting which depends on the underlying OS.
If it reports the same number, that means your procedure does not run long enough to capture the difference. You can try your code with a dummy for loop which took more time..
And here is an article that will tell you more details about this problem:
Bring Java's System.currentTimeMillis() Back into the Fold for Transaction Monitoring
 10302007, 09:23 PM #3Member
 Join Date
 Oct 2007
 Posts
 9
 Rep Power
 0
I have to say that I followed the form that my professor has given into powerpoint slides and it has to work cause his produces results.Now if I understand correctly you are implying that if I use it on Linux it should work or at least has better chances than windows???
PS:If it isn't what you are saying please forgive my noobity,I only recently began this course.
 10302007, 09:45 PM #4
I saw the same problem on Windows before but i did not test it on Linux. That tutorial tells this:
"The resolution or granularity of a clock is the smallest time difference that can be measured between two consecutive calls. For example, on many Microsoft Windows systems, the resolution of the call to currentTimeMillis is 10 ms. This is because the native implementation makes an operatingsystem call that has a resolution of 10 ms. Most other operating systems claim to have a resolution of 1 ms."
So you look like to have a better chance.
But you should check your method for inputs which may take more time to compute. Your prof. may use inputs which take more time! (Note that i am not thinking about newton method now or checking your code. Based on your input, i told you my quick informed guesses.)
 02152008, 06:11 PM #5Member
 Join Date
 Feb 2008
 Posts
 2
 Rep Power
 0
Similar Threads

Need help on a strange file access prob
By squad in forum Advanced JavaReplies: 0Last Post: 03212008, 10:55 AM 
Threading prob..
By banie in forum Java AppletsReplies: 0Last Post: 02052008, 06:30 AM 
having prob with Exception
By eva in forum New To JavaReplies: 1Last Post: 01042008, 05:44 PM 
I/O exercise
By Feldom in forum New To JavaReplies: 1Last Post: 10282007, 04:48 PM 
help with exercise
By e_as're in forum New To JavaReplies: 3Last Post: 09252007, 10:14 AM
Bookmarks