Skip to content

Instantly share code, notes, and snippets.

@rene-d
Last active March 31, 2020 04:00
Show Gist options
  • Save rene-d/0dd8f2a5ef234d104f699676704142a1 to your computer and use it in GitHub Desktop.
Save rene-d/0dd8f2a5ef234d104f699676704142a1 to your computer and use it in GitHub Desktop.
bench_pc
#!/bin/bash
docker run --rm -v $PWD:/a -w /a buildpack-deps:buster cc bench_pc.c $*
time docker run --rm -v $PWD:/a debian:buster-slim /a/a.out
time docker run --rm -v $PWD:/a --privileged debian:buster-slim /a/a.out
#include <stdlib.h>
#include <stdio.h>
#define NB_PASSES 10000
#define NB_ELEMS (500 * 64)
void bench_pc_ram_int();
void bench_pc_ram_float();
static float *tab_float;
static int *tab_int;
int main()
{
int i;
tab_int = malloc(NB_ELEMS * sizeof(int));
tab_float = malloc(NB_ELEMS * sizeof(float));
for (i = 0; i < NB_PASSES; i++)
{
bench_pc_ram_int();
bench_pc_ram_float();
if ((NB_PASSES < 100) || (i % (NB_PASSES / 100) == 0))
{
printf(".");
fflush(NULL);
}
}
printf("\n");
}
void bench_pc_ram_int()
{
int i;
tab_int[0] = 0;
for (i = 1; i < NB_ELEMS; i++)
{
tab_int[i] = tab_int[i - 1] + 1;
}
for (i = 1; i < NB_ELEMS / 2; i++)
{
tab_int[i] = tab_int[i] + tab_int[NB_ELEMS / 2 + i];
}
for (i = 1; i < NB_ELEMS / 4; i++)
{
tab_int[i] = tab_int[i] * tab_int[NB_ELEMS / 4 + i];
}
for (i = 1; i < NB_ELEMS / 16; i++)
{
tab_int[i] = tab_int[i] / tab_int[NB_ELEMS / 16 + i];
}
for (i = 1; i < NB_ELEMS / 32; i++)
{
tab_int[i] = tab_int[i] - tab_int[NB_ELEMS / 32 + i];
}
for (i = 1; i < NB_ELEMS / 64; i++)
{
tab_int[i] = -tab_int[i] + tab_int[NB_ELEMS / 64 + i];
}
}
void bench_pc_ram_float()
{
int i;
tab_float[0] = 0;
for (i = 1; i < NB_ELEMS; i++)
{
tab_float[i] = tab_float[i - 1] + 0.1;
}
for (i = 1; i < NB_ELEMS / 2; i++)
{
tab_float[i] = tab_float[i] + tab_float[NB_ELEMS / 2 + i];
}
for (i = 1; i < NB_ELEMS / 4; i++)
{
tab_float[i] = tab_float[i] * tab_float[NB_ELEMS / 4 + i];
}
for (i = 1; i < NB_ELEMS / 16; i++)
{
tab_float[i] = tab_float[i] / tab_float[NB_ELEMS / 16 + i];
}
for (i = 1; i < NB_ELEMS / 32; i++)
{
tab_float[i] = tab_float[i] - tab_float[NB_ELEMS / 32 + i];
}
for (i = 1; i < NB_ELEMS / 64; i++)
{
tab_float[i] = -tab_float[i] + tab_float[NB_ELEMS / 64 + i];
}
}
#!/bin/bash
cp bench_pc.c /tmp
docker run -ti --rm -v /tmp:/a -w /a builder cc bench_pc.c $*
docker create --name perf_d -ti -v /tmp:/a debian:buster-slim /a/a.out
docker create --name perf_p -ti -v /tmp:/a --privileged debian:buster-slim /a/a.out
echo
echo "NATIVE"
time ./a.out
echo
echo "DOCKER"
time docker start -i perf_d
echo
echo "PRIVILEGED"
time docker start -i perf_p
echo
echo "NATIVE"
./a.out & perf record -a -o perf_n.data -- sleep 15
echo
echo "DOCKER"
docker start -i perf_d & perf record -a -o perf_d.data -- sleep 15
echo
echo "PRIVILEGED"
docker start -i perf_p & perf record -a -o perf_p.data -- sleep 15
docker rm perf_d
docker rm perf_p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment