chore: parameterize benchmark constants

Co-authored-by: blueloveTH <28104173+blueloveTH@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-01-04 05:02:25 +00:00
parent 7c5a033a86
commit 7e510f91f3

View File

@ -3,6 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#define ARRAY_SIZE 1000
#define NUM_ROUNDS 100000
static inline uint64_t nanos(void) { static inline uint64_t nanos(void) {
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
@ -10,36 +13,36 @@ static inline uint64_t nanos(void) {
} }
static uint64_t bench_int(void) { static uint64_t bench_int(void) {
int arr[1000]; int arr[ARRAY_SIZE];
for (int i = 0; i < 1000; ++i) arr[i] = 1; for (int i = 0; i < ARRAY_SIZE; ++i) arr[i] = 1;
volatile long long total = 0; volatile long long total = 0;
uint64_t start = nanos(); uint64_t start = nanos();
for (int round = 0; round < 100000; ++round) { for (int round = 0; round < NUM_ROUNDS; ++round) {
long long sum = 0; long long sum = 0;
for (int i = 0; i < 1000; ++i) sum += arr[i]; for (int i = 0; i < ARRAY_SIZE; ++i) sum += arr[i];
total += sum; total += sum;
} }
uint64_t end = nanos(); uint64_t end = nanos();
// prevent optimization // prevent optimization
if (total == 0) printf("unused: %lld\n", total); if (total == 0) printf("unused: %lld\n", total);
return (end - start) / 100000; return (end - start) / NUM_ROUNDS;
} }
static uint64_t bench_char(void) { static uint64_t bench_char(void) {
char arr[1000]; char arr[ARRAY_SIZE];
for (int i = 0; i < 1000; ++i) arr[i] = 1; for (int i = 0; i < ARRAY_SIZE; ++i) arr[i] = 1;
volatile long long total = 0; volatile long long total = 0;
uint64_t start = nanos(); uint64_t start = nanos();
for (int round = 0; round < 100000; ++round) { for (int round = 0; round < NUM_ROUNDS; ++round) {
long long sum = 0; long long sum = 0;
for (int i = 0; i < 1000; ++i) sum += arr[i]; for (int i = 0; i < ARRAY_SIZE; ++i) sum += arr[i];
total += sum; total += sum;
} }
uint64_t end = nanos(); uint64_t end = nanos();
if (total == 0) printf("unused: %lld\n", total); if (total == 0) printf("unused: %lld\n", total);
return (end - start) / 100000; return (end - start) / NUM_ROUNDS;
} }
int main(void) { int main(void) {