LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get elapsed time (https://www.linuxquestions.org/questions/programming-9/get-elapsed-time-661182/)

HarryBoy 08-07-2008 10:31 AM

Get elapsed time
 
I need to check if a certain time has elapsed (about 10 seconds)and I am using the following code:
Code:

long getTickCount()
{
    tms tm;
    return times(&tm);
}

long timestart = getTickCount();

//and then when needed I do:

long newtime = getTickCount();

if( newtime - timestart > value )
//do something

this works but is it a suitable way of doing this. (I am new to linux programming)

chakka.lokesh 08-08-2008 05:09 AM

Quote:

Originally Posted by HarryBoy (Post 3239966)
this works ....

did u executed this code or not?

rogx 08-08-2008 06:13 AM

Re: Get elapsed time
 
Code:

/*
gcc -Wall -o tryelapsed tryelapsed.c
./tryelapsed
*/

#include <stdio.h>
#include <sys/time.h>

struct timeval tv;

static double curtime(void) {
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec / 1000000.0;
}

int main()
  {
  double timestart;
  double newtime;
  double value=10.0;
  double result;
  long unsigned cnt;

  timestart=curtime();

lbl_reloop:

  for(cnt=0;cnt<100000000;cnt++)
      result=100.0/3.0;

  newtime=curtime();

  printf("elapsed: %f\n", newtime - timestart);

  if( newtime - timestart < value )
    goto lbl_reloop;

  return(0);
  }

Can this help?

Bye, Rogx


All times are GMT -5. The time now is 05:28 AM.