Created
August 11, 2017 23:01
-
-
Save theodoregoetz/39e779bb5fa1fd875c6b80e0b1b0e138 to your computer and use it in GitHub Desktop.
anding: logical vs bitwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <chrono> | |
namespace user { | |
namespace chrono = std::chrono; | |
using std::chrono::duration_cast; | |
using std::chrono::steady_clock; | |
template <typename T = chrono::milliseconds, typename F> | |
static typename T::rep timeit(F fn) | |
{ | |
auto start = steady_clock::now(); | |
fn(); | |
auto duration = duration_cast<T>(steady_clock::now() - start); | |
return duration.count(); | |
} | |
} // namespace user | |
#include <random> | |
#include <vector> | |
namespace user { | |
template <typename T> | |
std::vector<T> uniform(T min, T max, size_t N, int seed) | |
{ | |
std::default_random_engine engine{seed}; | |
std::uniform_real_distribution<> distribution{min, max}; | |
std::vector<T> ret(N); | |
for (auto& i: ret) | |
{ | |
i = static_cast<T>(distribution(engine)); | |
} | |
return ret; | |
} | |
} | |
using namespace std; | |
void f(vector<float>& v) | |
{ | |
int count = 0; | |
for (int i=1; i<v.size(); i++) | |
{ | |
if ((v[i] > 50) && (v[i-1] > 50)) | |
{ | |
count++; | |
} | |
} | |
} | |
void g(vector<float>& v) | |
{ | |
int count = 0; | |
for (int i=1; i<v.size(); i++) | |
{ | |
if ((v[i] > 50) & (v[i-1] > 50)) | |
{ | |
count++; | |
} | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
int seed = atoi(argv[1]); | |
auto sample = user::uniform<float>(0,100,1000000,seed); | |
size_t t; | |
t = 0; | |
for (int i=0; i<10; i++) | |
{ | |
t += user::timeit<chrono::microseconds>([&]{ g(sample); }); | |
} | |
cout << "bitwise and: " << double(t) / 10. << endl; | |
t = 0; | |
for (int i=0; i<10; i++) | |
{ | |
t += user::timeit<chrono::microseconds>([&]{ f(sample); }); | |
} | |
cout << "logical and: " << double(t) / 10. << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment