delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/08/13:31:14

Message-ID: <3502E3F2.3039@quantum.de>
Date: Sun, 08 Mar 1998 19:31:14 +0100
From: Tom Chojnacki <chojnack AT quantum DOT de>
Reply-To: chojnack AT quantum DOT de
Organization: Quantum Software GmbH, Dortmund, Germany
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: odd clock() behaviour

Hi!

Recently I tried to measure the time some functions executes itself and
I used clock() function to do that.
I was surprized because sometimes clock() returned different values even
though my function performed exactly the same job.
I have written a short test:

#include <stdio.h>
#include <time.h>
#include <math.h>
int main()
{
clock_t start_time, end_time;
long i;
  start_time=clock();
  for (i = 0; i < 1000000; i++)
    {
    double root=sqrt(2.0);
    }
  end_time=clock();

  printf("total time=%f\n",
(double)(end_time-start_time)/CLOCKS_PER_SEC);
}

and while executing it I get mostly:
"total time=0.549310"

but sometimes I get:
"total time=0.494379"

and sometimes I get:
"total time=0.604241"

Can anyone explain why something like that happens.
Is it possible to cause that I get the same result every time I call
clock() function seqence.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019