compute C program time

Discussion in 'C Programming' started by John, Jan 6, 2010.

  1. John

    John Guest

    Dear all,

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

    Thanks.
     
    John, Jan 6, 2010
    #1
    1. Advertising

  2. John <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 6, 2010
    #2
    1. Advertising

  3. John

    John Guest

    On Jan 6, 4:53 pm, Richard Heathfield <> wrote:
    > John wrote:
    > > Dear all,

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

    >
    > 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
     
    John, Jan 6, 2010
    #3
  4. John

    bartc Guest

    "Keith Thompson" <> wrote in message
    news:...
    > John <> writes:
    >> 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.


    Didn't you say the opposite a while back:

    > "bartc" writes:
    >> C's clock() function gives elapsed times in msec.


    "Keith Thompson" <> wrote in message
    news:...
    > 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.


    ?

    --
    Bartc
     
    bartc, Jan 6, 2010
    #4
  5. In article <hi1sjl$v01$-september.org>,
    Richard <> wrote:

    >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
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, Jan 6, 2010
    #5
  6. John

    John Guest

    On Jan 6, 7:36 pm, Richard <> wrote:
    > remod <> writes:
    > > John ha scritto:

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

    >
    > > 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

    >
    > > If you're using another compiler, have a look at its manual. Talking
    > > about freely available C compilers, I'm sure that lcc-win32 and
    > > OpenWatcom both come with a profiler.

    >
    > > I suspect VisualStudio C++ Express doesn't but I have'nt checked.

    >
    > > Remo.D.

    >
    > 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?
     
    John, Jan 6, 2010
    #6
  7. remod <> writes:

    > John ha scritto:
    >
    >> What is the profiler used to compute time in C program? do you
    >> know?


    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>
    --
    Ben.
     
    Ben Bacarisse, Jan 6, 2010
    #7
  8. Richard <> writes:

    > remod <> writes:
    >> John ha scritto:
    >>
    >>> What is the profiler used to compute time in C program? do you know?

    <snip>
    > If linux then gprof.


    If they asked in the right group they'd get better (or at least more
    expansive) answers than that.

    --
    Ben.
     
    Ben Bacarisse, Jan 6, 2010
    #8
  9. John

    John Guest

    On Jan 6, 10:14 pm, remod <> wrote:
    > John ha scritto:
    >
    > > On Jan 6, 7:36 pm, Richard <> wrote:

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

    >
    > >   %   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

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


    On Jan 6, 10:14 pm, remod <> wrote:
    > John ha scritto:
    >
    > > On Jan 6, 7:36 pm, Richard <> wrote:

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

    >
    > > % 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

    >
    > 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
     
    John, Jan 6, 2010
    #9
  10. "bartc" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> John <> writes:
    >>> 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.

    >
    > Didn't you say the opposite a while back:


    No.

    >> "bartc" writes:
    >>> C's clock() function gives elapsed times in msec.

    >
    > "Keith Thompson" <> wrote in message
    > news:...
    >> 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.

    >
    > ?


    By "elapsed time", I meant wall clock time. By "elapsed CPU time", I
    meant CPU time.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 6, 2010
    #10
  11. John

    John Guest

    On Jan 7, 12:30 am, remod <> wrote:
    > John ha scritto:
    >
    > > On Jan 6, 10:14 pm, remod <> wrote:
    > >> John ha scritto:
    > >>> On Jan 6, 7:36 pm, Richard <> wrote:
    > >> 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.

    >
    > >   %   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

    >
    > 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....;(
     
    John, Jan 7, 2010
    #11
  12. John <> writes:

    > On Jan 7, 12:30 am, remod <> wrote:
    >> John ha scritto:
    >>
    >> > On Jan 6, 10:14 pm, remod <> wrote:
    >> >> John ha scritto:
    >> >>> On Jan 6, 7:36 pm, Richard <> wrote:
    >> >> 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.

    >>
    >> >   %   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

    >>
    >> 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....;(


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

    --
    Ben.
     
    Ben Bacarisse, Jan 7, 2010
    #12
  13. John

    Phil Carmody Guest

    "bartc" <> writes:
    > "Keith Thompson" <> wrote in message
    > news:...
    >> John <> writes:
    >>> 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.


    So CPU time not wall-clock time.

    > Didn't you say the opposite a while back:
    >
    >> "bartc" writes:
    >>> C's clock() function gives elapsed times in msec.

    >
    > "Keith Thompson" <> wrote in message
    > news:...
    >> No, it gives CPU time


    So CPU time not the wall-clock 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.

    >
    > ?


    Look the same to me.

    All time of any kind elapses, so that's not an important word
    to focus on.

    Phil
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
     
    Phil Carmody, Jan 7, 2010
    #13
  14. John

    Phil Carmody Guest

    (Richard Tobin) writes:
    > In article <hi1sjl$v01$-september.org>,
    > Richard <> wrote:
    >
    >>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.


    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
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
     
    Phil Carmody, Jan 7, 2010
    #14
  15. John

    Phil Carmody Guest

    Ben Bacarisse <> writes:
    > remod <> writes:
    >
    >> John ha scritto:
    >>
    >>> What is the profiler used to compute time in C program? do you
    >>> know?

    >
    > 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.


    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
    --
    Any true emperor never needs to wear clothes. -- Devany on r.a.s.f1
     
    Phil Carmody, Jan 7, 2010
    #15
  16. Phil Carmody <> writes:

    > Ben Bacarisse <> writes:
    >> remod <> writes:
    >>
    >>> John ha scritto:
    >>>
    >>>> What is the profiler used to compute time in C program? do you
    >>>> know?

    >>
    >> 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.

    >
    > 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>
    --
    Ben.
     
    Ben Bacarisse, Jan 7, 2010
    #16
  17. In article <>,
    Phil Carmody <> wrote:

    >> 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.


    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
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, Jan 7, 2010
    #17
  18. In article <>,
    Richard Heathfield <> wrote:

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


    >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
    --
    Please remember to mention me / in tapes you leave behind.
     
    Richard Tobin, Jan 7, 2010
    #18
  19. (Richard Tobin) writes:
    > In article <>,
    > Richard Heathfield <> wrote:
    >>> But fortunately this is not the case! The programs in question take
    >>> of the order of a few seconds to run, sometimes less.

    >
    >>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.)


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 7, 2010
    #19
  20. John

    Ian Collins Guest

    Richard Tobin wrote:
    > In article <>,
    > Phil Carmody <> wrote:
    >
    >>> 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.

    >
    > 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?


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

    --
    Ian Collins
     
    Ian Collins, Jan 7, 2010
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike Wahler
    Replies:
    0
    Views:
    462
    Mike Wahler
    Jul 24, 2003
  2. Huey

    How to compute time into integer?

    Huey, Dec 16, 2003, in forum: C Programming
    Replies:
    13
    Views:
    643
    Keith Thompson
    Dec 25, 2003
  3. Robert P. J. Day
    Replies:
    5
    Views:
    2,684
    mrdeath5493
    Jan 17, 2011
  4. ed
    Replies:
    0
    Views:
    113
  5. PerlFAQ Server
    Replies:
    0
    Views:
    287
    PerlFAQ Server
    Feb 2, 2011
Loading...

Share This Page