LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   measuring time (https://www.linuxquestions.org/questions/programming-9/measuring-time-18994/)

Hano 04-20-2002 02:14 PM

measuring time
 
Hi,



How do you measure time between events in linux?
are there any nice and short functions you can write?


Hano

zmedico 04-20-2002 06:25 PM

Most languages have time functions built in. For instance, Java has a System.currentTimeMillis() method that gives you the time in milliseconds.

My RedHat 7.2 installation came with kdevelop-2.0-1
That includes these nice references for C and C++ functions

/usr/share/doc/HTML/en/kdevelop/reference/C/cref.html
/usr/share/doc/HTML/en/kdevelop/reference/CPLUSPLUS/cref.html

Hano 04-21-2002 12:48 AM

hey, i end up answering myself again...

i did this C++ class as a chronometer, so enjoy everyone:

#include <sys/timeb.h>

class chronometer {
private:
struct timeb start_time;
struct timeb end_time;
int state;
int aux;
int sec;
int msec;
float total_seconds;

public:
chronometer(){state=0; };
~chronometer() {};

void start() {
reset();
state=1;
aux=ftime(&start_time);
};

void stop() {
aux=ftime(&end_time);
if (state==1) {
state=0;
total_seconds+=elapsed_time();
};
};

void restart() {
stop();
state=1;
aux=ftime(&start_time);
};

void reset() {
total_seconds=0.0;
};

float read_time() {
if (state==1) {
stop();
state=1;
aux=ftime(&start_time);
};
return total_seconds;
};

float elapsed_time() {
float seconds;
sec=end_time.time-start_time.time;
msec=end_time.millitm-start_time.millitm;
if (msec<0) {
sec--;
msec+=1000;
};
seconds=((float)msec )/1000;
seconds+=sec;
return seconds;
};

};


using it is easy and straightforward, just:

chronometer myclock;
myclock.start(); //start chronometer
//do whatever you want to clock
cout<<" have been up "<<myclock.read_time()<<"seconds "<<end;
myclock.stop(); //pause chronometer
//bla bla bla
myclock.restart(); //restart

...and so on

Hano


All times are GMT -5. The time now is 06:13 AM.