Results 1 to 9 of 9

Thread: Help Needed!

  1. #1
    Lyricid is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Exclamation Help Needed!

    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 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?

    i can work out the average time as you can see.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Yep:
    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]);
    And read the code conventions (variables start with lower case letters) and declare a method. Your code repeats itself.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.";
      }
    }

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    .. oh,that other way is nice and simple too I guess :)

  5. #5
    Lyricid is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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?
    Attached Files Attached Files

  6. #6
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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

  7. #7
    Lyricid is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    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

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

    Default

    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.

  9. #9
    Lyricid is offline Member
    Join Date
    Nov 2009
    Posts
    28
    Rep Power
    0

    Default

    ok im sorry...but its just im having so much trouble displaying these two things, i thought if a programmer could look at it, they could see what the problem is, the piece of code i am refering to is in the insertion sort class, at the bottom of the class.. thanks for everyones help!

Similar Threads

  1. Help needed
    By saibru in forum CLDC and MIDP
    Replies: 0
    Last Post: 03-23-2009, 09:40 AM
  2. a little help needed..
    By litojs in forum New To Java
    Replies: 1
    Last Post: 11-16-2008, 07:12 PM
  3. help needed!!! :S
    By mark-mlt in forum Networking
    Replies: 1
    Last Post: 04-14-2008, 09:27 AM
  4. Help needed.
    By necro-1000 in forum AWT / Swing
    Replies: 2
    Last Post: 01-12-2008, 11:37 AM
  5. help needed.
    By dirtycash in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 09:17 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
  •