CFD Online Discussion Forums

CFD Online Discussion Forums (
-   Main CFD Forum (
-   -   How to measure CPU time in C (

Amingo June 15, 2000 17:15

How to measure CPU time in C
Hi, all:

Can someone tell me how to meause CPU time in C? I am working in Unix network. Also, I would like to know the timing method of C in Linux environment.



andy June 16, 2000 04:01

Re: How to measure CPU time in C
A few possibilities:

clock_t times(struct tms *buffer);

time_t time(time_t *tloc);

hrtime_t gethrtime(void);

hrtime_t gethrvtime(void);

They use different timing methods. See the man pages in section 2.

clifford bradford June 16, 2000 15:13

Re: How to measure CPU time in C
i always found that the best (and easiest) way to measure actual CPU time in unix is to use the time command. say you have a code called cfd.exe you simply enter "time cfd.exe" at the prompt and the at the conclusion of the code the OS will print a line showing the timing of the code. the man page on the time command will explain the results.

Amingo June 16, 2000 22:26

Re: How to measure CPU time in C

Yeah, this is a very nice feature of UNIX. But when I want to measure the CPU cost of each parts of my code for further improvement, I need a some function which I can write in code.

I am not sure if time(...) can be write in my code. I think it should be.


Steve Amphlett June 19, 2000 05:07

Re: How to measure CPU time in C
It sounds like you are trying to profile your code. If you are working in UNIX, there are some pretty good built-in profilers. My own personal favourite is pixie, available on SGI and DEC Alpha machines, but you'll find the basic prof command anywhere. Basically, you build an instrumented executable, run it normally and then use prof to post-process the profile data that it produces.

All times are GMT -4. The time now is 02:09.