Skip to content

Instantly share code, notes, and snippets.

@RagnarGrootKoerkamp
Last active November 21, 2015 20:28
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 RagnarGrootKoerkamp/ed876916d14d097973f2 to your computer and use it in GitHub Desktop.
Save RagnarGrootKoerkamp/ed876916d14d097973f2 to your computer and use it in GitHub Desktop.
Timing int vs long long
#include <chrono>
#include <iostream>
using namespace std;
using namespace chrono;
using ll = long long;
using ull = unsigned long long;
auto now(){ return high_resolution_clock::now(); }
int main(){
auto t1 = now();
{
int k = 15;
int N = 30;
int mask = (1 << k) - 1;
while (!(mask & 1 << N)) {
int lo = mask & ~(mask - 1);
int lz = (mask + lo) & ~mask;
mask |= lz;
mask &= ~(lz - 1);
mask |= (lz / lo / 2) - 1;
}
}
auto t2 = now();
{
ll k = 15;
ll N = 30;
ll mask = (1ll << k) - 1ll;
while (!(mask & 1ll << N)) {
ll lo = mask & ~(mask - 1ll);
ll lz = (mask + lo) & ~mask;
mask |= lz;
mask &= ~(lz - 1ll);
mask |= (lz / lo / 2ll) - 1ll;
}
}
auto t3 = now();
{
unsigned k = 15;
unsigned N = 30;
unsigned mask = (1u << k) - 1u;
while (!(mask & 1u << N)) {
unsigned lo = mask & ~(mask - 1u);
unsigned lz = (mask + lo) & ~mask;
mask |= lz;
mask &= ~(lz - 1u);
mask |= (lz / lo / 2u) - 1u;
}
}
auto t4 = now();
{
ull k = 15;
ull N = 30;
ull mask = (1ull << k) - 1ull;
while (!(mask & 1ull << N)) {
ull lo = mask & ~(mask - 1ull);
ull lz = (mask + lo) & ~mask;
mask |= lz;
mask &= ~(lz - 1ull);
mask |= (lz / lo / 2ull) - 1ull;
}
}
auto t5 = now();
cout << (t2-t1).count() << "\t(int)" << endl;
cout << (t3-t2).count() << "\t(long long)" << endl;
cout << (t4-t3).count() << "\t(unsigned)" << endl;
cout << (t5-t4).count() << "\t(unsigned long long)" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment