├── .gitignore ├── Makefile ├── README.md ├── package.json ├── src ├── timer.c └── timer.h └── test.c /.gitignore: -------------------------------------------------------------------------------- 1 | # executable 2 | test 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: test.c src/timer.c 3 | $(CC) $^ -o $@ -Wall -Wextra 4 | 5 | clean: 6 | rm -f test 7 | 8 | .PHONY: clean 9 | 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # timer.h 2 | 3 | Simple timer with microsecond resolution. 4 | 5 | It uses `gettimeofday()`, so is not portable to non-Unix 6 | platforms. 7 | 8 | # Installation 9 | 10 | Install it with [clib](https://github.com/clibs/clib): 11 | 12 | $ clib install clibs/timer 13 | 14 | If you want to use it manually, copy `src/timer.h` and 15 | `src/timer.c` to your project directory and include them on your Makefile. 16 | 17 | # Usage 18 | 19 | This module defines a `timer_t` structure that can count 20 | deltas from microseconds up to hours. 21 | 22 | ```c 23 | #include "timer.h" 24 | 25 | int main() 26 | { 27 | timer_t timer; 28 | timer_start(&timer); 29 | 30 | /* do whatever you want */ 31 | 32 | timer_pause(&timer); 33 | 34 | /* call one of the several delta functions: */ 35 | timer_delta_us(&timer); 36 | timer_delta_ms(&timer); 37 | timer_delta_s(&timer); 38 | timer_delta_m(&timer); 39 | timer_delta_h(&timer); 40 | } 41 | ``` 42 | 43 | The file `test.c` shows a full example. 44 | 45 | # License 46 | 47 | (The MIT License) 48 | 49 | Copyright (c) 2011,2013 Alexandre Dantas 50 | 51 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 52 | 53 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 54 | 55 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 56 | 57 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "timer", 3 | "version" : "1.0.0", 4 | "repo" : "clibs/timer", 5 | "description" : "Timer with microsecond resolution", 6 | "keywords" : ["timer", "time", "delta", "count", "second"], 7 | "license" : "MIT", 8 | "src" : ["src/timer.h", "src/timer.c"] 9 | } 10 | 11 | -------------------------------------------------------------------------------- /src/timer.c: -------------------------------------------------------------------------------- 1 | #include "timer.h" 2 | 3 | #define MICROSSECONDS_IN_SECONDS 1000000 4 | 5 | /** Local function that returns the ticks 6 | * (number of microsseconds) since the Epoch. 7 | */ 8 | static suseconds_t get_ticks() 9 | { 10 | struct timeval tmp; 11 | gettimeofday(&(tmp), NULL); 12 | 13 | return (tmp.tv_usec) + (tmp.tv_sec * MICROSSECONDS_IN_SECONDS); 14 | } 15 | 16 | void timer_start(timer_t* t) 17 | { 18 | t->start_mark = get_ticks(); 19 | t->pause_mark = 0; 20 | t->running = true; 21 | t->paused = false; 22 | } 23 | 24 | void timer_pause(timer_t* t) 25 | { 26 | if (!(t->running) || (t->paused)) return; 27 | 28 | t->pause_mark = get_ticks() - (t->start_mark); 29 | t->running = false; 30 | t->paused = true; 31 | } 32 | 33 | void timer_unpause(timer_t* t) 34 | { 35 | if (t->running || !(t->paused)) return; 36 | 37 | t->start_mark = get_ticks() - (t->pause_mark); 38 | t->running = true; 39 | t->paused = false; 40 | } 41 | 42 | suseconds_t timer_delta_us (timer_t* t) 43 | { 44 | if (t->running) 45 | return get_ticks() - (t->start_mark); 46 | 47 | if (t->paused) 48 | return t->pause_mark; 49 | 50 | // Will never actually get here 51 | return (t->pause_mark) - (t->start_mark); 52 | } 53 | 54 | long timer_delta_ms(timer_t* t) 55 | { 56 | return (timer_delta_us(t) / 1000); 57 | } 58 | 59 | long timer_delta_s(timer_t* t) 60 | { 61 | return (timer_delta_us(t) / 1000000); 62 | } 63 | 64 | long timer_delta_m(timer_t* t) 65 | { 66 | return (timer_delta_s(t) / 60); 67 | } 68 | 69 | long timer_delta_h(timer_t* t) 70 | { 71 | return (timer_delta_m(t) / 60); 72 | } 73 | 74 | -------------------------------------------------------------------------------- /src/timer.h: -------------------------------------------------------------------------------- 1 | /** timer.h 2 | * Microsecond timer in C, using `gettimeofday()`. 3 | * 4 | * Note: `gettimeofday()` is Unix, GNU/Linux and 5 | * Mac OS X system-specific. 6 | * 7 | * The only portable function is time.h's `clock()`, 8 | * but it isn't very precise. 9 | * 10 | * See: http://www.songho.ca/misc/timer/timer.html 11 | * 12 | * Copyright (c) 2011,2013 Alexandre Dantas 13 | */ 14 | 15 | #ifndef TIMER_H_DEFINED 16 | #define TIMER_H_DEFINED 17 | 18 | #include 19 | #include /* gettimeofday() */ 20 | #include /* bool */ 21 | 22 | typedef struct timer_t 23 | { 24 | suseconds_t start_mark; /* Timer start point */ 25 | suseconds_t pause_mark; /* In case we pause the timer */ 26 | bool running; /* Is it running? */ 27 | bool paused; /* Is it paused? */ 28 | 29 | } timer_t; 30 | 31 | /** Starts the timer. 32 | * 33 | * @note If called multiple times, restarts the timer. 34 | */ 35 | void timer_start(timer_t* t); 36 | 37 | /** Pauses the timer. 38 | * 39 | * @note If called multiple times, it does nothing. 40 | */ 41 | void timer_pause(timer_t* t); 42 | 43 | /** Unpauses the timer. 44 | * 45 | * @note If the timer's not paused or this is called 46 | * multiple times, it does nothing. 47 | */ 48 | void timer_unpause(timer_t* t); 49 | 50 | /** Returns the time difference in microseconds 51 | * 52 | * @note (1/1000000 seconds) 53 | */ 54 | suseconds_t timer_delta_us(timer_t* t); 55 | 56 | /** Returns the time difference in miliseconds. 57 | * 58 | * @note (1/1000 seconds) 59 | */ 60 | long timer_delta_ms(timer_t* t); 61 | 62 | /** Returns the time difference in seconds. */ 63 | long timer_delta_s(timer_t* t); 64 | 65 | /** Returns the time difference in minutes (60 seconds). */ 66 | long timer_delta_m(timer_t* t); 67 | 68 | /** Returns the time difference in hours (3600 seconds). */ 69 | long timer_delta_h(timer_t* t); 70 | 71 | #endif 72 | 73 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* usleep */ 3 | #include "src/timer.h" 4 | 5 | int main() 6 | { 7 | timer_t timer; 8 | printf("Timer started\n"); 9 | 10 | timer_start(&timer); 11 | usleep(1e6); 12 | timer_pause(&timer); 13 | 14 | printf("Delta (us): %ld\n", timer_delta_us(&timer)); 15 | printf("Delta (ms): %ld\n", timer_delta_ms(&timer)); 16 | printf("That should be around 1 second\n\n"); 17 | 18 | timer_unpause(&timer); 19 | usleep(5e5); 20 | timer_pause(&timer); 21 | 22 | printf("Delta (us): %ld\n", timer_delta_us(&timer)); 23 | printf("Delta (ms): %ld\n", timer_delta_ms(&timer)); 24 | printf("And that should add to 1.5 seconds\n\n"); 25 | 26 | return 0; 27 | } 28 | 29 | --------------------------------------------------------------------------------