for (int i=1; i < num/3 + 1; ++i) {
Line l{"Line" + std::to_string(i),
Coord{i,i}, Coord{100*i,100*i}};
coll.push_back(l);
Circle c{"Circle" + std::to_string(i),
Coord{10*i,10*i}, i};
coll.push_back(c);
Polygon p{"Polygon" + std::to_string(i),
{Coord{i,i}, Coord{i, -i},
Coord{-i, -i}, Coord{-i, i}}};
coll.push_back(p);
}
return coll;
}
void cleanColl(GOColl& coll)
{
coll.clear();
}
long iterate(const GOColl& coll)
{
long numEmpty = 0;
for (const auto& go : coll) {
std::visit([&numEmpty] (const auto& obj) {
if (obj.getName().empty()) {
++numEmpty;
}
},
go);
}
return numEmpty;
}
long iterateDown(const GOColl& coll)
{
long sumPoints = 0;
for (const auto& go : coll) {
if (auto p = std::get_if<Polygon>(&go); p != nullptr) {
sumPoints += p->numPoints();
}
}
return sumPoints;
}
void covariant(const GOColl& coll)
{
for (const auto& go : coll) {
auto g = go;
assert((std::is_same_v<decltype(g), GOType>)); //... // e.g., std::visit(..., g); // no need to call delete
}
}
int main(int argc, const char** argv)
{
int num = 10000;
if (argc > 1) {
num = std::atoi(argv[1]);
}
std::cout << "***********************************************\n";
std::map<std::string, std::chrono::duration<double,std::milli>> data;
const int numLoops = 10;
int numMeasured = 0;
for (int i=0; i<numLoops; ++i) {
TrackNew::reset();
auto t1a = std::chrono::high_resolution_clock::now();
auto coll = initColl(num);
auto t1b = std::chrono::high_resolution_clock::now();
auto d1 = std::chrono::duration<double, std::milli>(t1b-t1a);
std::cout << "**** initColl(): " << d1.count() << " ms\n";
auto t2a = std::chrono::high_resolution_clock::now();
shuffleAndMod(coll);
auto t2b = std::chrono::high_resolution_clock::now();
auto d2 = std::chrono::duration<double, std::milli>(t2b-t2a);
std::cout << "**** shuffleAndMod(): " << d2.count() << " ms\n";
auto t3a = std::chrono::high_resolution_clock::now();
sort(coll);
auto t3b = std::chrono::high_resolution_clock::now();
auto d3 = std::chrono::duration<double, std::milli>(t3b-t3a);
std::cout << "**** sort(): " << d3.count() << " ms\n";
auto t4a = std::chrono::high_resolution_clock::now();
copy(coll);
auto t4b = std::chrono::high_resolution_clock::now();
auto d4 = std::chrono::duration<double, std::milli>(t4b-t4a);
std::cout << "**** copy() " << d4.count() << " ms\n";
auto t5a = std::chrono::high_resolution_clock::now();
auto numEmpty = iterate(coll);
auto t5b = std::chrono::high_resolution_clock::now();
auto d5 = std::chrono::duration<double, std::milli>(t5b-t5a);
std::cout << "**** iterate():): " << d5.count() << " ms\n";
std::cout << " " << numEmpty << " empty names\n";
auto t6a = std::chrono::high_resolution_clock::now();
auto numPoints = iterateDown(coll);
auto t6b = std::chrono::high_resolution_clock::now();
auto d6 = std::chrono::duration<double, std::milli>(t6b-t6a);
std::cout << "**** iterateDown(): " << d6.count() << " ms\n";
std::cout << " " << numPoints << " points\n";
auto t7a = std::chrono::high_resolution_clock::now();
covariant(coll);
auto t7b = std::chrono::high_resolution_clock::now();
auto d7 = std::chrono::duration<double, std::milli>(t7b-t7a);
std::cout << "**** covariant():): " << d7.count() << " ms\n";
auto t8a = std::chrono::high_resolution_clock::now();
cleanColl(coll);
auto t8b = std::chrono::high_resolution_clock::now();
auto d8 = std::chrono::duration<double, std::milli>(t8b-t8a);
std::cout << "**** cleanColl(): " << d8.count() << " ms\n";
auto d = std::chrono::duration<double, std::milli>(t8b-t1a);
std::cout << "---------- ";
TrackNew::status();