Re: clock() problem - measuring cycles

From: Robert B. Clark (epynex_at_3pynexf.pbz)
Date: 01/12/05


Date: Wed, 12 Jan 2005 11:42:27 -0500

On Wed, 12 Jan 2005 00:52:03 -0800, "Ufit"
<kot_tmp0SPAMSPAM@NOpoczta.fm> wrote:

>I must precisely measure the time of calculation.
>f.ex
>
>clock_t begin,finish,time;
>begin=clock();
>Calculations();
>finish=clock();
>time=finish-begin;
>
>but unfortunatelly finish is equal to begin
>sth like begin=56789 and finish=56789
>they give me 0. How else can I measure the time very precisely?

The short answer is that you can't, at least not in a portable fashion.
This is a very platform-dependent thing to do.

See the c.l.c FAQ, question 19.37:

How can I implement a delay, or time a user's response, with sub-second
resolution?

http://www.eskimo.com/~scs/C-faq/q19.37.html

Of course, you could always loop your calculations by some arbitrarily
large number of iterations, and take the elapsed time for the entire
process divided by the number of loops::

        #define ITERATIONS 1000000UL

        clock_t t0, t1, elapsed;
        unsigned long i;

        t0 = clock();
        for (i = 0; i < ITERATIONS; i++)
                Calculations();
        t1 = clock();

        elapsed = t1 - t0;

        printf("CLOCKS_PER_SEC is %f\n", CLOCKS_PER_SEC);
        printf("Total elapsed time: %ld ticks (%f sec).\n",
                elapsed, elapsed / CLOCKS_PER_SEC);
        printf("Elapsed time per calc is %.2e seconds.\n",
                elapsed / CLOCKS_PER_SEC / ITERATIONS);

This may not be desirable in your application for any number of
reasons--the least of which is the lack of precision your platform may
afford you.

You might want to post your question in a newsgroup that is more focused
upon your particular environment, as the means to achieve your goal will
necessarily involve the use of non-standard extensions to the C
language.

-- 
Robert B. Clark (email ROT13'ed)
Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/


Relevant Pages

  • Re: Possible F77 Code Improvement ??
    ... The only other things that jump out are the summation loop, ... I'd also worry about the sort2d routine. ... double precision ValAbsX, ValAbsR, ValAbsT ... Kount = Kount + 1 ...
    (comp.lang.fortran)
  • Re: status of quadruple precision arithmetic in g95 and gfortran?
    ... we were talking about consistent precision. ... Unless you're unrolling the loop to do ... stack easily. ...
    (comp.lang.fortran)
  • Re: status of quadruple precision arithmetic in g95 and gfortran?
    ... (snip regarding precision of sums, extended precision, ... Unless you're unrolling the loop to do ... the SIMD parallel instructions, ... stack easily. ...
    (comp.lang.fortran)
  • Re: Trouble with integer floating point conversion
    ... if I can get the computation with 80 bit precision instead of 64 ... Because my system has dimension ~1e3 and the loop is threefold I cannot use ... jacob at jacob point remcomp point fr ...
    (comp.lang.c)
  • cubic root subroutine
    ... I needed a cubic root subroutine but didnīt find one at a quick search. ... #we need a precision check here ... # Now we divide fraction by ten and go up again ... # Divide fraction by ten for next loop ...
    (comp.lang.perl.misc)