这里我有800个派生类Base
和8000000个这些类型的对象列表,可以是任何顺序。目标是尽可能有效地将列表分成800种类型。在这里,我写了两个函数来做到这一点。第一个假定为O(M * logN)时间,其中M是列表的大小,N =具体派生类的数量Base
,第二个假定为O(M)时间。但是当我计算输出时,第二个显然不比第一个快log800倍。我在这里弄错时间复杂吗?更好的是,是否有一个更快的功能,使整个比较成为一个没有实际意义的点?
#include <iostream>
#include <list>
#include <unordered_map>
#include <array>
#include <ctime>
class Base {
public:
virtual std::size_t ID() const = 0;
};
template <std::size_t N> class Derived : public Base {
virtual std::size_t ID() const override {return N;}
};
const std::size_t NumDerivedTypes = 800;
template <typename Iterator>
std::unordered_map<std::size_t, std::list<typename Iterator::value_type>> separateWithMap (Iterator first, Iterator last) {
std::unordered_map<std::size_t, std::list<typename Iterator::value_type>> map;
while (first != last) {
const auto it = map.find ((*first)->ID());
if (it != map.end()) {
it->second.emplace_back(*first);
}
else {
std::list<typename Iterator::value_type> newGroup = {*first};
map.emplace ((*first)->ID(), newGroup);
}
first++;
}
return map;
}
template <typename Iterator>
std::array<std::list<typename Iterator::value_type>, NumDerivedTypes> separateWithArray (Iterator first, Iterator last) {
std::array<std::list<typename Iterator::value_type>, NumDerivedTypes> array;
while (first != last) {
array[(*first)->ID()].emplace_back(*first);
++first;
}
return array;
}
// ------------------------------- Testing -------------------------------
template <std::size_t N>
void build (std::list<Base*>& weapons) {
weapons.emplace_back(new Derived<N>);
build<N+1>(weapons);
}
template <>
void build<NumDerivedTypes> (std::list<Base*>&) {} // End of recursion.
struct Timer {
const std::clock_t begin = std::clock();
~Timer() {
auto end = std::clock();
std::cout << double(end - begin) / CLOCKS_PER_SEC << " seconds.\n";
};
};
int main() {
// M = scrambled.size(), N = number of concrete derived classes of Base.
std::list<Base*> scrambled;
for (std::size_t i = 0; i < 10000; i++)
build<0>(scrambled); // Assume 'scrambled' has many, many elements in some unknown order.
std::cout << "scrambled.size() = " << scrambled.size() << '\n'; // 8000000
{
std::cout << "\nseparateWithMap started:\n"; // O(M*logN) time
Timer timer;
const std::unordered_map<std::size_t, std::list<Base*>> separated = separateWithMap (scrambled.begin(), scrambled.end());
std::cout << "separateWithMap ended:\n";
}
{
std::cout << "\nseparateWithArray started:\n"; // O(M) time
Timer timer;
const std::array<std::list<Base*>, NumDerivedTypes> partitioned = separateWithArray (scrambled.begin(), scrambled.end());
std::cout << "separateWithArray ended:\n";
}
}
输出:
scrambled.size() = 8000000
separateWithMap started.
separateWithMap ended.
30.318 seconds.
separateWithArray started.
separateWithArray ended.
22.869 seconds.
顺便说一下,两个函数都成功地将对象分成各自的类型(已测试),但出于显而易见的原因,我不会在输出中显示它们。