Proyecto para encontrar 2 puntos más cercano para el curso de Análisis y Diseño de Algoritmos.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer.c 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2018 Christopher Cromer
  3. * Copyright 2018 Rodolfo Cuevas
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  8. *
  9. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  10. *
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. */
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <math.h>
  18. /**
  19. * Cuando empezó el timer
  20. */
  21. static struct timespec start_time;
  22. /**
  23. * Cuando terminó el timer
  24. */
  25. static struct timespec stop_time;
  26. /**
  27. * Empezar el timer
  28. */
  29. void start_timer() {
  30. clock_gettime(CLOCK_MONOTONIC, &start_time);
  31. stop_time.tv_nsec = 0;
  32. }
  33. /**
  34. * Terminar el timer
  35. */
  36. void stop_timer() {
  37. if (start_time.tv_nsec == 0) {
  38. fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
  39. }
  40. else {
  41. clock_gettime(CLOCK_MONOTONIC, &stop_time);
  42. }
  43. }
  44. /**
  45. * Imprimir el tiempo de ejecución
  46. */
  47. void print_timer() {
  48. if (start_time.tv_nsec == 0) {
  49. fprintf(stderr, "Error: El temporizador no ha comenzado todavía!\n");
  50. }
  51. else if (stop_time.tv_nsec == 0) {
  52. fprintf(stderr, "Error: El temporizador no ha terminado todavía!\n");
  53. }
  54. else {
  55. time_t seconds = stop_time.tv_sec - start_time.tv_sec;
  56. unsigned long long milliseconds = round((stop_time.tv_nsec - start_time.tv_nsec)/ 1.0e6); // Convert nanoseconds to milliseconds
  57. if (milliseconds > 999) {
  58. seconds++;
  59. milliseconds = 0;
  60. }
  61. unsigned int minutes = 0;
  62. unsigned int hours = 0;
  63. if (seconds >= 3600) {
  64. hours = seconds / 3600;
  65. seconds = seconds - (hours * 3600);
  66. if (seconds >= 60) {
  67. minutes = seconds / 60;
  68. seconds = seconds - (minutes * 60);
  69. }
  70. fprintf(stdout, "Tiempo de ejecución: %d horas, %d minutos y %ld.%lld segundos\n", hours, minutes, seconds, milliseconds);
  71. }
  72. else if (seconds >= 60) {
  73. minutes = seconds / 60;
  74. seconds = seconds - (minutes * 60);
  75. fprintf(stdout, "Tiempo de ejecución: %d minutos y %ld.%lld segundos\n", minutes, seconds, milliseconds);
  76. }
  77. else {
  78. fprintf(stdout, "Tiempo de ejecución: %ld.%lld segundos\n", seconds, milliseconds);
  79. }
  80. }
  81. }