commit c3654563695b71bd82a9c2485ed39b3512d6ec53
parent a7b47762120fad899e87f187e48c7241bf0dbebc
Author: Andrew Laack <andrew@laack.co>
Date: Mon, 26 Oct 2026 22:44:36 -0500
Refactored timing code to print at end
Diffstat:
7 files changed, 80 insertions(+), 28 deletions(-)
diff --git a/perf-aware-programming/haversine/Makefile b/perf-aware-programming/haversine/Makefile
@@ -5,7 +5,7 @@ clean:
rm -rf out.json
build:
- gcc -g main.c -lm
+ gcc -g stub.c -lm
run: build
./a.out artifacts/out.json
diff --git a/perf-aware-programming/haversine/cpu.c b/perf-aware-programming/haversine/cpu.c
@@ -1,8 +1,3 @@
-#include <stdio.h>
-#include <inttypes.h>
-#include <stdint.h>
-#include <x86intrin.h>
-#include <unistd.h>
uint64_t rdtsc() {
uint64_t t = __rdtsc();
diff --git a/perf-aware-programming/haversine/json.c b/perf-aware-programming/haversine/json.c
@@ -1,9 +1,3 @@
-#include <stdio.h>
-#include <math.h>
-#include <string.h>
-#include <stdlib.h>
-#include "time.h"
-
struct Coordinates {
double x0;
double x1;
@@ -163,7 +157,7 @@ double* computeHaversines(struct Coordinates* coords, int size, double radius) {
return res;
}
-struct Coordinates* loadJson(char* filename, int* size, uint tsPerSecond) {
+struct Coordinates* loadJson(char* filename, int* size) {
char* line = NULL;
TIME_BLOCK_EXEC("Load file TS", line = loadFileToString(filename));
diff --git a/perf-aware-programming/haversine/main.c b/perf-aware-programming/haversine/main.c
@@ -1,8 +1,3 @@
-#include "cpu.c"
-#include <inttypes.h>
-#include "json.c"
-#include "time.h"
-
int main(int argc, char** argv) {
if(argc != 2) {
@@ -10,12 +5,9 @@ int main(int argc, char** argv) {
return -1;
}
- uint ts = tsPerSecond();
- printf("\nAvg TS / second\t\t%u\n", ts);
- printf("======================================================\n");
int size = 0;
- struct Coordinates* coords = loadJson(argv[1], &size, ts);
+ struct Coordinates* coords = loadJson(argv[1], &size);
if(coords == NULL) {
return -1;
@@ -27,4 +19,6 @@ int main(int argc, char** argv) {
// }
TIME_BLOCK_EXEC("Free coords", free(coords));
+ PRINT_REGIONS();
+ CLEANUP_REGIONS();
}
diff --git a/perf-aware-programming/haversine/stub.c b/perf-aware-programming/haversine/stub.c
@@ -0,0 +1,15 @@
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <x86intrin.h>
+#include <unistd.h>
+
+#include "cpu.c"
+#include "time.c"
+
+#include "json.c"
+#include "main.c"
diff --git a/perf-aware-programming/haversine/time.c b/perf-aware-programming/haversine/time.c
@@ -0,0 +1,60 @@
+struct Region {
+ char* name;
+ uint64_t time;
+};
+
+struct Region* regions = nullptr;
+int regionNumber = 0;
+
+void addRegion(char* name, uint64_t time) {
+
+ char *heapName = malloc(strlen(name) + 1);
+ strcpy(heapName, name);
+ struct Region* newRegion = malloc(sizeof(struct Region) * (regionNumber+1));
+
+ if(regionNumber != 0) {
+ memcpy(newRegion, regions, sizeof(struct Region)*regionNumber);
+ free(regions);
+ }
+
+ newRegion[regionNumber].name = heapName;
+ newRegion[regionNumber].time = time;
+
+ regionNumber += 1;
+ regions = newRegion;
+}
+
+void printRegion() {
+
+ uint ts = tsPerSecond();
+ printf("\nAvg TS / second\t\t%u\n", ts);
+ printf("======================================================\n");
+
+ for(int i = 0; i < regionNumber; ++i) {
+ char* name = regions[i].name;
+ uint64_t time = regions[i].time;
+ printf("%s\t\t%lld\n", name, time);
+ }
+}
+
+void cleanupRegions() {
+ for(int i = 0; i < regionNumber; ++i) {
+ free(regions[i].name);
+ }
+ free(regions);
+}
+
+#define TIME_BLOCK_EXEC(name, ...) do { \
+ uint64_t start = rdtsc(); \
+ __VA_ARGS__; \
+ uint64_t end = rdtsc(); \
+ addRegion(name, end - start); \
+} while(0);
+
+#define CLEANUP_REGIONS() do { \
+ cleanupRegions(); \
+} while(0);
+
+#define PRINT_REGIONS() do { \
+ printRegion(); \
+} while(0);
diff --git a/perf-aware-programming/haversine/time.h b/perf-aware-programming/haversine/time.h
@@ -1,6 +0,0 @@
-#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);