- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在阅读 Nicolai M.Josuttis 撰写的“The C++ STL. A Tutorial and References”一书,在专门介绍 STL 算法的一章中,作者陈述如下:如果你调用 remove()列表的元素,算法不知道它正在对列表进行操作,因此会执行它的操作适用于任何容器:通过更改元素的值重新排序元素。例如,如果算法删除第一个元素后,所有后续元素都将分配给它们之前的元素。这行为与列表的主要优点相矛盾:通过插入、移动和删除元素的能力修改链接而不是值。为了避免性能不佳,列表为所有操作算法提供了特殊的成员函数。你应该总是喜欢他们。此外,这些成员函数确实删除了“已删除”的元素,如本例所示:
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
// remove all elements with value 3 (poor performance)
coll.erase (remove(coll.begin(),coll.end(),
3),
coll.end());
// remove all elements with value 4 (good performance)
coll.remove (4);
}
当然,这似乎足以让我进一步考虑,但无论如何,我决定在我的 PC 上查看运行类似代码的结果,尤其是在 MSVC 2013 环境中。这是我即兴创作的代码:
int main()
{
srand(time(nullptr));
list<int>my_list1;
list<int>my_list2;
int x = 2000 * 2000;
for (auto i = 0; i < x; ++i)
{
auto random = rand() % 10;
my_list1.push_back(random);
my_list1.push_front(random);
}
list<int>my_list2(my_list1);
auto started1 = std::chrono::high_resolution_clock::now();
my_list1.remove(5);
auto done1 = std::chrono::high_resolution_clock::now();
cout << "Execution time while using member function remove: " << chrono::duration_cast<chrono::milliseconds>(done1 - started1).count();
cout << endl << endl;
auto started2 = std::chrono::high_resolution_clock::now();
my_list2.erase(remove(my_list2.begin(), my_list2.end(),5), my_list2.end());
auto done2 = std::chrono::high_resolution_clock::now();
cout << "Execution time while using generic algorithm remove: " << chrono::duration_cast<chrono::milliseconds>(done2 - started2).count();
cout << endl << endl;
}
当看到下面的输出时我很惊讶:
Execution time while using member function remove: 10773
Execution time while using generic algorithm remove: 7459
您能否解释一下这种矛盾行为的原因是什么?
最佳答案
这是一个缓存问题。大多数性能问题都是缓存问题。我们总是认为算法是首先要看的。但是,如果您故意强制编译器在一次运行中使用来自不同位置的内存,而在下一次运行中使用所有来自下一个位置的内存,您将看到缓存问题。
通过在构建原始列表时注释掉 push_back
或 push_front
,我强制编译器创建代码以在 中构建具有连续内存元素的列表my_list1
.
my_list2
始终位于连续内存中,因为它是在单个拷贝中分配的。
运行输出:
Execution time while using member function remove: 121
Execution time while using generic algorithm remove: 125
Process finished with exit code 0
这是我的代码,其中一个推送被注释掉了。
#include <list>
#include <algorithm>
#include <chrono>
#include <iostream>
using namespace std;
int main()
{
srand(time(nullptr));
list<int>my_list1;
// list<int>my_list2;
int x = 2000 * 2000;
for (auto i = 0; i < x; ++i)
{
auto random = rand() % 10;
// my_list1.push_back(random); // avoid pushing to front and back to avoid cache misses.
my_list1.push_front(random);
}
list<int>my_list2(my_list1);
auto started1 = std::chrono::high_resolution_clock::now();
my_list1.remove(5);
auto done1 = std::chrono::high_resolution_clock::now();
cout << "Execution time while using member function remove: " << chrono::duration_cast<chrono::milliseconds>(done1 - started1).count();
cout << endl << endl;
auto started2 = std::chrono::high_resolution_clock::now();
my_list2.erase(remove(my_list2.begin(), my_list2.end(),5), my_list2.end());
auto done2 = std::chrono::high_resolution_clock::now();
cout << "Execution time while using generic algorithm erase: " << chrono::duration_cast<chrono::milliseconds>(done2 - started2).count();
cout << endl << endl;
}
通过增加元素的数量并颠倒调用顺序以使删除先发生然后删除发生,删除需要更长的时间。同样,这更多的是关于缓存,而不是关于算法或正在完成的工作量。如果您正在运行另一个会弄脏缓存、检查互联网或移动鼠标的程序,则您的 32 KB L1 缓存将被弄脏,并且该运行的性能会下降。
关于c++ - 链表的通用函数 remove() 与成员函数 remove(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53083911/
我正在使用 .remove() 方法删除一个 html 元素,同时对于这个元素,我有一个事件处理程序,但它没有被触发。为什么会这样呢?这是jsFiddle和代码:HTML Delete I'm goi
所以我尝试从另一篇文章中编写此代码: while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
我正在阅读 Nicolai M.Josuttis 撰写的“The C++ STL. A Tutorial and References”一书,在专门介绍 STL 算法的一章中,作者陈述如下:如果你调用
是否有一种简单的机制来确定 DownloadManager remove() 何时完成,因为它看起来是部分异步的。该函数几乎立即返回下载表中已删除的条目计数,但实际的文件系统管理似乎被插入了某个后台线
我愿意: getActionBarToolbar().removeView(logoImage); getActionBarToolbar().addView(logoImage, lp); 我得到:
我有类(class)评论一对多关系。在类(class)表中有 id 和 title 列。在 Review 表中,有 id、comment 和 course_id,其中“course_id”作为指向类(
我在 stackoverflow 上阅读了不同的答案,了解如何销毁 wigdet/jQueryObject 并取消绑定(bind)其上的所有事件。 这就是我的想法。 $('选择器').remove()
我有一个由一个线程填充的 byte[] 列表,然后我有另一个线程正在从该列表中读取并通过网络发送项目。 每次我读取线程 2 中的项目时,我都想将其从内存中清除。但是因为我正在使用线程,如果我使用 .r
就算法而言,从连续数组中删除一组元素可以分两部分有效地完成。 将所有不删除的元素移到数组的前面。 将数组标记得更小。 这可以在 C++ 中使用 erase-remove 习惯用法来完成。 vector
我尝试删除包含在 map 中渲染的制造商的 View 。当我单击按钮时,事件 click .ver 被激活,但没有任何反应,并且我收到以下错误:Uncaught TypeError: undefine
场景: 使用 jQuery 2.0.1 构建的应用程序。 您的团队更喜欢原生 JavaScript。 选项有jQuery .remove()和 ChildNode.remove() . 您需要删除节点
最初我有一个像这样的删除功能: function ViewWorkflowDetail(btn, workflowId) { $("#workflowDetailPanel").remov
我正在编写 C++ 代码来解决 Leetcode 中的这个问题:https://leetcode.com/problems/remove-element/ Given an array nums an
根据太阳, "Iterator.remove is the only safe way to modify a collection during iteration; the behavior is
众所周知,从 std::vector 中完全删除所需项的一种好方法是 erase-remove idiom . 如以上链接中所述(截至本文发布日期),在代码中,erase-remove 习惯用法如下所
我在 HashSet 上调用 Iterator.remove() 时遇到问题。 我有一组带有时间戳的对象。在将新项目添加到集合之前,我会遍历集合,识别该数据对象的旧版本并将其删除(在添加新对象之前)。
这段代码: Collection col = new ArrayList(); col.add("a"); col.add("b"); col.add("c");
我试图通过在下面输入来卸载 conda 环境基础, conda env remove -n base 正如我所建议的那样,我尝试通过使用来停用基地 conda deactivate base 我再次尝
我已经对我的 IOS 应用程序进行了质量扫描分析。我收到以下警告: The binary has Runpath Search Path (@rpath) set. In certain cases
这个问题已经有答案了: Properly removing an Integer from a List (8 个回答) 已关闭 4 年前。 我是java新手。看起来很简单,但我不明白为什么会发生这种
我是一名优秀的程序员,十分优秀!