Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

Rep: Reputation: 0
clock() function problem


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, 11:21 AM   #2
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, 11:36 AM   #3
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 64
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, 12:18 PM   #4
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.



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

All times are GMT -5. The time now is 02:26 AM.

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