Results 1 to 6 of 6
  1. #1
    Bryan500 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default problem with System.nanoTime()

    hello, and thanks for any help in advance

    im having a little problem with trying to measure the time taken by a couple of algorithms to solve a problem. When the application starts the user presses a button which generates a random array of 10 integers (there is also another button which allows user to enter the 10 integers manually):
    Java Code:
                    .    ...if(e.getSource() == randB)
    	                {
    				Random r = new Random();
    				aDisplay.setText("The current array is: {");
    				
    				for(int x = 0; x < 10; x++)
    				{
    					array[x] = 1 + r.nextInt(100);
    					aDisplay.append(array[x] + ", ");
    				}
    				
    				aDisplay.append("}");				
    			}
    each algorithm is in a separate class from the above code and the algorithms used to sort the array is determined by a checkboxes:

    Java Code:
    public void itemStateChanged(ItemEvent e)
    		{			
    			bubbleSelected = bubbleC.isSelected()? true: false;
    			
    			selectionSelected = selectionC.isSelected()? true: false;
    						
    			mergeSelected = mergeC.isSelected()? true: false;
    						
    			quickSelected = quickC.isSelected()? true: false;
    		}
    next a button is pressed which will sort the array with all the selected algorithms and display the time it took,eg if "bubbleC" checkbox was selected:
    Java Code:
    display.setText(" ");
    				
    				if(bubbleSelected == true)
    				{
    					bub.setArray(array);
    					int bubA[] = new int[10];
    					
    					display.append("~~~~~~~~~~~~~~~~~~Bubble Sort~~~~~~~~~~~~~~~~~~\n");								
    					
    					long bStart = System.nanoTime();
    					bubA = bub.sort();
    					long bEnd = System.nanoTime();
    					long bTook = bEnd - bStart;									
    					
    					display.append("The sorted array is: {");
    					for(int x = 0; x < 10; x++)
    					{
    						display.append(bubA[x] + ", ");
    					}
    					display.append("}\n");
    					
    					display.append("The time taken to sort the array is: " + bTook + " nanoseconds\n\n");					
    				}
    bub is a BubbleSort object. here is the BubbleSort class:
    Java Code:
    public class BubbleSort
    {
    	private int A[];
    	
    	public BubbleSort()
    	{
    		A = new int[1];
    		A[0] = 0;
    	}
    	
    	public BubbleSort(int array[])
    	{
    		A = array;
    	}	
    	
    	public int[] sort()	
    	{
    		int temp;
    		
    		for(int x = 0; x < A.length; x++)
    		{
    			for(int y = 0; y < ( (A.length)-1 ); y++)
    			{
    				if(A[y] > A[y+1])
    				{
    					temp = A[y+1];
    					A[y+1] = A[y];
    					A[y] = temp;
    				}
    			}
    		}		
    		return A;
    	}
    	
    	public void setArray(int array[])
    	{
    		A = array;
    	}
    }
    the problem im getting is that when i initially press the sort button ill get a time taken of like around 25000 nanoseconds, however if i press the sort button again without changing the original unsorted array ill progressively get a lower and lower time. If i keep pressing it for a while it goes down to as low as less than 3000 nanoseconds when theoretically i should be getting back the same time since im using the same algorithm on the same array.

    on the other hand if i generate a new random array after each time i press the sort button it seems to work fine

    i dont know why this is happening because each time i press the sort button the original unsorted array is sent to bub and the array "bubA[]" which stores the sorted array returned by "bub.sort()" is cleared:
    Java Code:
    ...if(bubbleSelected == true)
    				{
    					bub.setArray(array);
    					int bubA[] = new int[10];
    any help would be greatly appreciated

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Probably need the whole program to see what is happening.
    Have you tried adding println() statements to see where the code is executing and when and what the different values are to see what the reasons for the different times are?
    For example is array changed after the sort? Print it out before and after the sort to see. Arrays.toString(array)

  3. #3
    Bryan500 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Probably need the whole program to see what is happening.
    Have you tried adding println() statements to see where the code is executing and when and what the different values are to see what the reasons for the different times are?
    For example is array changed after the sort? Print it out before and after the sort to see. Arrays.toString(array)
    o wow i feel so dumb for not doing that from the start. After doing that i realised that "array" is in fact being sorted even though im technically passing by value; it seems that because its an array its actually passing the references to each of its members as the value?

    to solve it i did this:
    Java Code:
    if(bubbleSelected == true)
    				{
    					int ububA[] = new int[10];
    					for(int y = 0; y < 10; y++)
    					{
    						ububA[y] = array[y];
    					}
    					
    					bub.setArray(ububA);
    					int bubA[] = new int[10];
    making a an actual copy of "array" then passing the copy to bub.sort() stops bub from modifying the original "array"

    seems to work fine now when i press sort over and over without changing the value of "array"

    thanks a lot Norm
    (sigh, i could have saved myself an hour of frustration :()

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You would do well to look into using a Java profiler here. There's a science to profiling code that you or I are likely not yet equipped to handle, and this includes improved efficiency of the Java "Hot Spot" compiler with subsequent iterations of a method, "warm-up" cycles for the JVM, and taking garbage collection into consideration. There are many profiler's out there, some are even free.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    an array its actually passing the references to each of its members as the value?
    An array is treated sort of like an object. It is passes a reference to the array when calling a method. not to its elements.

  6. #6
    Bryan500 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You would do well to look into using a Java profiler here. There's a science to profiling code that you or I are likely not yet equipped to handle, and this includes improved efficiency of the Java "Hot Spot" compiler with subsequent iterations of a method, "warm-up" cycles for the JVM, and taking garbage collection into consideration. There are many profiler's out there, some are even free.
    Thanks for pointing that out, i didnt even know these existed. I only started learning Java a few weeks ago but a profiler will definitely be useful. A quick google search turned up a few but is there any one that you would recommend?

    EDIT:
    Quote Originally Posted by Norm View Post
    An array is treated sort of like an object. It is passes a reference to the array when calling a method. not to its elements.
    thanks i will remember this.
    (come to think of it, im vaguely recalling one of my old programming teachers from about two years ago pointing out this same thing to me >.>)
    Last edited by Bryan500; 11-01-2008 at 03:44 PM.

Similar Threads

  1. JNI - No System.out for ExceptionDescribe
    By SFLeBrun in forum Advanced Java
    Replies: 0
    Last Post: 11-26-2007, 10:42 PM
  2. Getting System Properties
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-19-2007, 05:00 PM
  3. system information
    By nitinborge5 in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 09:25 AM
  4. problem trying to log on to my video library system
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-18-2007, 02:33 PM
  5. Running a system
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-29-2007, 03:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •