简体   繁体   English

我的程序随着时间的推移而变慢,我不知道为什么。 Memory 泄漏?

[英]My program is slowing down over time and i have no idea why. Memory leak?

I'm working on a program that acts as a very simple voting system which reads numbers from a file, converts the numbers from the file to type unsigned int.我正在开发一个程序,它充当一个非常简单的投票系统,它从文件中读取数字,将文件中的数字转换为 unsigned int 类型。

Here is an example of what data in the txt file looks like:以下是 txt 文件中数据的示例:

1 34 2 50 23 12
1 30 5 17
5
30 2 3 22
23 45

Each line is a vote from one person and the numbers on each line are the person's candidate preferences, left being first preference, right being last preference.每行是一个人的投票,每行上的数字是该人的候选人偏好,左边是第一偏好,右边是最后偏好。

Once all the data is read from the file, it enters an infinite loop where every round (iteration) it calculates the remaining candidates (ie eliminates the candidate with the fewest votes).一旦从文件中读取了所有数据,它就会进入一个无限循环,其中每一轮(迭代)它都会计算剩余的候选者(即淘汰得票最少的候选者)。 The program exits with code 0 when a candidate with the majority votes have been found.当找到具有多数票的候选人时,程序以代码 0 退出。

My issue is that using the g++ compiler, at around round 40 or so the program starts to slow down and I'm assuming that this is because of a memory leak however I have no idea where in the program it could be leaking.我的问题是使用 g++ 编译器,大约在第 40 轮左右,程序开始变慢,我假设这是因为 memory 泄漏但是我不知道它可能在程序中的哪个位置泄漏。

This is what I get when debugging the program through Deleaker.这是我通过Deleaker调试程序时得到的。

Note: Thank you, everyone, for your help.注:谢谢大家的帮助。 However, as much as I don't want to, I am required to redact the code posted here due to certain reasons.但是,尽管我不想这样做,但由于某些原因,我需要编辑此处发布的代码。 I will not delete the post in case someone can find use in the answers in some way.我不会删除帖子,以防有人能以某种方式找到答案。 Hope you understand, thank you.希望你能理解,谢谢。

I am making some guesses here, but in terms of time complexity of the algorithm,我在这里做一些猜测,但就算法的时间复杂度而言,

while (p != vote_collection.end())  {
    //...
    if (p->spent()) {
        p = vote_collection.erase(p);
    }
 }

is problematic because vote_collection is a vector.是有问题的,因为vote_collection是一个向量。 Suppose N is that container's size, ie the number of votes.假设N是该容器的大小,即投票数。 When p->spent() is true (which happens more likely in later iterations), then you are going to erase p .p->spent()为真时(在以后的迭代中更有可能发生),那么您将擦除p Erasing an element from a vector has linear time complexity in N in the worst case (when erasing at the beginning, which you are likely to do as you iterate the vector from beginning to end.) Since this will be happening to many of he votes, this loop has quadratic time complexity in N .在最坏的情况下,从向量中擦除元素在N中具有线性时间复杂度(在开始擦除时,您可能会在从头到尾迭代向量时这样做。)因为这将发生在他的许多投票中,这个循环在N中具有二次时间复杂度。 You always want to try to avoid quadratic complexities if the input variable may be large.如果输入变量可能很大,您总是希望避免二次复杂性。

The reason for this being the case is that vectors are storing the elements continuously in memory.出现这种情况的原因是向量将元素连续存储在 memory 中。 When you erase an element, all the other elements after the erased one must be shifted one element to close up the gap.当您擦除一个元素时,擦除后的所有其他元素必须移动一个元素以缩小间隙。 This requires moving almost the whole vector when the erased element is close to the beginning.这需要在擦除元素接近开头时移动几乎整个向量。

Instead of using the current approach, you could simply leave all the spent votes in the vector and simply make sure that ranked_candidates skips spent votes.您可以简单地将所有已用票留在向量中,并确保ranked_candidates跳过已用票,而不是使用当前方法。

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

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