Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created February 18, 2026 19:56
Show Gist options
  • Select an option

  • Save billywhizz/072744b51fb697f8a107b506f749efe6 to your computer and use it in GitHub Desktop.

Select an option

Save billywhizz/072744b51fb697f8a107b506f749efe6 to your computer and use it in GitHub Desktop.
kevent with timeout = 0
rate 80986.22 nanos_op 12347
rate 81772.37 nanos_op 12229
rate 81650.72 nanos_op 12247
rate 81608.60 nanos_op 12253
rate 81304.12 nanos_op 12299
kevent64 with timeout = 0 and flags = 0
rate 81713.43 nanos_op 12237
rate 81648.45 nanos_op 12247
rate 81656.38 nanos_op 12246
rate 81582.91 nanos_op 12257
rate 81233.58 nanos_op 12310
kevent64 with timeout = 0 and flags = KEVENT_FLAG_IMMEDIATE
rate 6289150.00 nanos_op 159
rate 6320872.50 nanos_op 158
rate 6311868.00 nanos_op 158
rate 6351545.50 nanos_op 157
rate 6349960.50 nanos_op 157
#include <sys/event.h>
#include <sys/time.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#define NEVENTS 1024
uint64_t hrtime() {
struct timespec t;
if (clock_gettime(CLOCK_MONOTONIC, &t)) return 0;
return (t.tv_sec * (uint64_t) 1e9) + t.tv_nsec;
}
int main () {
struct timespec tspec;
tspec.tv_sec = tspec.tv_nsec = 0;
int count = 0;
int fd = 0;
fd = kqueue();
assert(fd > 0);
count = 100000;
fprintf(stderr, "kevent with timeout = 0\n");
struct kevent events1[NEVENTS];
assert(kevent(fd, NULL, 0, events1, NEVENTS, &tspec) == 0);
for (int j = 0; j < 5; j++) {
uint64_t start = hrtime();
for (int i = 0; i < count; i++) {
assert(kevent(fd, NULL, 0, events1, NEVENTS, &tspec) == 0);
}
uint64_t nanos = hrtime() - start;
float rate = count / ((float)nanos / 1e9);
uint64_t nanos_op = nanos / count;
fprintf(stderr, "rate %.2f nanos_op %llu\n", rate, nanos_op);
}
assert(close(fd) == 0);
fd = kqueue();
assert(fd > 0);
count = 100000;
fprintf(stderr, "kevent64 with timeout = 0 and flags = 0\n");
struct kevent64_s events2[NEVENTS];
assert(kevent64(fd, NULL, 0, events2, NEVENTS, 0, &tspec) == 0);
for (int j = 0; j < 5; j++) {
uint64_t start = hrtime();
for (int i = 0; i < count; i++) {
assert(kevent64(fd, NULL, 0, events2, NEVENTS, 0, &tspec) == 0);
}
uint64_t nanos = hrtime() - start;
float rate = count / ((float)nanos / 1e9);
uint64_t nanos_op = nanos / count;
fprintf(stderr, "rate %.2f nanos_op %llu\n", rate, nanos_op);
}
assert(close(fd) == 0);
fd = kqueue();
assert(fd > 0);
count = 7000000;
struct kevent64_s events3[NEVENTS];
fprintf(stderr, "kevent64 with timeout = 0 and flags = KEVENT_FLAG_IMMEDIATE\n");
assert(kevent64(fd, NULL, 0, events3, NEVENTS, KEVENT_FLAG_IMMEDIATE, &tspec) == 0);
for (int j = 0; j < 5; j++) {
uint64_t start = hrtime();
for (int i = 0; i < count; i++) {
assert(kevent64(fd, NULL, 0, events3, NEVENTS, KEVENT_FLAG_IMMEDIATE, &tspec) == 0);
}
uint64_t nanos = hrtime() - start;
float rate = count / ((float)nanos / 1e9);
uint64_t nanos_op = nanos / count;
fprintf(stderr, "rate %.2f nanos_op %llu\n", rate, nanos_op);
}
assert(close(fd) == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment