Skip to content

Instantly share code, notes, and snippets.

@jiunbae
Created May 22, 2017 01:04
Show Gist options
  • Save jiunbae/4b3209ac54d7f186e18041c37656eadd to your computer and use it in GitHub Desktop.
Save jiunbae/4b3209ac54d7f186e18041c37656eadd to your computer and use it in GitHub Desktop.
// FILE: main.cc
#include <vector>
#include <string>
#include <algorithm>
#include "utility.h"
using namespace std;
#define VECTOR_SIZE 1000000
#define SWAP_SIZE 100000
void process(time_point<steady_clock> start) {
vector<int> v;
INFO("process start!");
for (auto i = 0; i < VECTOR_SIZE; i++) {
v.push_back(random());
}
INFO(fstring("vector generated! %d", VECTOR_SIZE));
for (auto i = 0; i < SWAP_SIZE; i++) {
auto lposition = random();
auto rposition = random();
iter_swap(v.begin() + lposition, v.begin() + rposition);
}
INFO(fstring("vector swaped! %d", SWAP_SIZE));
sort(v.begin(), v.end());
INFO("vector sorted!");
}
int main(int argc, char * argv[]) {
auto start = begin();
INFO("program start!")
process(start);
INFO("program done!")
return 0;
}
// FILE: utility.h
#pragma once
#include <iostream>
#include <random>
#include <memory>
#include <chrono>
using namespace std;
using namespace chrono;
#define RANDOM_LOW 0
#define RANDOM_HGIH 1000000
#define TIMESTAMP(x) cout.width(8); cout << done(x) << " ms";
#define INFO(msg) TIMESTAMP(start); cout<< "\t" << msg << endl;
int random();
time_point<steady_clock> begin();
long long done(time_point<steady_clock>);
template <typename T>
T process_arg(T value) noexcept {
return value;
}
template <typename T>
T const * process_arg(basic_string<T> const & value) noexcept {
return value.c_str();
}
template<typename ... Args>
string fstring(const string& format, Args const & ... args)
{
const auto fmt = format.c_str();
const size_t size = snprintf(nullptr, 0, fmt, process_arg(args) ...) + 1;
auto buf = make_unique<char[]>(size);
snprintf(buf.get(), size, fmt, process_arg(args) ...);
auto res = string(buf.get(), buf.get() + size - 1);
return res;
}
// FILE: utility.cc
#include "utility.h"
random_device rd;
mt19937 rng(rd());
uniform_int_distribution<int> uniform(RANDOM_LOW, RANDOM_HGIH);
int random()
{
return uniform(rng);
}
time_point<steady_clock> begin()
{
return steady_clock::now();
}
long long done(time_point<steady_clock> start)
{
auto now = begin();
auto elapsed = duration_cast<milliseconds>(now - start);
return elapsed.count();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment