Skip to content

Instantly share code, notes, and snippets.

@PhilipWitte
Last active November 30, 2018 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilipWitte/53adb83be316385af1b9c14e8bf81c18 to your computer and use it in GitHub Desktop.
Save PhilipWitte/53adb83be316385af1b9c14e8bf81c18 to your computer and use it in GitHub Desktop.
C Return Benchmark
// INSTRUCTIONS:
// Compile with `gcc -s -O3 -flto -o bench bench.c`
// Run with `./bench 100000 10000`
// IMPORTANT:
// The two parameters are REQUIRED and are the numbers for:
// 1. How many points in the array.
// 2. How many times to cycle through the array.
//
// The numbers are not multiplied to ensure the compiler can't cheat.
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define BY_COPY
typedef struct Point Point;
struct Point
{
float x, y, z;
};
#ifdef BY_COPY
Point Point_init(float x, float y, float z)
{
return (Point) { x, y, z };
}
Point add(Point* a, Point* b)
{
return (Point) {
a->x + b->x,
a->y + b->y,
a->z + b->z
};
}
Point sub(Point* a, Point* b)
{
return (Point) {
a->x - b->x,
a->y - b->y,
a->z - b->z
};
}
Point foo(Point* p, float n)
{
Point x = Point_init(n, n, n);
return add(p, &x);
}
Point bar(Point* p, float n)
{
Point x = foo(p, n);
return sub(p, &x);
}
#else
void Point_init(float x, float y, float z, Point* result)
{
*result = (Point) { x, y, z };
}
void add(Point* a, Point* b, Point* result)
{
*result = (Point) {
a->x + b->x,
a->y + b->y,
a->z + b->z
};
}
void sub(Point* a, Point* b, Point* result)
{
*result = (Point) {
a->x - b->x,
a->y - b->y,
a->z - b->z
};
}
void foo(Point* p, float n, Point* result)
{
Point x;
Point_init(n, n, n, &x);
add(p, &x, result);
}
void bar(Point* p, float n, Point* result)
{
Point x;
foo(p, n, &x);
sub(p, &x, result);
}
#endif
float calc(Point* p)
{
return p->x + p->y + p->z;
}
int main(int argc , char** argv)
{
int pointCount = atoi(argv[1]);
Point* points = malloc(sizeof(Point) * pointCount);
for (int i = 0; i < pointCount; ++i) {
#ifdef BY_COPY
points[i] = Point_init(i, i, i);
#else
Point_init(i, i, i, &points[i]);
#endif
}
clock_t start = clock();
int cycleCount = atoi(argv[2]);
for (int c = 0; c < cycleCount; ++c) {
#ifdef BY_COPY
if (c % 2 == 0) {
for (int i = 0; i < pointCount; ++i) {
Point t = bar(&points[i], i + 1);
points[i] = add(&points[i], &t);
}
}
else {
for (int i = 0; i < pointCount; ++i) {
Point t = bar(&points[i], i + 1);
points[i] = sub(&points[i], &t);
}
}
#else
if (c % 2 == 0) {
for (int i = 0; i < pointCount; ++i) {
Point t;
bar(&points[i], i + 1, &t);
add(&points[i], &t, &points[i]);
}
}
else {
for (int i = 0; i < pointCount; ++i) {
Point t;
bar(&points[i], i + 1, &t);
sub(&points[i], &t, &points[i]);
}
}
#endif
}
double duration = (clock() - start) / (double)CLOCKS_PER_SEC;
printf("Time: %f\n", duration);
float total = 0;
for (int i = 0; i < pointCount; ++i) {
total += calc(points + i);
}
printf("Total: %f\n", total);
free(points);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment