Results 1 to 6 of 6
Thread: problem with System.nanoTime()
- 11-01-2008, 01:39 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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):
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:. ...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("}"); }
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: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; }
bub is a BubbleSort object. here is the BubbleSort class: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"); }
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.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; } }
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:
any help would be greatly appreciatedJava Code:...if(bubbleSelected == true) { bub.setArray(array); int bubA[] = new int[10];
- 11-01-2008, 01:48 PM #2
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)
- 11-01-2008, 02:24 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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:
making a an actual copy of "array" then passing the copy to bub.sort() stops bub from modifying the original "array"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];
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 :()
-
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.
- 11-01-2008, 03:36 PM #5
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.an array its actually passing the references to each of its members as the value?
- 11-01-2008, 03:39 PM #6
Member
- Join Date
- Nov 2008
- Posts
- 3
- Rep Power
- 0
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:
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
-
JNI - No System.out for ExceptionDescribe
By SFLeBrun in forum Advanced JavaReplies: 0Last Post: 11-26-2007, 10:42 PM -
Getting System Properties
By Java Tip in forum Java TipReplies: 0Last Post: 11-19-2007, 05:00 PM -
system information
By nitinborge5 in forum New To JavaReplies: 1Last Post: 08-07-2007, 09:25 AM -
problem trying to log on to my video library system
By warship in forum New To JavaReplies: 0Last Post: 07-18-2007, 02:33 PM -
Running a system
By ai_2007 in forum Advanced JavaReplies: 1Last Post: 06-29-2007, 03:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks