Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2007, 09:44 PM
Member
 
Join Date: Oct 2007
Posts: 9
jhetfield18 is on a distinguished road
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:

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(MathResUs-NewtonResUs); 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: Non-positive 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=endNewton-startNewton; startMath = System.currentTimeMillis(); for(i=0;i<X;i++){ Math.sqrt(numbers[i]); } endMath = System.currentTimeMillis(); timeMath=endMath-startMath; 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 : 10-30-2007 at 10:11 PM. Reason: Code placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-30-2007, 10:15 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-30-2007, 10:23 PM
Member
 
Join Date: Oct 2007
Posts: 9
jhetfield18 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-30-2007, 10:45 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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 operating-system 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.)
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-15-2008, 07:11 PM
Member
 
Join Date: Feb 2008
Posts: 2
CSnoob87 is on a distinguished road
Hey all...new to Java
ooops wrong forum...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on a strange file access prob squad Advanced Java 0 03-21-2008 11:55 AM
Threading prob.. banie Java Applets 0 02-05-2008 07:30 AM
having prob with Exception eva New To Java 1 01-04-2008 06:44 PM
I/O exercise Feldom New To Java 1 10-28-2007 05:48 PM
help with exercise e_as're New To Java 3 09-25-2007 11:14 AM


All times are GMT +3. The time now is 12:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org