Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created January 12, 2015 12:35
Show Gist options
  • Select an option

  • Save JossWhittle/7e1e9bb01f4e39346888 to your computer and use it in GitHub Desktop.

Select an option

Save JossWhittle/7e1e9bb01f4e39346888 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <omp.h>
#include <cassert>
using namespace std;
constexpr int T_SIZE = 1e8;
// Bitwise operator based function to check multiple of 9
bool isMulOf9(int n) {
// Base cases
if (n == 0 || n == 9)
return true;
if (n < 9)
return false;
// if the number is greater than 9, then do this
return isMulOf9((n >> 3) - (n & 7));
};
int main() {
bool r;
{ // Speed round - int mod
auto tik = omp_get_wtime();
for (int i = 0; i < T_SIZE; ++i) {
r = (i % 9) == 0;
}
auto tok = omp_get_wtime();
printf("int mod : %f\n", (tok - tik));
}
{ // Speed round - recursive mod
auto tik = omp_get_wtime();
for (int i = 0; i < T_SIZE; ++i) {
r = isMulOf9(i);
}
auto tok = omp_get_wtime();
printf("rec mod : %f\n", (tok - tik));
}
{ // Accuracy round
for (int i = 0; i < T_SIZE; ++i) {
assert(((i % 9) == 0) == isMulOf9(i));
}
printf("Equivilant Results\n");
}
system("pause");
return 0;
};
int mod : 0.327569
rec mod : 3.301001
Equivilant Results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment