Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // clang -O2 -march=native timetest.c -o timetest
- //
- #include <time.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <limits.h>
- static long diff_nano(struct timespec *start, struct timespec *end) {
- return ((end->tv_sec * (1000000000)) + (end->tv_nsec)) - ((start->tv_sec * 1000000000) + (start->tv_nsec));
- }
- int main(int argc, char **argv) {
- struct timespec start, now, delay_time;
- long t;
- delay_time.tv_sec = 0;
- delay_time.tv_nsec = 9000000; // 9ms
- long lowest = LONG_MAX;
- long highest = 0;
- long tt = 0;
- volatile uint64_t a[4096];
- clock_gettime(CLOCK_MONOTONIC_RAW, &start);
- while(1) {
- //
- // DO THE STUFF
- //
- for(size_t i = 0; i < 4096; ++i) {
- a[i]++;
- }
- //
- // NOTE(peter): Wait for 10ms
- //
- clock_nanosleep(CLOCK_MONOTONIC_RAW, 0, &delay_time, 0); // 9ms
- clock_gettime(CLOCK_MONOTONIC_RAW, &now);
- while(diff_nano(&start, &now) < 10000000) { // remaining 1ms
- clock_gettime(CLOCK_MONOTONIC_RAW, &now);
- }
- t = diff_nano(&start, &now);
- start.tv_sec = now.tv_sec;
- start.tv_nsec = now.tv_nsec;
- // NOTE(peter): Print result on change
- if(t < lowest) {
- lowest = t;
- printf("%ld ns (l: %ld h: %ld)\n", t, lowest, highest);
- }
- if(t > highest) {
- highest = t;
- printf("%ld ns (l: %ld h: %ld)\n", t, lowest, highest);
- }
- // NOTE(peter): Print result every 100 frames...
- if(tt++ == 100) {
- printf("%ld ns (l: %ld h: %ld)\n", t, lowest, highest);
- tt = 0;
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment