Skip to content

Instantly share code, notes, and snippets.

@XANOZOID
Created January 15, 2024 13:36
Show Gist options
  • Save XANOZOID/3f86666edc93dcc49bca0ffd3509210f to your computer and use it in GitHub Desktop.
Save XANOZOID/3f86666edc93dcc49bca0ffd3509210f to your computer and use it in GitHub Desktop.
Very bad benchmark
#include "vector_lib.h"
#include <stdio.h>
#include <time.h>
#include <windows.h>
int main() {
struct timespec start, end;
setup();
// Get start time
clock_gettime(CLOCK_MONOTONIC, &start);
run();
// Wait for completion
while (ready() != 1) {
continue;
}
// Get end time
clock_gettime(CLOCK_MONOTONIC, &end);
// Calculate nanoseconds elapsed
long long diff_nsec = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
// Print time in nanoseconds and seconds
printf("done in: %lld nanoseconds (%f ms)\n", diff_nsec, diff_nsec / 1e9 * 1000);
// Get start time
clock_gettime(CLOCK_MONOTONIC, &start);
runSync();
// Get end time
clock_gettime(CLOCK_MONOTONIC, &end);
// Calculate nanoseconds elapsed
diff_nsec = (end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec);
// Print time in nanoseconds and seconds
printf("done in: %lld nanoseconds (%f ms)\n", diff_nsec, diff_nsec / 1e9 * 1000);
return 0;
}
gcc -Wall -O3 main.c vector_lib.c -o program.exe -lm
#include "vector_lib.h"
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
typedef struct {
double x;
double y;
} Vector2D;
typedef struct {
Vector2D vectors[MAX_VECTORS];
Vector2D otherVectors[MAX_WALLS];
int cycleDone;
HANDLE thread;
} GameState;
GameState ggs;
DWORD WINAPI perf(void* vgs) {
GameState* gs = (GameState*)vgs;
// Perform the vector operations
for (int i = 0; i < 5; i++) {
for (int j = 0; j < MAX_VECTORS; j++) {
int moveOut = 0;
for (int k = 0; k < MAX_WALLS; k++) {
if (gs->vectors[j].x < gs->otherVectors[k].x) {
moveOut = 1;
break;
}
}
if (moveOut) {
gs->vectors[j].x += 1;
}
}
}
// printf("Code execution in perf finished!\n");
gs->cycleDone = 1;
return 0;
}
int run() {
ggs.thread = CreateThread(
NULL,
0,
&perf,
&ggs,
0,
NULL);
return 0;
}
int ready() {
return ggs.cycleDone;
}
void setup() {
GameState* gs = &ggs;
srand(1234);
for (int i = 0; i < MAX_VECTORS; i++) {
gs->vectors[i].x = rand() / (double)RAND_MAX;
gs->vectors[i].y = rand() / (double)RAND_MAX;
}
for (int i = 0; i < MAX_WALLS; i++) {
gs->otherVectors[i].x = rand() / (double)RAND_MAX;
gs->otherVectors[i].y = rand() / (double)RAND_MAX;
}
}
int runSync() {
return (int)perf(&ggs);
}
#ifndef VECTORLIB
#define VECTORLIB
#define MAX_VECTORS 6000
#define MAX_WALLS 100
int run();
int runSync();
int ready();
void setup();
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment