LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-07-2008, 10:31 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
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)
 
Old 08-08-2008, 05:09 AM   #2
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 211

Rep: Reputation: 32
Quote:
Originally Posted by HarryBoy View Post
this works ....
did u executed this code or not?
 
Old 08-08-2008, 06:13 AM   #3
rogx
LQ Newbie
 
Registered: Jan 2005
Location: Roma, Italy
Distribution: slackware, slamd64
Posts: 23

Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
diff. between elapsed time and cpu time madhugp Linux - General 1 01-15-2007 07:57 AM
find elapsed time innuendo_98 Programming 2 11-10-2005 08:03 AM
amarok progressbar time elapsed problem abattoir Linux - Software 0 04-13-2005 10:31 AM
elapsed time formula for open office calc philosophe Linux - Software 1 03-30-2005 05:32 AM
getting elapsed time.. Mad_C Programming 2 04-01-2003 01:12 AM


All times are GMT -5. The time now is 07:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration