先前循环迭代对当前迭代执行时间的影响

Impact of the prior loop iteration on the execution time of the current iteration

本文关键字:迭代 影响 执行时间 循环      更新时间:2023-10-16

我正在尝试测量folly哈希图中并发插入的性能。此处提供了用于此类插入的程序的简化版本:

#include <folly/concurrency/ConcurrentHashMap.h>
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
const int kNumMutexLocks = 2003;
std::unique_ptr<std::mutex[]> mutices(new std::mutex[kNumMutexLocks]);
__inline__ void
concurrentInsertion(unsigned int threadId, unsigned int numInsertionsPerThread,
unsigned int numInsertions, unsigned int numUniqueKeys,
folly::ConcurrentHashMap<int, int> &follyMap) {
int base = threadId * numInsertionsPerThread;
for (int i = 0; i < numInsertionsPerThread; i++) {
int idx = base + i;
if (idx >= numInsertions)
break;
int val = idx;
int key = val % numUniqueKeys;
mutices[key % kNumMutexLocks].lock();
auto found = follyMap.find(key);
if (found != follyMap.end()) {
int oldVal = found->second;
if (oldVal < val) {
follyMap.assign(key, val);
}
} else {
follyMap.insert(key, val);
}
mutices[key % kNumMutexLocks].unlock();
}
}
void func(unsigned int numInsertions, float keyValRatio) {
const unsigned int numThreads = 12; // Simplified just for this post
unsigned int numUniqueKeys = numInsertions * keyValRatio;
unsigned int numInsertionsPerThread = ceil(numInsertions * 1.0 / numThreads);
std::vector<std::thread> insertionThreads;
insertionThreads.reserve(numThreads);
folly::ConcurrentHashMap<int, int> follyMap;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < numThreads; i++) {
insertionThreads.emplace_back(std::thread([&, i] {
concurrentInsertion(i, numInsertionsPerThread, numInsertions,
numUniqueKeys, follyMap);
}));
}
for (int i = 0; i < numThreads; i++) {
insertionThreads[i].join();
}
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
float insertionTimeMs =
std::chrono::duration<double, std::milli>(diff).count();
std::cout << "i: " << numInsertions << "tj: " << keyValRatio
<< "ttime: " << insertionTimeMs << std::endl;
}
int main() {
std::vector<float> js = {0.5, 0.25};
for (auto j : js) {
std::cout << "-------------" << std::endl;
for (int i = 2048; i < 4194304 * 8; i *= 2) {
func(i, j);
}
}  
}

问题是在main中使用此循环会突然增加func函数中的测量时间。也就是说,如果我直接从main调用函数而不进行任何循环(如下所示),则在某些情况下的测量时间突然缩短了100倍以上。

int main() {
func(2048, 0.25); // ~ 100X faster now that the loop is gone.
}

可能的原因

  1. 我在构建 hasmap 时分配了大量内存。我相信当我在循环中运行代码时,当循环的第二次迭代正在执行时,计算机正忙于为第一次迭代释放内存。因此,程序变得慢得多。如果是这种情况,如果有人可以提出更改,我将不胜感激,我可以在循环中获得相同的结果。

更多详情

请注意,如果我在 main 中展开循环,我会遇到同样的问题。也就是说,以下程序具有相同的问题:

int main() {
performComputation(input A);
...
performComputation(input Z);
} 

示例输出

第一个程序的输出如下所示:

i: 2048     j: 0.5  time: 1.39932
...
i: 16777216 j: 0.5  time: 3704.33
------------- 
i: 2048     j: 0.25 time: 277.427 <= sudden increase in execution time
i: 4096     j: 0.25 time: 157.236
i: 8192     j: 0.25 time: 50.7963
i: 16384    j: 0.25 time: 133.151
i: 32768    j: 0.25 time: 8.75953
...
i: 2048     j: 0.25 time: 162.663

在主func中单独运行,产量i=2048j=0.25

i: 2048     j: 0.25 time: 1.01

任何评论/见解都非常感谢。

如果说是内存分配减慢了速度,而performComputation(input)之前的内存内容无关紧要,您可以重用分配的内存块。

int performComputation(input, std::vector<char>& memory) { 
/* Note: memory will need to be passed by reference*/
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < numThreads; i++) {
t.emplace_back(std::thread([&, i] {
func(...); // Random access to memory
}));
}
for (int i = 0; i < numThreads; i++) {
t[i].join();
}
auto end = std::chrono::steady_clock::now();
float time = std::chrono::duration<double, std::milli>(end - start).count();
}
int main() {
// A. Allocate ~1GB memory here
std::vector<char> memory(1028 * 1028 * 1028) //is that 1 gig?
for (input: inputs)
performComputation(input, memory);
}

我对确切的细节不太有信心,但在我看来,这是构建地图时内存分配的结果。我使用普通unordered_map和单个mutex复制了您所看到的行为,并使地图对象完全修复funcstatic。(实际上,现在第一次稍微慢一点,因为还没有为地图分配内存,然后每次后续运行都更快且一致的时间。

我不确定为什么这会有所不同,因为地图已被破坏,内存应该被释放。出于某种原因,地图释放的内存似乎不会在后续调用func时重复使用。也许还有其他人比我更了解这一点。

编辑:减少最小,可重现的示例和输出

void func(int num_insertions)
{
const auto start = std::chrono::steady_clock::now();
std::unordered_map<int, int> map;
for (int i = 0; i < num_insertions; ++i)
{
map.emplace(i, i);
}
const auto end = std::chrono::steady_clock::now();
const auto diff = end - start;
const auto time = std::chrono::duration<double, std::milli>(diff).count();
std::cout << "i: " << num_insertions << "ttime: " << time << "n";
}
int main()
{
func(2048);
func(16777216);
func(2048);
}

使用非静态映射:

i: 2048 time: 0.6035
i: 16777216     time: 4629.03
i: 2048 time: 124.44

使用静态地图:

i: 2048 time: 0.6524
i: 16777216     time: 4828.6
i: 2048 time: 0.3802

另一个编辑:还应该提到静态版本还需要在最后调用map.clear(),尽管这与插入的性能问题并不真正相关。

测量挂钟时间时使用平均值!

您正在测量挂钟时间。在这方面,看到的实际时间跳跃有点小范围,理论上可能导致操作系统延迟或其他处理,或者由于程序引起的线程管理(例如清理)可能会更糟(请注意,这可能会因平台/系统而异,请记住上下文切换很容易花费 ~10-15ms) 有太多的参数在起作用,无法确定。 当使用挂钟进行测量时,通常的做法是在数百或数千次的循环中取平均值以取峰值/等...考虑

使用探查器

学习使用探查器 - 探查器可以帮助您快速查看程序实际花费的时间,并一次又一次地节省宝贵的时间。