Skip to content

Instantly share code, notes, and snippets.

@MarcoLizza
Created August 30, 2021 13:32
Show Gist options
  • Save MarcoLizza/1063fcd917f8369e5fe1aa019b540a08 to your computer and use it in GitHub Desktop.
Save MarcoLizza/1063fcd917f8369e5fe1aa019b540a08 to your computer and use it in GitHub Desktop.
Integer Math Profiling
#include <stdio.h>
#include <math.h>
#include <time.h>
int ifloor(float x)
{
const int i = (int)x;
return (x < 0.0f && x != i ? i - 1 : i);
}
int iceil(float x)
{
const int i = (int)x;
return (x > 0.0f && x != i ? i + 1 : i);
}
int main(void) {
float x = 123456.789f;
clock_t s = clock();
for (int i = 0; i < 1000000000; ++i) {
int v = (int)floorf(x);
}
clock_t e = clock();
printf("%f\n", (float)(e - s) / (float)CLOCKS_PER_SEC);
clock_t s1 = clock();
for (int i = 0; i < 1000000000; ++i) {
int v = ifloor(x);
}
clock_t e1 = clock();
printf("%f\n", (float)(e1 - s1) / (float)CLOCKS_PER_SEC);
printf("%d\n", (int)ceilf(x));
printf("%d\n", iceil(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment