Why does changing one line slow down my timer?

edited November 2016 in Questions about Code

Hello guys. I'm starting out with processing and I'm trying to make a stop watch with a pause function. I have gotten a working timer and the code is below: However, replacing line 16 with the code on line 17 (i.e uncommenting line 17 and commenting out line 16) produces a massive slow down in the timer.. milliseconds pass like seconds. I cannot understand why this happens.. any help would be greatly appreciated. (I want line 17 to work, line 16 is just a debugging line. )

The logic behind using the variable totalTime was so that I could pause the timer by stopping the incrementation whenever the user triggered the pause action. When the timer is not paused (the pausing code hasn't been added yet), totalTime is incremented by 2 when 2 milliseconds pass.

       int timeBefore=0;
        int totalTime = 0;
        void setup(){
          size(512,128);
        }

        void draw(){
          background(125);
          int timeNow = millis();
          if ((timeNow - timeBefore)> 2){
            timeBefore = timeNow;
            totalTime += 2;


          }
          drawTime(width/2,height/2,millis());
          //drawTime(width/2,height/2,totalTime);

        }


        void drawTime(int x, int y, int milliSeconds) {

          //String to store the time value
          //String timeString = "";
          //String hours="";
          String mins="";
          String sec="";
          String mills="";


          //**Do some error checking here
          if (milliSeconds<0) {
            println("Something went wrong when displaying the time");
            return; //
          } else {
            mills= str(((milliSeconds)%1000)/10); //divide by 100 so that we display only two digits
            mills = formatTime(mills,2);
            sec = str((milliSeconds/1000) % 60);
            sec = formatTime(sec,2);
            mins = str(milliSeconds/(60*1000)); //not modding by 20 because we can count minutes indefinitely
            mins = formatTime(mins,2);
            text (mins+":"+sec+":"+mills,x,y);
          }
        }

        //function to format strings to always have a fixed number of digits
        String formatTime(String input, int digits){

          while (input.length() < digits){
            input = "0"+input;
          }

          return input;


        }'

Answers

  • @mahela007 -- Try using println and looking at the console tab to see what is happening.

    For example, you could add a statement at the very top of your drawTIme function displaying the argument:

    println(milliSeconds);
    
  • Answer ✓

    The default frame rate is ~60 times a second so on average the time between frames (timeNow - timeBefore) is 16-17 milliseconds but you are adding only 2ms per frame. In other words the time is running 1/8 of real time.

    Unless you can get frame rates in excess of 500 the elapsed time between frames will always be greater than 2ms so the draw method would be

    void draw() {
      background(125);
      int timeNow = millis();
      totalTime += (timeBefore = timeNow);
    
      drawTime(width/2, height/2, millis());
      //drawTime(width/2,height/2,totalTime);
    }
    

    This blog post will give you better insight to measuring time in Processing.

  • Thank you quark. That was very helpful. I will read the blog post you linked to as well. Is there any way to determine the frame rate at runtime?

  • @jeremydouglass.. i tried that. I still couldn't figure out what was wrong.

  • You can get the current frame rate with the variable frameRate

Sign In or Register to comment.