Results 1 to 10 of 10
  1. #1
    deadManN is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Stopwatch return wrong values

    Hello everybody,

    i dunno where i'm going wrong, but there's a problem with my code.

    as u can launch, and see after stop, getElapsedMilisecond function return wrong values :-s

    After calling stop() data shouldn't reset, and i need to keep track of total elapsed time, which is sum of all start() to stop() and the time passed after last start() call.

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javatesttimer;
    
    import TimeLib.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    /**
     *
     * @author Hassan
     */
    public class JavaTestStopwatch {
        public static void main(String[] args) {
            try {
                Stopwatch sw=new Stopwatch();
                System.out.println("Application Start");
                System.out.println(sw.getElapsedMilliseconds());
                sw.start();
                System.out.println("Stopwath Start");
                System.out.println(sw.getElapsedMilliseconds());
                Thread.sleep(2000);
                System.out.println("Application Sleep for Two Seconds");
                System.out.println(sw.getElapsedMilliseconds());
                sw.stop();
                System.out.println("Stopwatch Stop");
                System.out.println(sw.getElapsedMilliseconds());
                sw.start();
                System.out.println("Stopwatch Start");
                System.out.println(sw.getElapsedMilliseconds());
                sw.start();
                System.out.println("Stopwatch Start");
                System.out.println(sw.getElapsedMilliseconds());
                Thread.sleep(2000);
                System.out.println("Application Sleep for Two Seconds");
                System.out.println(sw.getElapsedMilliseconds());
                sw.stop();
                System.out.println("Stopwatch Stop");
                System.out.println(sw.getElapsedMilliseconds());
                sw.reset();
                System.out.println("Stopwatch Reset");
                System.out.println(sw.getElapsedMilliseconds());
                sw.start();
                System.out.println("Stopwatch Start");
                System.out.println(sw.getElapsedMilliseconds());
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaTestStopwatch.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package TimeLib;
    
    import sun.security.util.Debug;
    
    /**
     *
     * @author Hassan
     */
    
    public final class Stopwatch {
        
        public Stopwatch(){
            reset();
        }
        
        private long startTime = 0;
        private long totalStop = 0;
        private long stopTime = 0;
        private boolean running = false;
        private boolean launched = false;
        
        public void start() {
            if(running == false)
            {
                if (!this.launched) {
                    this.launched = true;
                    this.startTime = System.currentTimeMillis();
                }
                this.running = true;
            }
        }
    
        
        public void stop() {
            if (this.running) {
                this.totalStop += System.currentTimeMillis() - this.startTime;
                this.stopTime = System.currentTimeMillis();
            }
            this.running = false;
        }
        
        public void reset() {
           this.running = false;
           this.launched = false;
           this.startTime = 0;
           this.totalStop = 0;
           this.stopTime = 0;
        }
    
        
        //elaspsed time in milliseconds
        public long getElapsedMilliseconds() {
            Debug.println("Total Stop ", this.totalStop+"");
            Debug.println("Start Time ", this.startTime+"");
            Debug.println("Stop Time  ", this.stopTime+"");
            long elapsed;
            if (running) {
                 elapsed = (System.currentTimeMillis() - (startTime+ totalStop));
            }
            else {
                elapsed = (stopTime - (startTime + totalStop));
            }
            return elapsed;
        }
        
        
        //elaspsed time in seconds
        public long getElapsedSeconds() {
            long elapsed;
            if (running) {
                elapsed = ((System.currentTimeMillis()- (startTime+ totalStop)) / 1000);
            }
            else {
                elapsed = (stopTime - (startTime + totalStop) / 1000);
            }
            return elapsed;
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Stopwatch return wrong values

    getElapsedMilisecond function return wrong values
    Can you show the values that are returned and explain why they are wrong and show what the values should be?

  3. #3
    deadManN is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stopwatch return wrong values

    i already provide a test class,

    this class should return the time passed, while stop watch where enable ...

    start() start the stop watch, and stop() shut down it, but user can call start to continue the process, if any one press reset, all stored value should reset.

    for example, if u press start... in current time, it should return 0
    after 1 sec, it should return 1000 MS, if u stop it, it should still return 1000, if u start it again, the value should be same, and again after 1sec sleep in thread, the value should increase around 1000 ms +/- 1ms in a very simple application...
    if u reset time, the value should become 0...

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Stopwatch return wrong values

    I get the following when I run it.
    Please explain and show what values are wrong.
    Running: F:\Java\jre6\bin\java.exe -classpath D:\JavaDevelopment;.;..\. -Xmx512M JavaTestStopwatch

    Application Start
    Total Stop : 0
    Start Time : 0
    Stop Time : 0
    0
    Stopwath Start
    Total Stop : 0
    Start Time : 1324483222269
    Stop Time : 0
    0
    Application Sleep for Two Seconds
    Total Stop : 0
    Start Time : 1324483222269
    Stop Time : 0
    2000
    Stopwatch Stop
    Total Stop : 2000
    Start Time : 1324483222269
    Stop Time : 1324483224269
    0
    Stopwatch Start
    Total Stop : 2000
    Start Time : 1324483222269
    Stop Time : 1324483224269
    0
    Stopwatch Start
    Total Stop : 2000
    Start Time : 1324483222269
    Stop Time : 1324483224269
    0
    Application Sleep for Two Seconds
    Total Stop : 2000
    Start Time : 1324483222269
    Stop Time : 1324483224269
    2000
    Stopwatch Stop
    Total Stop : 6000
    Start Time : 1324483222269
    Stop Time : 1324483226269
    -2000
    Stopwatch Reset
    Total Stop : 0
    Start Time : 0
    Stop Time : 0
    0
    Stopwatch Start
    Total Stop : 0
    Start Time : 1324483226269
    Stop Time : 0
    0

    0 error(s)

  5. #5
    deadManN is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stopwatch return wrong values

    Start time : keep the time when you launch start() function
    Stop Time : keep the time since last time you call Stop() function
    total Stop : is equal to totalStop + stopTime - StartTime since last stop call

    as you can see
    Stopwatch Start
    0
    Application Sleep for Two Seconds
    2000
    Stopwatch Stop
    -2000

    the -2000 value is wrong in here, it should be 2000 or 2001

    cuz i stop application right after that two second sleep, so it can take 2000 ms or 2001 ms to launch whole application, in debug mode it may take more than then 10k of ms, i gotta go , will back soon

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Stopwatch return wrong values

    Sorry, I do not see where in the print out I posted you are taking the values from.
    Is this the section you are talking about?

    Application Sleep for Two Seconds
    Total Stop : 2000
    Start Time : 1324483222269
    Stop Time : 1324483224269
    2000
    Stopwatch Stop
    Total Stop : 6000
    Start Time : 1324483222269
    Stop Time : 1324483226269
    -2000
    Which line(s) in the above are wrong? What is the correct value?
    If you Copy the above and add your comments to the copy it would be easier to see which line you are talking about.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Stopwatch return wrong values

    One problem with your print outs is that you do not print an id on each line that is printed. For example if you:
    System.out.println("123 - " + sw.getElapsedMilliseconds());
    If you labeled all your print outs with unique ids (here 123) then you could say:
    the output at line with 123 should be ....

  8. #8
    deadManN is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stopwatch return wrong values

    yea, the section you said where i got wrong value..

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default Re: Stopwatch return wrong values

    Can you copy the output and put a label on the line with the wrong value?

    Try debugging the code by adding some printlns in the code to print out the values of all the variables used in computing the results. To make the print outs more efficient, only print out the values when the result value is wrong, like when it is less than 0.

  10. #10
    deadManN is offline Member
    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Default Re: Stopwatch return wrong values

    my ISP authentication server was down, so i start to draw it all over a paper, and the problem fixed :|
    public void start() {
    long sysElapsed = System.currentTimeMillis();
    if (!this.running){
    if (!this.launched) {
    this.launched = true;
    this.startTime = sysElapsed;
    }
    else if (this.launched) {
    this.totalStop += (sysElapsed - this.stopTime);
    }
    }
    this.running = true;
    }


    public void stop() {
    long sysElapsed = System.currentTimeMillis();
    if (this.running) {
    this.stopTime = sysElapsed;
    }
    this.running = false;
    }


    if (running) {
    elapsed = System.currentTimeMillis() - startTime - totalStop;
    }
    else {
    elapsed = stopTime - startTime - totalStop;
    }

Similar Threads

  1. Cannot return values from hashmap
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 06-17-2011, 04:16 PM
  2. Return positive values only
    By DomBrown in forum New To Java
    Replies: 14
    Last Post: 03-31-2011, 07:48 PM
  3. method return statement is wrong
    By ftrengnr in forum New To Java
    Replies: 1
    Last Post: 10-19-2010, 04:15 AM
  4. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 04:07 AM

Posting Permissions

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