Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------------------------------
- main.cpp
- -----------------------------------------------------------------------
- #include <chrono>
- #include <cstdlib>
- #include <iostream>
- #include <vector>
- #include "log_duration.h"
- using namespace std;
- vector<int> ReverseVector(const vector<int>& source_vector) {
- vector<int> res;
- for (int i : source_vector) {
- res.insert(res.begin(), i);
- }
- return res;
- }
- int CountPops(const vector<int>& source_vector, int begin, int end) {
- int res = 0;
- for (int i = begin; i < end; ++i) {
- if (source_vector[i]) {
- ++res;
- }
- }
- return res;
- }
- void AppendRandom(vector<int>& v, int n) {
- for (int i = 0; i < n; ++i) {
- v.push_back(rand() % 2);
- }
- }
- void Operate() {
- {
- LOG_DURATION("Total"s);
- vector<int> random_bits;
- static const int N = 1 << 17;
- {
- LOG_DURATION("Append random"s);
- AppendRandom(random_bits, N);
- }
- vector<int> reversed_bits;
- {
- LOG_DURATION("Reverse"s);
- reversed_bits = ReverseVector(random_bits);
- }
- {
- LOG_DURATION("Counting"s);
- for (int i = 1, step = 1; i <= N; i += step, step *= 2) {
- double rate = CountPops(reversed_bits, 0, i) * 100. / i;
- cout << "After "s << i << " bits we found "s << rate << "% pops"s << endl;
- }
- }
- }
- }
- int main() {
- Operate();
- }
- -----------------------------------------------------------------------
- log_duration.h
- -----------------------------------------------------------------------
- #pragma once
- #include <chrono>
- #include <iostream>
- #define PROFILE_CONCAT_INTERNAL(X, Y) X ## Y
- #define PROFILE_CONCAT(X, Y) PROFILE_CONCAT_INTERNAL(X, Y)
- #define UNIQUE_VAR_NAME_PROFILE PROFILE_CONCAT(profileGuard, __LINE__)
- #define LOG_DURATION(x) LogDuration UNIQUE_VAR_NAME_PROFILE(x)
- class LogDuration {
- public:
- using Clock = std::chrono::steady_clock;
- std::string operation_name;
- LogDuration(std::string operation_name_) : operation_name(operation_name_){}
- ~LogDuration() {
- using namespace std::chrono;
- using namespace std::literals;
- const auto end_time = Clock::now();
- const auto dur = end_time - start_time_;
- std::cerr << operation_name << ": "s << duration_cast<milliseconds>(dur).count() << " ms"s << std::endl;
- }
- private:
- const Clock::time_point start_time_ = Clock::now();
- };
Advertisement
Add Comment
Please, Sign In to add comment