Function Timer in C

Y

yoviesmanda

I have a problem with C programming. I want to make function timing
in C, but I don't know how the function or the algorithm. so, I need
your help to giveme information about it. Thanks.. for your
attention... see.. you..
 
I

Ian Collins

I have a problem with C programming. I want to make function timing
in C, but I don't know how the function or the algorithm. so, I need
your help to giveme information about it. Thanks.. for your
attention... see.. you..
Try comp.lang.c. When you do, make the question a bit clearer, what do
you want to time?
 
L

Larry Smith

I have a problem with C programming. I want to make function timing
in C, but I don't know how the function or the algorithm. so, I need
your help to giveme information about it. Thanks.. for your
attention... see.. you..

Look up the clock() function (usually declared in <time.t>):

man clock
 
L

Larry Smith

Victor said:
and woman what?

V

Well gee, thanks for correcting the typing error.

'man' - FYI for Windows folks, that's the Unix manual viewer
command.

Ian posted from Solaris, and I mistakenly viewed his
email headers, instead of the OP's headers, when replying.

Thanks for the gracious corrections.
 
J

Jim Langston

I have a problem with C programming. I want to make function timing
in C, but I don't know how the function or the algorithm. so, I need
your help to giveme information about it. Thanks.. for your
attention... see.. you..

I generally just use clock().

unsigned int Start = clock();
MyFunction();
unsigned int End = clock();
std::cout "Elapapsed time: " << End - Start << " ns" << "\n";

Modify to taste. I think it's ms. On my system it's 1/1000 of a second.
 
J

James Kanze

I generally just use clock().
unsigned int Start = clock();
MyFunction();
unsigned int End = clock();
std::cout "Elapapsed time: " << End - Start << " ns" << "\n";
Modify to taste. I think it's ms.

It's CLOCKS_PER_SECOND. Posix requires CLOCKS_PER_SECOND to be
1000000, but it may vary on other systems. The best way to
write the above would be:

std::cout << "Elapapsed time: "
<< double( End - Start ) / CLOCKS_PER_SEC * 1000.0
<< " ms" << std::endl ;
On my system it's 1/1000 of a second.

In the old days, 50 or 60 used to be frequent values.
 
S

Siddhartha Gandhi

I generally just use clock().

unsigned int Start = clock();
MyFunction();
unsigned int End = clock();
std::cout "Elapapsed time: " << End - Start << " ns" << "\n";

Modify to taste. I think it's ms. On my system it's 1/1000 of a second.

On most systems it is a millisecond. I use std::clock() as well,
margin of error of close to 10 milliseconds, though. You could also
use gprof or a similar profiler.
 
J

James Kanze

On most systems it is a millisecond.

On all systems, it's CLOCKS_PER_SEC. Other than that, the only
"standard" I know is Posix, which requires a value of 1000000
for CLOCKS_PER_SEC, i.e. microseconds. (Linux is also Posix
conform in this respect.)
I use std::clock() as well,
margin of error of close to 10 milliseconds, though.

Historically, the granularity was the number of ticks. This
certainly isn't the case today, and I don't know of any system
which really gives microsecond granularity. (Historically, on
the first systems I used, CLOCKS_PER_SEC was 50 in Europe, and
60 in America. Which was the granularity.)
 

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,297
Messages
2,571,536
Members
48,284
Latest member
alphabetsalphabets

Latest Threads

Top