我有一些容器的数据,并考虑将它们放入一个结构(更容易给它的功能等)。该程序应尽可能快。访问结构体中的容器是慢还是完全一样?C++ STL容器的性能。更好的速度在结构或无结构?
// data without struct
double d1;
bool b1;
std::map<std::string, std::deque<double>> map1;
std::vector<std::deque<int>> a1;
std::vector<std::deque<int>> a2;
std::vector<std::vector<<std::deque<int>>> a3;
std::vector<std::vector<<std::deque<int>>> a4;
// ... and perhaps even more
VS
struct containerstruct
{
double d1;
bool b1;
std::map<std::string, std::deque<double>> map1;
std::vector<std::deque<int>> a1;
std::vector<std::deque<int>> a2;
std::vector<std::vector<<std::deque<int>>> a3;
std::vector<std::vector<<std::deque<int>>> a4;
// ... and perhaps even more
};
containerstruct data;
那么,你有没有测量?过早优化是所有邪恶的根源:) – Rakete1111
不会有任何区别。但你为什么不尝试一下呢? – DeiDei
这取决于你如何使用它。不要在乎现在过早的优化,首先要确保你有一个可读,可维护和可行的好程序。然后,如果性能不符合您衡量和测试的要求,并找出瓶颈,并专注于其中的前几名。 –