compute C program time

J

John

Dear all,

Do you have any good way to compute the functions' cpu time/elapse
time in C program?

Thanks.
 
K

Keith Thompson

John said:
Do you have any good way to compute the functions' cpu time/elapse
time in C program?

The clock() function tells you the elapsed CPU time. The time()
function gives you the real time (but its resolution is 1 second on
many systems).

There are other time functions on many systems, but they're not
defined by the C standard. Consult your system's documentation.
 
J

John

clock() is the Bad Way, but might be your only shot. If you have access
to a profiler, however, that is the Good Way.

No profiler anyway, I just have std c functions.
then use clock function to compute the cpu time, and time function to
compute
the elapse time, right?

What is the profiler used to compute time in C program? do you know?

John Cui
 
B

bartc

Keith Thompson said:
The clock() function tells you the elapsed CPU time.

Didn't you say the opposite a while back:

Keith Thompson said:
No, it gives CPU time (not elapsed time), expressed as a value
of type clock_t. To get the time in seconds, the result must be
scaled by dividing by CLOCKS_PER_SEC.

?
 
R

Richard Tobin

Richard said:
If linux then gprof.

Last time I tried to use gprof I found it useless. It seems to sample
at a fixed rate, possibly 100Hz. This (or, as I remember it, 60Hz)
was useful 25 years ago, but with processors a thousand times faster
the granularity is far too coarse.

I would be delighted to learn that there's some way to fix this.

-- Richard
 
J

John

If linux then gprof.

I used cygwin with gcc on windows to code.

I used gprof, but I got stranger output.
the time of all functions are 0!
as below:

$ gprof.exe test_ORC.exe gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 78 0.00 0.00 ORCfilegetline
0.00 0.00 0.00 6 0.00 0.00 ORCstrncpy
0.00 0.00 0.00 6 0.00 0.00 ORCstrtrim
0.00 0.00 0.00 2 0.00 0.00 ORCfileopen
0.00 0.00 0.00 1 0.00 0.00 ORCcheckerror
0.00 0.00 0.00 1 0.00 0.00 ORCfilecreate
0.00 0.00 0.00 1 0.00 0.00 ORCfilegetinfo
0.00 0.00 0.00 1 0.00 0.00
ORCfilegetmaxline
0.00 0.00 0.00 1 0.00 0.00
ORCfilestatistics


Anyone can help me?
 
B

Ben Bacarisse

remod said:
John ha scritto:

This is system specific and is best asked in a group the deals with
the system you are using.
If you're working on Linux you can use valgrind as a profile. I don't
know if it's available for other Unix versions but certainly is not
for Windows.

If you're using gcc, the profiler is gprof. You have to compile your
code with a set of specific switches. Have a look here:
http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html

There are lots of options beyond these and I have some comments I'd
make about this above if it were topical but I don't want this thread
to degenerate into debuggers/profilers for Linux/Windows/embedded
systems etc. The OP should ask about program timing in the right
group.

<snip>
 
J

John

John ha scritto:




