【发布时间】:2017-06-05 12:10:16
【问题描述】:
我有一些数据容器,并考虑将它们放入结构中(更容易将其提供给函数等)。 程序应该尽可能快。对结构中容器的访问是较慢还是完全相同?
// 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;
【问题讨论】:
-
嗯,你测量了吗?过早的优化是万恶之源:)
-
不会有区别。但是你为什么不自己尝试一下呢?
-
这取决于你如何使用它。现在不要关心过早的优化,首先要确保你有一个可读、可维护和工作的好程序。然后,如果性能不符合您测量、基准和分析的要求,以找到瓶颈,并专注于其中的前几个。
-
为了回答在您的机器上使用您的数据编译器会更快的问题,我需要您的编译器、您的机器、您的数据和 400 美元/小时。
-
您几乎肯定想在这些容器周围命名,即使它是一组
typedef std::vector<std::deque<int>> DomainRelevantName,但最好将它们包装在单独的类中以强制执行有用的不变量
标签: c++ performance struct stl containers