How to count CPU time by using C function

P

PCHOME

Hi!

I am new to C.
If I want to know the CPU time needed to run a sequence of my C
code(not necessary the whole program, maybe just part of my program),

What function should I use? Some example?

Thanks!
 
K

Keith Thompson

PCHOME said:
I am new to C.
If I want to know the CPU time needed to run a sequence of my C
code(not necessary the whole program, maybe just part of my program),

What function should I use? Some example?

Standard C provides the clock() function for this purpose. Your
system, or any C textbook, should include documentation on it.

Save the value returned by clock() before and after the code executes,
subtract the former from the latter, and divide by CLOCKS_PER_SEC to
get seconds.

There may be system-specific functions that provide similar
functionality, perhaps in more detail. (You might find the times()
function useful if your system provides it, but if you have any
questions about it, you'll need to ask in a system-specific
newsgroup.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top