Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Electronics ➜ Microprocessors ➜ Timer library (ProfileTimer) - times code on the Arduino

Timer library (ProfileTimer) - times code on the Arduino

Postings by administrators only.

Refresh page


Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Wed 11 May 2011 02:49 AM (UTC)

Amended on Thu 18 Aug 2011 10:03 PM (UTC) by Nick Gammon

Message
I have adapted the "Timer" class presented elsewhere in the forum, for use on an Arduino. This will time any "batch" of code, by simply using a single line. For example, to time "something" I might do this:


{
ProfileTimer t ("testing something");
something ();
}


The interesting thing here is that the entire work is done in the ProfileTimer class constructor and destructor. The constructor makes a note of the current time, and echoes the "start" message.

The destructor makes a note of the (new) current time, subtracts the finish from the start, and displays the interval.

You can nest calls (see example program) so you can time smaller bits of code while still Timer the larger overall picture.

The purpose of the braces is to "scope" the ProfileTimer class so that when it hits the closing brace it goes out of scope and the destructor is called.

If you simply want to time an entire function, this is not necessary as the destructor is called when the function exits.

eg.


void myfunction (void)
  {
  ProfileTimer t ("myfunction");

  //  do something lengthy here

  }  // end of myfunction - time elapsed will be displayed


Or to save typing in the function name you can do this:


void myfunction ()
  {
  ProfileTimer t (__PRETTY_FUNCTION__);

  //  do something lengthy here
  
  }


The __PRETTY_FUNCTION__ argument returns the current function name, and its arguments.

In fact you can make a define to do that:


#define TIME_FUNCTION  ProfileTimer t (__PRETTY_FUNCTION__)


So now you can just to this:


void myfunction ()
  {
  TIME_FUNCTION;

  //  do something lengthy here
  
  }



The Arduino library can be downloaded from here:

http://www.gammon.com.au/Arduino/ProfileTimer.zip (4 Kb)

Unzip the file into your Arduino sketch "libraries" folder.

To use it just:


#include <ProfileTimer.h>


Example code that times analog reads:


#include <ProfileTimer.h>

void setup ()
{
  Serial.begin (115200);
}  // end setup

void readSensors ()
  {
 ProfileTimer t ("readSensors");

  int a = analogRead (A0);
  int b = analogRead (A1);
  }
  
void loop ()
{
  delay (500);

  ProfileTimer t ("analog read");
  analogRead (A1);

  {
    ProfileTimer t1 ("multiple reads");
    for (int i = A0; i <= A5; i++)
      analogRead (i);
  }  // end timed bit of code

}  // end loop


[EDIT] Renamed from Timer to ProfileTimer.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Greete   (16 posts)  Bio
Date Reply #1 on Thu 18 Aug 2011 07:33 PM (UTC)
Message
hi,
the link for the library is not working, did you have another one?
Top

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #2 on Thu 18 Aug 2011 09:08 PM (UTC)
Message
It looks like it was renamed to ProfileTimer. Try http://www.gammon.com.au/Arduino/ProfileTimer.zip

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 18 Aug 2011 10:04 PM (UTC)
Message
Oops. Fixed original post.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Greete   (16 posts)  Bio
Date Reply #4 on Fri 19 Aug 2011 10:14 PM (UTC)
Message
thanks!!!!
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


40,573 views.

Postings by administrators only.

Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.