Re: A very slow program

Kaz Kylheku <[email protected]> Mon, 16 Sep 2024 02:44:10 -0000 (UTC)
Newsgroups alt.comp.lang.c,comp.lang.c
Organization A noiseless patient Spider
Message-ID <[email protected]>
On 2024-09-15, Student Project <[email protected]> wrote:
> /*
> The result on my slow machine:
> Program takes an average of 0.018000 seconds to find 78498 primes.
> */

Use the clock() function, to get clock_t time values. They usually
have better granularity than time_t. The resolution of clock_t is
given by the CLOCKS_PER_SEC constant. There is no difftime equivalent;
you just subtract the earlier clock_t from the later one, and divide by
CLOCKS_PER_SEC to get seconds. This is usually done in floating-point:

  clock_t start = clock();
  clock_t end = clock();
  double interval = (end - start) / (double) CLOCKS_PER_SEC;

(This assumes that CLOCKS_PER_SEC is in range of double; if that
were not the case, we get undefined behavior. I'm only pointing this
pointless concern because if I don't, someone else will.)

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @[email protected]