27 std::cout << msg << std::endl;
31 std::this_thread::sleep_for(std::chrono::milliseconds(5));
43void benchmark(std::string name, std::function<
void()> function,
int n) {
44 std::chrono::time_point<std::chrono::high_resolution_clock> pstr, pend;
46 pstr = std::chrono::high_resolution_clock::now();
47 for (
int i = 0; i < n; i++) {function();}
48 pend = std::chrono::high_resolution_clock::now();
50 auto mstr = std::chrono::time_point_cast<std::chrono::microseconds>(pstr).time_since_epoch().count();
51 auto mend = std::chrono::time_point_cast<std::chrono::microseconds>(pend).time_since_epoch().count();
52 auto nstr = std::chrono::time_point_cast<std::chrono::nanoseconds>(pstr).time_since_epoch().count();
53 auto nend = std::chrono::time_point_cast<std::chrono::nanoseconds>(pend).time_since_epoch().count();
55 print(
"Benchmark " + name +
": " + std::to_string(mend - mstr) +
"ms - " + std::to_string(nend - nstr) +
"ns");
67 std::thread count([]() {
benchmark(
"Count", []() {
set::count({0, 0, 1, 0, 0}, 0);}, 1000);});
68 std::thread unique([]() {
benchmark(
"Unique", []() {
set::unique(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
69 std::thread sort([]() {
benchmark(
"Sort", []() {
set::sort(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
70 std::thread rsort([]() {
benchmark(
"Reverse Sort", []() {
set::rsort(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
71 std::thread reverse([]() {
benchmark(
"Reverse", []() {
set::reverse(std::vector<int>({0, 0, 1, 0, 0}));}, 1000);});
72 std::thread sunion([]() {
benchmark(
"Union", []() {
set::sunion(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
73 std::thread sintersection([]() {
benchmark(
"Intersection", []() {
set::intersection(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
74 std::thread diff([]() {
benchmark(
"Difference", []() {
set::diff(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
75 std::thread sdiff([]() {
benchmark(
"Symmetric difference", []() {
set::sdiff(std::vector<int>({1, 2, 3}), std::vector<int>({3, 4, 5}));}, 1000);});
void benchmark(std::string name, std::function< void()> function, int n)
Manages the benchmarking of a passed function.
std::mutex out
The std::mutex object that locks the std::cout resource.
void print(std::string msg)
Manages the std::cout resource between simultaneously running threads.
int main()
Main entry point that runs a benchmark for every library method.
std::vector< T > & diff(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory difference of two sets.
bool contains(const std::vector< T > &k, const T &kk) noexcept
Checks if an element is present in the set.
std::vector< T > & sdiff(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory symmetric difference of two sets.
std::vector< T > & intersection(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory intersection of two sets.
std::vector< T > & rsort(std::vector< T > &c, const std::vector< T > &k)
Sorts a set in descending order.
int count(const std::vector< T > &k, const T &kk) noexcept
Counts the number of times an element is present in the set.
std::vector< T > & unique(std::vector< T > &c, const std::vector< T > &k)
Removes duplicates from a set.
std::vector< T > & sort(std::vector< T > &c, const std::vector< T > &k)
Sorts a set in ascending order.
std::vector< T > & reverse(std::vector< T > &c, const std::vector< T > &k)
Reverses the passed set.
std::vector< T > & sunion(std::vector< T > &c, const std::vector< T > &k, const std::vector< T > &kk)
Obtains the set theory union of two sets.
This is the main header file of the cset library. It contains all the functions and their implementat...