Usar um temporizador em C
- 
          
            Utilizar a função gettimeofdaycomo marco de referência do temporizador
- 
          
            Utilizar a função clock_gettimecomo marco de referência do temporizador em C
 
Este artigo irá introduzir múltiplos métodos sobre como utilizar um temporizador em C.
Utilizar a função gettimeofday como marco de referência do temporizador
    
gettimeofday é uma função compatível com o POSIX para recuperar o tempo do sistema. São necessários dois argumentos, um do tipo struct timeval e um do tipo struct timezone, o último dos quais é agora obsoleto. Assim, precisaríamos de declarar apenas estruturas timeval para armazenar os valores de tempo recuperados. O struct timeval consiste em dois membros que representam segundos e microssegundos, respectivamente.
No exemplo seguinte, implementamos duas funções para encontrar o valor máximo no array de inteiros. Uma delas baseia-se na comparação de valores, e a outra utiliza os índices para encontrar o número inteiro com o maior valor. Utilizamos gettimeofday antes e depois da função max_ ser chamada para comparar a velocidade destas funções.
Note que, existe uma função time_diff que calcula o tempo decorrido em segundos. Neste caso, só executamos o teste uma vez num array de inteiros gerados aleatoriamente, mas geralmente, devem ser utilizados mais métodos estatísticos para medir o desempenho em sistemas modernos.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
int max_index(int arr[], int size) {
  size_t max = 0;
  for (int j = 0; j < size; ++j) {
    if (arr[j] > arr[max]) {
      max = j;
    }
  }
  return arr[max];
}
int max_value(int arr[], int size) {
  int max = arr[0];
  for (int j = 0; j < size; ++j) {
    if (arr[j] > max) {
      max = arr[j];
    }
  }
  return max;
}
float time_diff(struct timeval *start, struct timeval *end) {
  return (end->tv_sec - start->tv_sec) + 1e-6 * (end->tv_usec - start->tv_usec);
}
enum { WIDTH = 100000 };
int main() {
  struct timeval start;
  struct timeval end;
  int max;
  int *arr = malloc(WIDTH * sizeof(int));
  srand(time(NULL));
  for (size_t i = 0; i < WIDTH; i++) {
    arr[i] = rand();
  }
  gettimeofday(&start, NULL);
  max = max_index(arr, WIDTH);
  gettimeofday(&end, NULL);
  printf("max_index: %0.8f sec, max = %d\n", time_diff(&start, &end), max);
  gettimeofday(&start, NULL);
  max = max_value(arr, WIDTH);
  gettimeofday(&end, NULL);
  printf("max_value: %0.8f sec, max = %d\n", time_diff(&start, &end), max);
  free(arr);
  exit(EXIT_SUCCESS);
}
Resultado:
max_index: 0.00028346 sec, max = 2147391322
max_value: 0.00022213 sec, max = 2147391322
Utilizar a função clock_gettime como marco de referência do temporizador em C
Alternativamente, podemos utilizar o clock_gettime para atingir objectivos de medição semelhantes. O clock_gettime é um método mais recente e recomendado utilizado em bases de código mais recentes. Armazena o valor de tempo no objecto struct timespec e leva o ponteiro até ele como segundo parâmetro. Entretanto, o primeiro argumento especifica o tipo de relógio a utilizar. Neste exemplo, recuperamos o CLOCK_REALTIME porque mede o chamado tempo do relógio de parede. É representado como segundos e nanossegundos passados desde a época, a data de início da medição da hora.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
int max_index(int arr[], int size) {
  size_t max = 0;
  for (int j = 0; j < size; ++j) {
    if (arr[j] > arr[max]) {
      max = j;
    }
  }
  return arr[max];
}
int max_value(int arr[], int size) {
  int max = arr[0];
  for (int j = 0; j < size; ++j) {
    if (arr[j] > max) {
      max = arr[j];
    }
  }
  return max;
}
float time_diff2(struct timespec *start, struct timespec *end) {
  return (end->tv_sec - start->tv_sec) + 1e-9 * (end->tv_nsec - start->tv_nsec);
}
enum { WIDTH = 100000 };
int main() {
  struct timespec start2, end2;
  int max;
  int *arr = malloc(WIDTH * sizeof(int));
  srand(time(NULL));
  for (size_t i = 0; i < WIDTH; i++) {
    arr[i] = rand();
  }
  clock_gettime(CLOCK_REALTIME, &start2);
  max = max_index(arr, WIDTH);
  clock_gettime(CLOCK_REALTIME, &end2);
  printf("max_index: %0.8f sec, max = %d\n", time_diff2(&start2, &end2), max);
  clock_gettime(CLOCK_REALTIME, &start2);
  max = max_value(arr, WIDTH);
  clock_gettime(CLOCK_REALTIME, &end2);
  printf("max_value: %0.8f sec, max = %d\n", time_diff2(&start2, &end2), max);
  free(arr);
  exit(EXIT_SUCCESS);
}
Resultado:
max_index: 0.00028346 sec, max = 2147391322
max_value: 0.00022213 sec, max = 2147391322
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook