LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-26-2006, 08:52 AM   #1
osle
LQ Newbie
 
Registered: Oct 2006
Posts: 8

Rep: Reputation: 0
clock() function problem


Hi,

I have a strange problem. Here is the code:

#include <time.h>
clock_t clockNow;
clockNow = clock();

clock() always returns zero. why ??????

I'm using RedHat 2.6.5-7, but it has happened on machines with different distributions also. This machine is a server.

This, on the other hand, works fine:
#include <time.h>
time_t timeNow;
time( &timeNow );

but I need something with higher accuracy than seconds.

Any comments are welcome...
Thank you!
 
Old 11-26-2006, 12:21 PM   #2
soggycornflake
Member
 
Registered: May 2006
Location: England
Distribution: Slackware 10.2, Slamd64
Posts: 249

Rep: Reputation: 31
What are you trying to do? clock and time are completely different functions. clock returns the cpu time used by the program and time returns the number of seconds since epoch.

If you want the current time to a higher resolution than seconds (which seems to be what you're looking for), then use gettimeofday(2), which provides microsecond resolution.
 
Old 11-26-2006, 12:36 PM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
clock tells you roughly how much CPU time has been used by the process which called it. Since you're calling it near or at the start of a program, it's going to return 0. What else do you expect?
 
Old 11-26-2006, 01:18 PM   #4
osle
LQ Newbie
 
Registered: Oct 2006
Posts: 8

Original Poster
Rep: Reputation: 0
you are both right

I guess that's why I used the newbie forum...

clock() is not the appropriate call. And yes, I was looking for a higher resolution time function.
gettimeofday() should do it.

Thanks!
 
  


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
System clock correct, hardware clock wrong karlovac Linux - General 5 01-16-2009 08:19 PM
Failed to set system clock to hardware clock jrtayloriv Linux - Newbie 2 09-25-2008 08:06 AM
Clock problem masterross Red Hat 3 10-28-2006 07:49 PM
Linux OS Clock/Timezone shows the clock off by 6 hours between OS's JBailey742 Linux - Software 9 04-07-2006 12:40 AM
clock problem minor Linux - General 12 10-31-2003 05:37 AM


All times are GMT -5. The time now is 01:54 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration