Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Last active December 16, 2015 14:59
Show Gist options
  • Save stepancheg/5452524 to your computer and use it in GitHub Desktop.
Save stepancheg/5452524 to your computer and use it in GitHub Desktop.
#!/bin/sh -e
die() {
echo "$@" >&2
exit 1
}
test -n "$CC" || die '$CC not set'
$CC -O3 -std=c99 -c ./foo.c
$CC -O3 -std=c99 -c ./main.c
$CC *.o
# vim: set ts=4 sw=4 et:
int foo(int p) {
return p == 1 || p == 4 || p == 6 || p == 10 || p == 12 || p == 25;
}
#define _GNU_SOURCE
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <stdbool.h>
#include <unistd.h>
int foo(int);
#define E(call) do { if (__builtin_expect(call < 0, 0)) { perror(#call); exit(1); }; } while (false)
long long microseconds() {
struct timeval tv;
E(gettimeofday(&tv, NULL));
return 1000000LL * tv.tv_sec + tv.tv_usec;
}
int main(void) {
int random_data[1000000];
for (int i = 0; i < 1000000; ++i) {
random_data[i] = ((unsigned) random()) % 150;
}
for (;;) {
long long start = microseconds();
long long count = 0;
while (microseconds() - start < 1000000) {
int iter = 1000000;
for (int i = 0; i < iter; ++i) {
foo(random_data[i]);
}
count += iter;
}
long long dps = 1000000L * (microseconds() - start);
long long ps_per_call = dps / count;
printf("%lld ps per call\n", ps_per_call);
}
return 0;
}
// vim: set ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment