Skip to content

Instantly share code, notes, and snippets.

@esote
Last active September 2, 2021 14:58
Show Gist options
  • Save esote/f22202115d008d1449d0477f3dc3ee4b to your computer and use it in GitHub Desktop.
Save esote/f22202115d008d1449d0477f3dc3ee4b to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <stdio.h>
#include <time.h>
#define NNN 2048
#define BENCH_COUNT 10
#define timespecsub(tsp, usp, vsp) \
do \
{ \
(vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
(vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
if ((vsp)->tv_nsec < 0) \
{ \
(vsp)->tv_sec--; \
(vsp)->tv_nsec += 1000000000L; \
} \
} while (0)
static void benchmark(void (*f)(void), const char *name);
static void copyij();
static void copyji();
static void init_mat();
static volatile int src[NNN][NNN], dst[NNN][NNN];
int main(void)
{
size_t i;
printf("ij:\n");
for (i = 0; i < BENCH_COUNT; i++)
{
benchmark(copyij, "copyij");
}
printf("ji:\n");
for (i = 0; i < BENCH_COUNT; i++)
{
benchmark(copyji, "copyji");
}
}
static void
benchmark(void (*f)(void), const char *name)
{
struct timespec start, end, delta;
init_mat();
assert(clock_gettime(CLOCK_MONOTONIC, &start) != -1);
f();
assert(clock_gettime(CLOCK_MONOTONIC, &end) != -1);
timespecsub(&end, &start, &delta);
printf("%s: NNN=%d: elapsed=%lld.%.9ld secs\n", name, NNN, delta.tv_sec,
delta.tv_nsec);
}
#pragma GCC push_options
#pragma GCC optimize("O0")
static void
copyij()
{
size_t i, j;
for (i = 0; i < NNN; i++)
{
for (j = 0; j < NNN; j++)
{
src[i][j] += 1;
}
}
}
static void
copyji()
{
size_t i, j;
for (i = 0; i < NNN; i++)
{
for (j = 0; j < NNN; j++)
{
dst[j][i] += 1;
}
}
}
#pragma GCC pop_options
static void
init_mat()
{
size_t i, j;
for (i = 0; i < NNN; i++)
{
for (j = 0; j < NNN; j++)
{
src[i][j] = dst[i][j] = 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment