#include <vector>
#include <string>
#include <iostream>
#include <iomanip>
#include <chrono>
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());
}
};
int main(int argc, const char * argv[])
{
const int N = 10000000;
{
muTimer mu;
double sum = 0;
for(int i = 1; i <= N; ++i)
{
double x = 200/double(i);
sum += x;
}
mu.stop();
cout << mu.duration() << endl;
cout << sum << endl;
}
{
muTimer mu;
double sum = 0;
for(int i = 1; i <= N; ++i)
{
double * f = new double(i);
double x = 200/double(*f);
sum += x;
delete f;
}
mu.stop();
cout << mu.duration() << endl;
cout << sum << endl;
}
}