File indexing completed on 2023-03-17 11:28:16
0001 #include <stdlib.h>
0002 #include "Stopwatch.h"
0003
0004 double seconds() { return ((double)clock()) / (double)CLOCKS_PER_SEC; }
0005
0006 void Stopwtach_reset(Stopwatch Q) {
0007 Q->running = 0;
0008 Q->last_time = 0.0;
0009 Q->total = 0.0;
0010 }
0011
0012 Stopwatch new_Stopwatch(void) {
0013 Stopwatch S = (Stopwatch)malloc(sizeof(Stopwatch_struct));
0014 if (S == NULL)
0015 return NULL;
0016
0017 Stopwtach_reset(S);
0018 return S;
0019 }
0020
0021 void Stopwatch_delete(Stopwatch S) {
0022 if (S != NULL)
0023 free(S);
0024 }
0025
0026
0027
0028 void Stopwatch_start(Stopwatch Q) {
0029 if (!(Q->running)) {
0030 Q->running = 1;
0031 Q->total = 0.0;
0032 Q->last_time = seconds();
0033 }
0034 }
0035
0036
0037
0038
0039
0040
0041
0042 void Stopwatch_resume(Stopwatch Q) {
0043 if (!(Q->running)) {
0044 Q->last_time = seconds();
0045 Q->running = 1;
0046 }
0047 }
0048
0049 void Stopwatch_stop(Stopwatch Q) {
0050 if (Q->running) {
0051 Q->total += seconds() - Q->last_time;
0052 Q->running = 0;
0053 }
0054 }
0055
0056 double Stopwatch_read(Stopwatch Q) {
0057 if (Q->running) {
0058 double t = seconds();
0059 Q->total += t - Q->last_time;
0060 Q->last_time = t;
0061 }
0062 return Q->total;
0063 }