我遇到了std :: set的奇怪行为.
这是代码:
#include <cstdio>
#include <windows.h>
#include <stdlib.h>
#include <vector>
#include <set>
using namespace std;
int main(int argc, char *argv[])
{
set<int> b[100];
for (int o=0; o<10; o++)
{
int tt = GetTickCount();
for (int i=0; i<5000000; i++)
{
b[o].insert(i);
}
tt = GetTickCount() - tt;
b[o].clear();
printf("%d\n", tt);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在Windows XP上运行.
这是有趣的部分:第一次打印时间约为3500毫秒,而接下来的时间都超过9000毫秒!为什么会这样?
哦,这只发布在发布版本(-O2优化).
它不会发生在Linux上(在更改代码后进行编译).
还有一件事:当我使用英特尔VTune进行性能分析时运行它总是需要大约3000毫秒,所以它应该是这样的.
更新:这是一些新代码:
#include <cstdio>
#include <windows.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
const int count = 10000000;
int …Run Code Online (Sandbox Code Playgroud)