#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <memory.h>
using namespace std;
class muTimer
{
using Clock = std::chrono::high_resolution_clock;
bool active = false;
Clock::duration duration_;
Clock::time_point start_ = Clock::now(), stop_ = Clock::now();
muTimer(const muTimer&) = delete;
muTimer& operator=(const muTimer&) = delete;
public:
using ns = std::chrono::nanoseconds;
using mks = std::chrono::microseconds;
using ms = std::chrono::milliseconds;
muTimer() { reset(); start(); }
~muTimer() = default;
muTimer& reset()
{
duration_ = std::chrono::nanoseconds(0);
active = false;
return *this;
}
muTimer& start()
{
if (!active)
{
start_ = Clock::now();
active = true;
}
return *this;
}
muTimer& stop()
{
if (active)
{
stop_ = Clock::now();
duration_ += stop_ - start_;
active = false;
}
return *this;
}
template<typename T = mks>
unsigned long long duration()
{
return static_cast<unsigned long long>
(std::chrono::duration_cast<T>(stop_-start_).count());
}
};
const int COUNT = 10000;
const int TRIES = 100000;
int main(int argc, const char * argv[])
{
volatile char * a = new char[COUNT];
volatile char * b = new char[COUNT];
for(int i = 0; i < COUNT; ++i)
a[i] = b[i] = rand() % 255;
{
int total = 0;
muTimer mu;
mu.start();
for(int i = 0; i < TRIES; ++i)
{
for(int j = 0; j < COUNT; ++j)
if (a[j] != b[j]) { ++total; break; }
}
cout << total << " " << mu.stop().duration() << endl;
}
{
int total = 0;
muTimer mu;
mu.start();
for(int i = 0; i < TRIES; ++i)
{
if (memcmp((void*)a,(void*)b,COUNT)) ++total;
}
cout << total << " " << mu.stop().duration() << endl;
}
}