C/Development/Timer — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:22, 25 мая 2010

Display a software timer

#include <stdio.h>
#define DELAY 128000
struct my_time {
  int hours;
  int minutes;
  int seconds;
} ;

int main(void)
{
  struct my_time systime;

  if(systime.seconds==60) {
    systime.seconds = 0;
    systime.minutes++;
  }
  if(systime.minutes==60) {
    systime.minutes = 0;
    systime.hours++;
  }
  if(systime.hours==24)
     systime.hours = 0;
  printf("\n%02d:", systime.hours);
  printf("\n%02d:", systime.minutes);
  printf("\n%02d\n", systime.seconds);

  return 0;
}


Test our timer function

/*
Beginning C, Third Edition
 By Ivor Horton
 ISBN: 1-59059-253-0
 Published: Apr 2004
 Publisher: apress
*/
#include <stdio.h>
#include <time.h>
void main()
{
  long count = 100000000;          /* Number of loop iterations */
  long i = 0;                      /* Loop counter              */
  time_t calendar = 0;             /* Holds calendar time       */
  clock_t now = 0;                 /* Holds initial clock time  */
  int interval = 3;                /* Seconds interval for o/p  */
  int elapsed = 0;                 /* Elapsed clock time secs.  */
  calendar = time(NULL);           /* Get current calendar time */
  now = clock();                   /* Get current clock time    */
  printf("Initial clock time = %d Initial calendar time = %d\n",
                                                       now, calendar);
    
    for(i = 0L ; i<count ; i++)
    {
      elapsed = (clock()-now)/CLOCKS_PER_SEC;
      if(elapsed>=interval)
      {
        interval += 3;
        printf("\nElapsed = %ld seconds  iterations = %ld", elapsed, i);
      }
    }
  printf("\nCPU time for %ld interations is %.2lf seconds\n",
                                count, (double)(clock()-now)/CLOCKS_PER_SEC );
  printf("\Final clock time = %d Final calendar time = %d\n",
                                                       clock(), time(NULL));
  printf("\nElapsed calendar time to execute the program is %lf\n",
                                           difftime(time(NULL), calendar));
}