New Linux Timing Code

For some time, I had a problem with my old Linux timing code. The units hr_time.h/c had to have the includes arranged in the right order and some flags set for it to compile. Plus it never seemed right having to do this in hr_time.h
#include <linux/time.h>
#define __timespec_defined 1
#define __itimerspec_defined 1
#define __timeval_defined 1
But today I found some better code on this SolarianProgrammer website and I’m always happy to give credit where it’s due. I’ve witched to using C17 which clang on Ubuntu (clang 14) and Raspberry Pi (clang 11) both seem to accept.
So now my hr_time.h looks like this:
hr_time.h
#pragma once
#include <stdlib.h>
#include <time.h>
void startTimer();
void stopTimer();
double diff();
hr_time.c
#include "hr_time.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
struct timespec start;
struct timespec end;
void startTimer() {
if (timespec_get(&start, TIME_UTC) != TIME_UTC) {
printf("Error in calling timespec_get\n");
exit(EXIT_FAILURE);
}
};
void stopTimer() {
if (timespec_get(&end, TIME_UTC) != TIME_UTC) {
printf("Error in calling timespec_get\n");
exit(EXIT_FAILURE);
}
};
double diff()
{
return (double)(end.tv_sec - start.tv_sec) +
((double)(end.tv_nsec - start.tv_nsec) / 1000000000L);
}
I’m only ever using one timer, if you want to modify this to pass in a timer in the startTimer and stopTimer then it’s easy enough.
Remember the cellular automation Life? I covered it in previous blog entries
I occasionally write answers on Quora.com and many recurring questions are about assembly language. Now I used to write games in 6502 and Z80 assembly language when the only alternative was Basic. But assembly language is very tedious to write, painstaking and it’s slow to write programs in assembly language. Most of your code is moving values between memory and registers or vice versa.
Every month a new Tiobe Index is published purporting to show the most popular programming languages. Except, if you look at other programming language popularity surveys, lists etc, the Tiobe Index increasingly seems to be at odds with everybody else.
Toptal.com is a site for recruiting freelances for projects. They have
There’s nothing worse than a program halting with a simple “Segmentation fault” and no idea where or why. It happened to me today working on the 2nd eBook (for Raspberry Pi) and I had to figure out where it was going wrong.
Updated 20/03/2025
First I modded the code that inits sound to this:
If you use Windows 11 you’ll be aware of one or two issues with it; the file explorer popup menu needs a second click to take you to the Windows 10 menu with Copy, cut paste etc.