c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

commit a7b47762120fad899e87f187e48c7241bf0dbebc
parent 19450dfb5683b14c43126469cda6bd4a4f74ddda
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 19 Oct 2026 14:03:57 -0500

Profiling macro

Diffstat:
Mperf-aware-programming/haversine/Makefile | 4++--
Mperf-aware-programming/haversine/history.txt | 2++
Mperf-aware-programming/haversine/json.c | 30++++++++++--------------------
Mperf-aware-programming/haversine/main.c | 6++----
Aperf-aware-programming/haversine/time.h | 6++++++
5 files changed, 22 insertions(+), 26 deletions(-)

diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile @@ -1,11 +1,11 @@ gen: mkdir artifacts || true - python3 gen.py artifacts/out.json 30000 + python3 gen.py artifacts/out.json 300000 clean: rm -rf out.json build: - gcc main.c -lm + gcc -g main.c -lm run: build ./a.out artifacts/out.json diff --git a/perf-aware-programming/haversine/history.txt b/perf-aware-programming/haversine/history.txt @@ -9,3 +9,5 @@ Free line 504007 Compute 11049943 Free havs 2653 Free coords 215784 + +That's ~80% on parsing the json subset. diff --git a/perf-aware-programming/haversine/json.c b/perf-aware-programming/haversine/json.c @@ -2,6 +2,7 @@ #include <math.h> #include <string.h> #include <stdlib.h> +#include "time.h" struct Coordinates { double x0; @@ -164,10 +165,8 @@ double* computeHaversines(struct Coordinates* coords, int size, double radius) { struct Coordinates* loadJson(char* filename, int* size, uint tsPerSecond) { - uint64_t startLf = rdtsc(); - char* line = loadFileToString(filename); - uint64_t endLf = rdtsc(); - printf("Load File TS\t\t%lld\n", endLf - startLf); + char* line = NULL; + TIME_BLOCK_EXEC("Load file TS", line = loadFileToString(filename)); if(line == NULL) { return NULL; @@ -176,27 +175,18 @@ struct Coordinates* loadJson(char* filename, int* size, uint tsPerSecond) { int sz = 0; uint64_t startParse = rdtsc(); - struct Coordinates* res = parseCoordinates(line, &sz); - uint64_t endParse = rdtsc(); - printf("Parse File\t\t%lld\n", endParse - startParse); + struct Coordinates* res = NULL; - size[0] = sz; + TIME_BLOCK_EXEC("Parse File", res = parseCoordinates(line, &sz)); - uint64_t startFree = rdtsc(); - free(line); - uint64_t endFree = rdtsc(); - printf("Free line\t\t%lld\n", endFree - startFree); + size[0] = sz; + TIME_BLOCK_EXEC("Free line", free(line)); - uint64_t startCompute = rdtsc(); - double* havs = computeHaversines(res,sz, 6372.8); - uint64_t endCompute = rdtsc(); - printf("Compute\t\t\t%lld\n", endCompute - startCompute); + double* havs = NULL; + TIME_BLOCK_EXEC("Compute havs", havs = computeHaversines(res,sz, 6372.8)); - uint64_t startFreeH = rdtsc(); - free(havs); - uint64_t endFreeH = rdtsc(); - printf("Free havs\t\t%lld\n", endFreeH - startFreeH); + TIME_BLOCK_EXEC("Free havs", free(havs)); return res; } diff --git a/perf-aware-programming/haversine/main.c b/perf-aware-programming/haversine/main.c @@ -1,6 +1,7 @@ #include "cpu.c" #include <inttypes.h> #include "json.c" +#include "time.h" int main(int argc, char** argv) { @@ -25,8 +26,5 @@ int main(int argc, char** argv) { // printf("x0: %.10f, x1: %.10f, y0: %.10f, y1: %.10f\n", current->x0, current->x1, current->y0, current->y1); // } - uint64_t startFree = rdtsc(); - free(coords); - uint64_t endFree = rdtsc(); - printf("Free coords\t\t%lld\n", endFree - startFree); + TIME_BLOCK_EXEC("Free coords", free(coords)); } diff --git a/perf-aware-programming/haversine/time.h b/perf-aware-programming/haversine/time.h @@ -0,0 +1,6 @@ +#define TIME_BLOCK_EXEC(name, ...) do { \ + uint64_t start = rdtsc(); \ + __VA_ARGS__; \ + uint64_t end = rdtsc(); \ + printf("%s\t\t%lld\n", name, end - start); \ +} while(0);