78 calls seems too few to collect significative measure. Try to collect
more sample (in the order of thousands or better millions call.

John ha scritto:




78 calls seems too few to collect significative measure. Try to collect
more sample (in the order of thousands or better millions call.

After I read a very big file, the time still is 0.

$ gprof.exe test_ORCfile.exe gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
no time accumulated

% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 2462420 0.00 0.00 ORCfilegetline
0.00 0.00 0.00 4 0.00 0.00 ORCstrlen
0.00 0.00 0.00 4 0.00 0.00 ORCstrncpy
0.00 0.00 0.00 1 0.00 0.00 ORCcheckerror
0.00 0.00 0.00 1 0.00 0.00 ORCfilecreate
0.00 0.00 0.00 1 0.00 0.00 ORCfilefree
0.00 0.00 0.00 1 0.00 0.00 ORCfilegetinfo
0.00 0.00 0.00 1 0.00 0.00
ORCfilegetmaxline
0.00 0.00 0.00 1 0.00 0.00 ORCfileopen
0.00 0.00 0.00 1 0.00 0.00
ORCfilestatistics

$ gprof.exe -v
GNU gprof (GNU Binutils) 2.18.50.20080625
Based on BSD gprof, copyright 1983 Regents of the University of
California.
This program is free software. This program has absolutely no
warranty.

$ cygcheck.exe -c cygwin
Cygwin Package Information
Package Version Status
cygwin 1.7.1-1 OK

what is the reason??


John Cui
 
J

John

John ha scritto:



Just to be sure, have you compiled and linked with the -pg option? 2M of
calls are definetely enough to get samples!!

Sure, I used -pg option. otherwise, I cannot get the gmon.out file.

Still have no solution....;(
 
B

Ben Bacarisse

John said:
Sure, I used -pg option. otherwise, I cannot get the gmon.out file.

Still have no solution....;(

Have you tried a more system-specific group? Maybe alt.comp.cygwin?
If not that, then a GNU group, perhaps.
 
P

Phil Carmody

Last time I tried to use gprof I found it useless. It seems to sample
at a fixed rate, possibly 100Hz. This (or, as I remember it, 60Hz)
was useful 25 years ago, but with processors a thousand times faster
the granularity is far too coarse.

If your programs were running for several minutes in the past, and
are running for several minutes now, then it's precisely the same
level of granularity.

Just because processors are 1000x faster, doesn't mean you should
start trying to optimise things that take up 0.08% of the run time;
you should still be trying to optimise things that take 80% of the
run time.
I would be delighted to learn that there's some way to fix this.

Run your program for longer. Bigger sample -> more information in
the data. Passive profiling is still all about statistics.

Phil
 
P

Phil Carmody

Ben Bacarisse said:
This is system specific and is best asked in a group the deals with
the system you are using.


There are lots of options beyond these and I have some comments I'd
make about this above if it were topical but I don't want this thread
to degenerate into debuggers/profilers for Linux/Windows/embedded
systems etc. The OP should ask about program timing in the right
group.

I don't consider sharing information about tools which C developers
may use to be off-topic.

For example, I don't use windows, I've not even seen a windows machine
for ages, and I rabidly detest anything to do with windows, but I think
that posts like "New version of MSVC now with C99 compatability available
for free download" are perfectly on-topic.

Behind the abstract language there's a huge hinterland of real-world
use of the language - without those tools the language may as well
not exist.

Phil
 
B

Ben Bacarisse

Phil Carmody said:
I don't consider sharing information about tools which C developers
may use to be off-topic.

Neither do it, as it happens. That's why I didn't make any complaint
about the suggestions to use valgrind or gprof or whatever, but the
discussion has reached a stage where the next step relative merits of
various timing strategies under whatever system the OP is using. That
sort of thing is, I contend, better kicked of with a fresh thread
where experts about that sort of thing are likely to be found.
For example, I don't use windows, I've not even seen a windows machine
for ages, and I rabidly detest anything to do with windows, but I think
that posts like "New version of MSVC now with C99 compatability available
for free download" are perfectly on-topic.

Absolutely. I can't think of anyone who'd object to that.

<snip>
 
R

Richard Tobin

Last time I tried to use gprof I found it useless. It seems to sample
at a fixed rate, possibly 100Hz. This (or, as I remember it, 60Hz)
was useful 25 years ago, but with processors a thousand times faster
the granularity is far too coarse.
[/QUOTE]
If your programs were running for several minutes in the past, and
are running for several minutes now, then it's precisely the same
level of granularity.

But fortunately this is not the case! The programs in question take
of the order of a few seconds to run, sometimes less.

It would be possible to modify the program to perform the same
computation many times, but that's a pain, especially if the you have
to, say, add code to free data that would not otherwise be freed. Why
isn't there just a knob to set the sample frequency?

-- Richard
 
R

Richard Tobin

But fortunately this is not the case! The programs in question take
of the order of a few seconds to run, sometimes less.
[/QUOTE]
In which case, why on earth are you worried about performance?

Because I run hundreds of such programs one after the other.

(They're actually XML analogues of sed, grep, and so on - programs
that may be short, but whose performance is important because they are
used repeatedly in more complicated scripts.)

-- Richard
 
K

Keith Thompson

Because I run hundreds of such programs one after the other.

(They're actually XML analogues of sed, grep, and so on - programs
that may be short, but whose performance is important because they are
used repeatedly in more complicated scripts.)

I wonder if you might improve performance by combining them into a
single executable, saving the overhead of program startup. For a
program that takes "a few seconds", it probably won't make much
difference, but for something that runs for a fraction of a second
it might be significant.
 
I

Ian Collins

If your programs were running for several minutes in the past, and
are running for several minutes now, then it's precisely the same
level of granularity.

But fortunately this is not the case! The programs in question take
of the order of a few seconds to run, sometimes less.

It would be possible to modify the program to perform the same
computation many times, but that's a pain, especially if the you have
to, say, add code to free data that would not otherwise be freed. Why
isn't there just a knob to set the sample frequency?[/QUOTE]

Use a better tool; something like the SunStudio profiler should give you
the level of detail you want.
 

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
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top