[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Electronics
. -> [Folder]  Microprocessors
. . -> [Subject]  Timer library (ProfileTimer) - times code on the Arduino
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Timer library (ProfileTimer) - times code on the Arduino

Postings by administrators only.

[Refresh] Refresh page


Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] 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
[Go to top] top

Posted by Greete   (16 posts)  [Biography] 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?
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] 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
[Go to top] top

Posted by Greete   (16 posts)  [Biography] bio
Date Reply #4 on Fri 19 Aug 2011 10:14 PM (UTC)
Message
thanks!!!!
[Go to top] 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.


13,576 views.

Postings by administrators only.

[Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]