mirror of
https://github.com/pocketpy/pocketpy
synced 2026-02-04 06:30:17 +00:00
chore: add struct array traversal benchmark
Co-authored-by: blueloveTH <28104173+blueloveTH@users.noreply.github.com>
This commit is contained in:
parent
8d1d7769b5
commit
7c7675b9d6
BIN
benchmarks/struct_array_traverse
Executable file
BIN
benchmarks/struct_array_traverse
Executable file
Binary file not shown.
53
benchmarks/struct_array_traverse.c
Normal file
53
benchmarks/struct_array_traverse.c
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#define _POSIX_C_SOURCE 199309L
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
static inline uint64_t nanos(void) {
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t bench_int(void) {
|
||||||
|
int arr[1000];
|
||||||
|
for (int i = 0; i < 1000; ++i) arr[i] = 1;
|
||||||
|
|
||||||
|
volatile long long total = 0;
|
||||||
|
uint64_t start = nanos();
|
||||||
|
for (int round = 0; round < 1000; ++round) {
|
||||||
|
long long sum = 0;
|
||||||
|
for (int i = 0; i < 1000; ++i) sum += arr[i];
|
||||||
|
total += sum;
|
||||||
|
}
|
||||||
|
uint64_t end = nanos();
|
||||||
|
// prevent optimization
|
||||||
|
if (total == 0) printf("unused: %lld\n", total);
|
||||||
|
return (end - start) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t bench_char(void) {
|
||||||
|
char arr[1000];
|
||||||
|
for (int i = 0; i < 1000; ++i) arr[i] = 1;
|
||||||
|
|
||||||
|
volatile long long total = 0;
|
||||||
|
uint64_t start = nanos();
|
||||||
|
for (int round = 0; round < 1000; ++round) {
|
||||||
|
long long sum = 0;
|
||||||
|
for (int i = 0; i < 1000; ++i) sum += arr[i];
|
||||||
|
total += sum;
|
||||||
|
}
|
||||||
|
uint64_t end = nanos();
|
||||||
|
if (total == 0) printf("unused: %lld\n", total);
|
||||||
|
return (end - start) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
uint64_t int_avg = bench_int();
|
||||||
|
uint64_t char_avg = bench_char();
|
||||||
|
printf("int[1000] average traversal (ns): %llu\n",
|
||||||
|
(unsigned long long)int_avg);
|
||||||
|
printf("char[1000] average traversal (ns): %llu\n",
|
||||||
|
(unsigned long long)char_avg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user