Results 1 to 9 of 9
Thread: Help Needed!
- 12-09-2009, 02:53 PM #1
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
Help Needed!
i have this piece of code which i have written, the problem is i need to display the best time out of 5 running times, but the arrays which are being sorted have the same size and range which are defined by the user, so the code runs 5 times, shows 5different times for each sort, but i need to display the best time and display the worst time any ideas?Java Code:long StartTime, EndTime, ElapsedTime; StartTime=System.nanoTime(); sorter.sort(); EndTime=System.nanoTime(); //the time after the run ElapsedTime= EndTime- StartTime; //calculates the difference between time after run and start System.out.println("Insertion Sort Algorithm running time (ns): " + ElapsedTime +"ns"); //this prints the time System.out.println(""); long StartTime1, EndTime1, ElapsedTime1; StartTime1=System.nanoTime(); sorter1.sort(); EndTime1=System.nanoTime(); //the time after the run ElapsedTime1= EndTime1- StartTime1; //calculates the difference between time after run and start System.out.println("Insertion Sort Algorithm running time (ns): " + ElapsedTime1 +"ns"); //this prints the time System.out.println(""); long StartTime2, EndTime2, ElapsedTime2; StartTime2=System.nanoTime(); sorter2.sort(); EndTime2=System.nanoTime(); //the time after the run ElapsedTime2= EndTime2- StartTime2; //calculates the difference between time after run and start System.out.println("Insertion Sort Algorithm running time (ns): " + ElapsedTime2 +"ns"); //this prints the time System.out.println(""); long StartTime3, EndTime3, ElapsedTime3; StartTime3=System.nanoTime(); sorter3.sort(); EndTime3=System.nanoTime(); //the time after the run ElapsedTime3= EndTime3- StartTime3; //calculates the difference between time after run and start System.out.println("Insertion Sort Algorithm running time (ns): " + ElapsedTime3 +"ns"); //this prints the time System.out.println(""); long StartTime4, EndTime4, ElapsedTime4; StartTime4=System.nanoTime(); sorter4.sort(); EndTime4=System.nanoTime(); //the time after the run ElapsedTime4= EndTime4- StartTime4; //calculates the difference between time after run and start System.out.println("Insertion Sort Algorithm running time (ns): " + ElapsedTime4 +"ns"); //this prints the time System.out.println(""); long best, worst, average; average = (ElapsedTime + ElapsedTime1 + ElapsedTime2 + ElapsedTime3 + ElapsedTime4)/5;
i can work out the average time as you can see.
- 12-09-2009, 03:28 PM #2
Yep:
And read the code conventions (variables start with lower case letters) and declare a method. Your code repeats itself.Java Code:long[] times = new long[5]; // add elapsedTimes to this array in your code java.util.Arrays.sort(times); System.out.println("best: " + times[0]); System.out.println("slow: " + times[5]);Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-09-2009, 03:40 PM #3
I would consider doing this kind of thing in a loop, but even if you do it 5 times over like this,
I would use a kind of a time keeper structure, to store the start and stop time for each execution. and then i would have a list of these individual exection objects, and then after all executions have completed, go over each time keeping structure and compute the min, max,average, and total times.
Here is a sample of this, where I also created a summary structure to store the computed results.
Java Code:import java.util.ArrayList; import java.util.List; /** *Created on Dec 9, 2009 */ /** * */ public class TestRunTime { public void run() { Statistics stats = new Statistics(); // run the thing 5 times, and collect the time it took to run, add it to statistics processing component for (int i = 0; i < 5; i++) { ExecutionTime t = new ExecutionTime(); t.start(); // how ever you set up the sorter thing // sorter.sort(); t.stop(); System.out.println(t); stats.addExecutionTime(t); } // for ExecutionSummary summary = stats.getSummary(); System.out.println(summary); } } class Statistics { List<ExecutionTime> executionTimes = new ArrayList<ExecutionTime>(); public void addExecutionTime(ExecutionTime item) { executionTimes.add(item); } public ExecutionSummary getSummary() { ExecutionSummary summary = new ExecutionSummary(); for (ExecutionTime item : executionTimes) { long elapsedTime = item.getElapsedTime(); summary.totalTime += elapsedTime; summary.numberExecutions ++; // compute the min and max time if (summary.minTime == -1 || elapsedTime < summary.minTime) { summary.minTime = elapsedTime; } if (summary.maxTime == -1 || elapsedTime > summary.maxTime) { summary.maxTime = elapsedTime; } } summary.averageTime = summary.totalTime / summary.numberExecutions; return summary; } } /** * A structure to hold the summarized results. * */ class ExecutionSummary { public long averageTime; public long minTime = -1; public long maxTime = -1; public long totalTime; public int numberExecutions; @Override public String toString() { StringBuilder b = new StringBuilder(); b.append(" minTime: ").append(minTime); b.append(" maxTime: ").append(maxTime); b.append(" average: ").append(averageTime); b.append(" totalTime: ").append(totalTime); b.append(" iterations: ").append(numberExecutions); return b.toString(); } } /** * Holds the counters for a single execution */ class ExecutionTime { long startTime; long stopTime; public void start() { startTime = System.nanoTime(); } public void stop() { stopTime = System.nanoTime(); } public long getElapsedTime() { return stopTime - startTime; } @Override public String toString() { return "run time: " + getElapsedTime() + " ns."; } }
- 12-09-2009, 03:41 PM #4
.. oh,that other way is nice and simple too I guess :)
- 12-09-2009, 05:11 PM #5
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
i am having trouble implementing both of them! what am i doing wrong??
i have uploaded my code with this post can you take a look at it and tell me where its going wrong please?
- 12-09-2009, 05:21 PM #6
Why don't you post the code and tell us what's going wrong?
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 12-09-2009, 05:45 PM #7
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
i did post the code its in the post just above yours in a zip file....
whats going wrong is basically
once the algorithm sorts the array the running time is displayed
this is done 5 times to acquire 5 running times
the lowest running time is the Best case
the highest running time is the worst case
i am having trouble displaying the highest time and lowest time
i tried using the two above codes but it just wont compile corrrectly for me
-
A few recommendations: The easier you make it for others to read your code and understand your problem, the greater your chance of getting decent help. Since it's much easier to read a small bit of code posted in the forum vs. downloading a zip file, unzipping, and going through unknown amounts of code, many more will read your code if you post it here.
I recommend you post your newest code that uses the above recommendations, that you post the full error messages and that you indicate (with comments) the lines in code that correspond to the line numbers in the error messages.
Some links that I have found helpful for me when I've asked coding questions here and elsewhere:
The Short, Self Contained, Correct (Compilable), Example
How to ask forum questions that folks will likely answer
Best of luck!!Last edited by Fubarable; 12-09-2009 at 06:08 PM.
- 12-09-2009, 09:34 PM #9
Member
- Join Date
- Nov 2009
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Help needed
By saibru in forum CLDC and MIDPReplies: 0Last Post: 03-23-2009, 09:40 AM -
a little help needed..
By litojs in forum New To JavaReplies: 1Last Post: 11-16-2008, 07:12 PM -
help needed!!! :S
By mark-mlt in forum NetworkingReplies: 1Last Post: 04-14-2008, 09:27 AM -
Help needed.
By necro-1000 in forum AWT / SwingReplies: 2Last Post: 01-12-2008, 11:37 AM -
help needed.
By dirtycash in forum New To JavaReplies: 3Last Post: 12-03-2007, 09:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks