我正在考虑将C++用于性能关键应用程序。我认为C和C++都有可比的运行时间。但是我发现C++函数需要运行4次以上的可比的C代码片段。为什么此C++函数需要c函数4倍的时间
当我做了反汇编,我看到end(),++,!=都是作为函数调用实现的。是否有可能使他们(至少其中一些)内联?
这里是C++代码:
typedef struct pfx_s {
unsigned int start;
unsigned int end;
unsigned int count;
} pfx_t;
typedef std::list<pfx_t *> pfx_list_t;
int
eval_one_pkt (pfx_list_t *cfg, unsigned int ip_addr)
{
const_list_iter_t iter;
for (iter = cfg->begin(); iter != cfg->end(); iter++) {
if (((*iter)->start <= ip_addr) &&
((*iter)->end >= ip_addr)) {
(*iter)->count++;
return 1;
}
}
return 0;
}
而且这是等价的C代码:
int
eval_one_pkt (cfg_t *cfg, unsigned int ip_addr)
{
pfx_t *pfx;
TAILQ_FOREACH (pfx, &cfg->pfx_head, next) {
if ((pfx->start <= ip_addr) &&
(pfx->end >= ip_addr)) {
pfx->count++;
return 1;
}
}
return 0;
}
您是否优化过?它应该内联他们,当你打开优化。 –
C++会这样做,以允许运算符重载。一个优秀的优化者应该能够判断你是否实际上超载了他们并且不接电话。 –
您报告的统计信息似乎意味着您调试了代码的调试版本,可能是调试版本的库。这种测量几乎没有意义。 – AnT