Portable method of accessing CPU time for given process

This is a discussion on Portable method of accessing CPU time for given process within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I'm using the following code to print the duration of a program. I'm using clock(), or if available clock_gettime() ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    21

    Portable method of accessing CPU time for given process

    Hi, I'm using the following code to print the duration of a program. I'm using clock(), or if available clock_gettime() with CLOCK_PROCESS_CPUTIME_ID.

    Code:
    #ifdef _POSIX_CPUTIME
    #ifdef CLOCK_PROCESS_CPUTIME_ID
    #define TELLTIME if( clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &duration1) == 0 ) {\
                                fprintf(stdout, "Time Elapsed: %lf seconds \n",(double)duration1.tv_nsec/NANOSECS);\
                            } else {\
                                perror("Error: clock_gettime() function error: ");\
                            }
    #else
    #define TELLTIME fprintf(stdout, "Time Elapsed: %.2f seconds.\n", (double)((double)clock()/CLOCKS_PER_SEC));
    #endif
    #else
    #define TELLTIME ;
    #endif
    ...
    ...
    TELLTIME;
    I'm just wondering if there's a way of getting the precision of clock_gettime with CLOCK_PROCESS_CPUTIME_ID when the system doesn't define CLOCK_PROCESS_CPUTIME_ID

    If anyone has any insight it would be appreciated.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do "man clock_gettime", you will see a function called clock_getres, which gives you the "resolution" of the time you get.

    It is very unlikely that the function is available if CLOCK_PROCESS_CPUTIME_ID is not defined - as it is part of the documented interface of that function.

    Note however that clock() is DEFINED to give CPU-time, it doesn't always do that. For example, in Windows, clock() returns the time elapsed, not the CPU-time used by this process.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Hi matsp, thanks for your comments. I've read through and used clock_getres(), it wasn't defined as was thought. Thanks for the tipoff about clock_getres()! Quite useful.

    The application at this point in time is for Linux and other unix-like OSs so I've decided to rely on clock() should CLOCK_PROCESS_CPUTIME_ID not be defined.

    Thanks for the comments.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    the availability of CLOCK_PROCESS_CPUTIME_ID can be check at compile-time with _POSIX_CPUTIME and _SC_CPUTIME at runtime with sysconf() [man posixoptions; man sysconf]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CPU Time
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 08-14-2002, 01:17 PM
  2. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  3. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 06:17 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM
  5. One process at a time
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-06-2001, 12:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21