Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
benchmark: add
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Aug 13, 2024
1 parent b0127a5 commit 93c41ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions include/benchmark.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

#include <SDL2/SDL_stdinc.h>

typedef struct {
Uint64 start;
} BENCHMARK;

BENCHMARK *Benchmark_Start(void);

#define Benchmark_End(...) \
Benchmark_End_Impl(__FILE__, __LINE__, __func__, __VA_ARGS__)

void Benchmark_End_Impl(
const char *file, int32_t line, const char *func, BENCHMARK *b,
const char *message);
29 changes: 29 additions & 0 deletions src/benchmark.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "benchmark.h"

#include "log.h"
#include "memory.h"

#include <SDL2/SDL_timer.h>

BENCHMARK *Benchmark_Start(void)
{
BENCHMARK *const b = Memory_Alloc(sizeof(BENCHMARK));
b->start = SDL_GetPerformanceCounter();
return b;
}

void Benchmark_End_Impl(
const char *const file, const int32_t line, const char *const func,
BENCHMARK *b, const char *const message)
{
const double elapsed = (double)(SDL_GetPerformanceCounter() - b->start)
* 1000.0 / (double)SDL_GetPerformanceFrequency();

if (message == NULL) {
Log_Message(file, line, func, "took %.02f ms", elapsed);
} else {
Log_Message(file, line, func, "%s: took %.02f ms", message, elapsed);
}

Memory_FreePointer(&b);
}

0 comments on commit 93c41ee

Please sign in to comment.