Skip to content

Instantly share code, notes, and snippets.

@bensuperpc
Forked from Lewiscowles1986/benchmark-pc.c
Created October 20, 2020 18:08
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 bensuperpc/e056d2634ee4aa1497068d8697f9bcce to your computer and use it in GitHub Desktop.
Save bensuperpc/e056d2634ee4aa1497068d8697f9bcce to your computer and use it in GitHub Desktop.
Benchmarking PC
// Integer and float benchmark for Win32 and Win64
// Results are below main(), line 91
#include <stdlib.h>
#include <stdio.h>
#ifdef _WIN32
#include <sys/timeb.h>
#else
#include <sys/time.h>
#endif
#include <time.h>
#define NBRS 1000000
double
mygettime(void) {
# ifdef _WIN32
struct _timeb tb;
_ftime(&tb);
return (double)tb.time + (0.001 * (double)tb.millitm);
# else
struct timeval tv;
if(gettimeofday(&tv, 0) < 0) {
perror("oops");
}
return (double)tv.tv_sec + (0.000001 * (double)tv.tv_usec);
# endif
}
template< typename Type >
void my_test(const char* name) {
volatile Type v = 0;
// Do not use constants or repeating values
// to avoid loop unroll optimizations.
// All values >0 to avoid division by 0
Type v0 = (Type)(rand() % 256)/32 + 1;
Type v1 = (Type)(rand() % 256)/32 + 1;
Type v2 = (Type)(rand() % 256)/32 + 1;
Type v3 = (Type)(rand() % 256)/32 + 1;
Type v4 = (Type)(rand() % 256)/32 + 1;
Type v5 = (Type)(rand() % 256)/32 + 1;
Type v6 = (Type)(rand() % 256)/32 + 1;
Type v7 = (Type)(rand() % 256)/32 + 1;
Type v8 = (Type)(rand() % 256)/32 + 1;
Type v9 = (Type)(rand() % 256)/32 + 1;
Type v10 = (Type)(rand() % 256)/32 + 1;
Type v11 = (Type)(rand() % 256)/32 + 1;
Type v12 = (Type)(rand() % 256)/32 + 1;
Type v13 = (Type)(rand() % 256)/32 + 1;
Type v14 = (Type)(rand() % 256)/32 + 1;
Type v15 = (Type)(rand() % 256)/32 + 1;
double t1 = mygettime();
for (size_t i = 0; i < NBRS; ++i) {
v += v0;
v += v2;
v += v4;
v += v6;
v += v8;
v += v10;
v += v12;
v += v14;
}
printf("%s add: %f MIPS\n", name, (((double)NBRS/(mygettime() - t1)) * 8.0f) / 1000000.0f);
t1 = mygettime();
for (size_t i = 0; i < NBRS; ++i) {
v -= v1;
v -= v3;
v -= v5;
v -= v7;
v -= v9;
v -= v11;
v -= v13;
v -= v15;
}
printf("%s sub: %f MIPS\n", name, (((double)NBRS/(mygettime() - t1)) * 8.0f) / 1000000.0f);
t1 = mygettime();
for (size_t i = 0; i < NBRS; ++i) {
v *= v0;
v *= v2;
v *= v4;
v *= v6;
v *= v8;
v *= v10;
v *= v12;
v *= v14;
}
printf("%s mul: %f MIPS\n", name, (((double)NBRS/(mygettime() - t1)) * 8.0f) / 1000000.0f);
t1 = mygettime();
for (size_t i = 0; i < NBRS; ++i) {
v /= v1;
v /= v3;
v /= v5;
v /= v7;
v /= v9;
v /= v11;
v /= v13;
v /= v15;
}
printf("%s div: %f MIPS\n", name, (((double)NBRS/(mygettime() - t1)) * 8.0f) / 1000000.0f);
}
int main() {
my_test< short >(" short");
my_test< int >(" int");
my_test< long >(" long");
my_test< long long >(" long long");
my_test< float >(" float");
my_test< double >(" double");
my_test<long double >("long double");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment