简体   繁体   English

C ++矢量性能非直观结果?

[英]c++ vector performance non intuitive result?

I am fiddling with the performance wizard in VS2010, the one that tests instrumentation (function call counts and timing.) 我对VS2010中的性能向导不满意,该向导用于测试检测(函数调用计数和计时)。

After learning about vectors in the C++ STL, I just decided to see what info I can get about performance of filling a vector with 1 million integers: 在学习了C ++ STL中的向量之后,我才决定看看我能获得关于用一百万个整数填充向量的性能的哪些信息:

#include <iostream>
#include <vector>

void generate_ints();

int main() {
  generate_ints();
  return 0;
}

void generate_ints() {
  typedef std::vector<int> Generator;
  typedef std::vector<int>::iterator iter;
  typedef std::vector<int>::size_type size;

  Generator generator;

  for (size i = 0; i != 1000000; ++i) {
    generator.push_back(i);
  }
}

What I get is: 2402.37 milliseconds of elapsed time for the above. 我得到的是:上面的经过时间为2402.37毫秒。 But I learnt that vectors have to resize themselves when they run out of capacity as they are contiguous in memory. 但是我了解到,向量在内存不足时必须重新调整自身大小,因为它们在内存中是连续的。 So I thought I'd get better performance by making one addition to the above which was: 因此,我认为通过对上述内容进行补充可以得到更好的性能:

generate.reserve(1000000);

However this doubles the execution time of the program to around 5000 milliseconds. 但是,这会使程序的执行时间增加一倍,达到大约5000毫秒。 Here is a screenshot of function calls, left side without the above line of code and right side with. 这是函数调用的屏幕截图,左侧没有上面的代码行,右侧没有。 I really don't understand this result and it doesn't make sense to me given what I learnt about how defining a vectors capacity if you know you will fill it with a ton is a good thing. 我真的不明白这个结果,而且鉴于我了解到如何定义矢量容量(如果您知道会用吨填充)的定义,这对我来说是没有意义的。 Specifying reserve basically doubled most of the function calls. 指定储备基本上使大多数函数调用加倍。

http://imagebin.org/179302 http://imagebin.org/179302

From the screenshot you posted, it looks like you're compiling without optimization, which invalidates any benchmarking you do. 从您发布的屏幕快照来看,您似乎是在没有优化的情况下进行编译,这会使您执行的所有基准测试无效。

You benchmark when you care about performance, and when you care about performance, you press the "go faster" button on the compiler, and enable optimizations. 您可以在关注性能时进行基准测试,而在关注性能时,可以按编译器上的“快速运行”按钮,并启用优化。

Telling the compiler to go slow, and then worrying that it's slower than expected is pointless. 告诉编译器变慢,然后担心它的速度比预期的慢是没有意义的。 I'm not sure why the code becomes slower when you insert a reserve call, but in debug builds, a lot of runtime checks are inserted to catch more errors, and it is quite possible that the reserve call causes more such checks to be performed, slowing down the code. 我不确定插入reserve调用时为什么代码变慢 ,但是在调试版本中,插入了许多运行时检查以捕获更多错误,并且reserve调用很有可能导致执行更多此类检查,减慢代码速度。

Enable optimizations and see what happens. 启用优化,看看会发生什么。 :) :)

Did you perform all your benchmarks under Release configuration? 您是否在发布配置下执行了所有基准测试?

Also, try running outside of profiler, in case you have hit some profiler-induced artifact (add manual time measurements to your code - you can use clock() for that). 另外,请尝试在Profiler之外运行,以防遇到一些Profiler引起的伪像(将手动时间测量值添加到代码中-您可以为此使用clock() )。

Also, did you by any chance make a typo and actually called resize instead of reserve ? 另外,您是否有机会打错字并实际上称为resize而不是reserve

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM