C++ (Qt)#include <set>#include <chrono>#include <iostream> using Time = std::chrono::high_resolution_clock;using ms = std::chrono::milliseconds; int main(){ std::set<int> initialSet; for (int i = 0; i < 5000000; ++i) { initialSet.insert(i); } std::set<int> setFilledWithForInsert; { const auto start = Time::now(); for (const int value: initialSet) { setFilledWithForInsert.insert(value); } std::cout << "Insert: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n"; } std::set<int> setFilledWithForEmplaceHint; { const auto start = Time::now(); auto it = setFilledWithForEmplaceHint.cend(); for (const int value: initialSet) { it = setFilledWithForEmplaceHint.emplace_hint(it, value); } std::cout << "EmplaceHint: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n"; } std::set<int> setFilledWithForInsertHint; { const auto start = std::chrono::system_clock::now(); auto it = setFilledWithForEmplaceHint.cend(); for (const int value: initialSet) { it = setFilledWithForInsertHint.insert(it, value); } std::cout << "InsertHint: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n"; } std::set<int> setFilledWithIterators; { const auto start = std::chrono::system_clock::now(); setFilledWithIterators.insert(initialSet.cbegin(), initialSet.cend()); std::cout << "InsertIterators: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n"; } return 0;}
C++ (Qt) std::set<int> initialSet; for (int i = 0; i < 5000000; ++i) { initialSet.insert(i); }
C++ (Qt)template <typename T>std::set<T> &subtract (std::set<T> &initSet, const std::set<T> &otherSet) { auto it = initSet.cbegin(); auto end = initSet.cend(); auto otherIt = otherSet.cbegin(); auto otherEnd = otherSet.cend(); while (it != end) { const auto &value = *it; while (otherIt != otherEnd) { if (*otherIt >= value) { break; } ++otherIt; } if (otherIt == otherEnd) { break; } if (*otherIt == value) { it = initSet.erase(it); } else { ++it; } } return initSet;}
C++ (Qt)#include <algorithm>#include <chrono>#include <iostream>#include <set> using Time = std::chrono::high_resolution_clock;using ms = std::chrono::milliseconds; template <typename T>std::set<T> &subtract (std::set<T> &initSet, const std::set<T> &otherSet) { auto it = initSet.cbegin(); auto end = initSet.cend(); auto otherIt = otherSet.cbegin(); auto otherEnd = otherSet.cend(); while (it != end) { const auto &value = *it; while (otherIt != otherEnd) { if (*otherIt >= value) { break; } ++otherIt; } if (otherIt == otherEnd) { break; } if (*otherIt == value) { it = initSet.erase(it); } else { ++it; } } return initSet;} int main(){ std::set<int> initialSet; auto it = initialSet.end(); for (int i = 0; i < 5000000; ++i) { it = initialSet.emplace_hint(it, i); } std::set<int> diffSet; it = diffSet.end(); for (int i = 0; i < 5000000; i += 2) { it = diffSet.emplace_hint(it, i); } auto start = Time::now(); std::set<int> setSubtractedWithStl; { std::set<int> initialSetCopy = initialSet; start = Time::now(); std::set_difference(initialSetCopy.cbegin(), initialSetCopy.cend(), diffSet.cbegin(), diffSet.cend(), std::inserter(setSubtractedWithStl, setSubtractedWithStl.end())); } std::cout << "Stl + destructor: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << '\n'; start = Time::now(); subtract(initialSet, diffSet); std::cout << "Heaven: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << '\n'; const bool hasEqualSize = initialSet.size() == setSubtractedWithStl.size(); bool equal = false; if (hasEqualSize) { equal = std::equal(initialSet.cbegin(), initialSet.cend(), setSubtractedWithStl.cbegin()); } std::cout << "Sets are equal: " << std::boolalpha << equal << '\n'; return 0;}
C++ (Qt)template <class T>void subtract( std::set<T> & set1, const std::set<T> & set2 ){ typedef typename std::set<T>::iterator TIter; TIter it2 = set2.begin(); while (it2 != set2.end()) { TIter it1 = set1.find(*it2); if (it1 != set1.end()) set1.erase(it1); ++it2; }}
C++ (Qt)template<class T>void RemoveElems( std::vector<T> & vec ){ size_t place = 0; for (size_t i = 0; i < vec.size(); ++i) { if (IsElementDeleted(vec[i])) continue; if (i != place) vec[place] = vec[i]; ++place; } vec.resize(place);}