- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我一直认为 std::vector
被“实现为数组”是普遍的智慧,等等等等。今天下楼测试了一下,好像不是这样:
以下是一些测试结果:
UseArray completed in 2.619 seconds
UseVector completed in 9.284 seconds
UseVectorPushBack completed in 14.669 seconds
The whole thing completed in 26.591 seconds
这大约慢了 3 到 4 倍!并不能真正证明“vector
可能会慢几纳秒”的评论。
以及我使用的代码:
#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <boost/date_time/posix_time/ptime.hpp>
#include <boost/date_time/microsec_time_clock.hpp>
class TestTimer
{
public:
TestTimer(const std::string & name) : name(name),
start(boost::date_time::microsec_clock<boost::posix_time::ptime>::local_time())
{
}
~TestTimer()
{
using namespace std;
using namespace boost;
posix_time::ptime now(date_time::microsec_clock<posix_time::ptime>::local_time());
posix_time::time_duration d = now - start;
cout << name << " completed in " << d.total_milliseconds() / 1000.0 <<
" seconds" << endl;
}
private:
std::string name;
boost::posix_time::ptime start;
};
struct Pixel
{
Pixel()
{
}
Pixel(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b)
{
}
unsigned char r, g, b;
};
void UseVector()
{
TestTimer t("UseVector");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
std::vector<Pixel> pixels;
pixels.resize(dimension * dimension);
for(int i = 0; i < dimension * dimension; ++i)
{
pixels[i].r = 255;
pixels[i].g = 0;
pixels[i].b = 0;
}
}
}
void UseVectorPushBack()
{
TestTimer t("UseVectorPushBack");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
std::vector<Pixel> pixels;
pixels.reserve(dimension * dimension);
for(int i = 0; i < dimension * dimension; ++i)
pixels.push_back(Pixel(255, 0, 0));
}
}
void UseArray()
{
TestTimer t("UseArray");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
Pixel * pixels = (Pixel *)malloc(sizeof(Pixel) * dimension * dimension);
for(int i = 0 ; i < dimension * dimension; ++i)
{
pixels[i].r = 255;
pixels[i].g = 0;
pixels[i].b = 0;
}
free(pixels);
}
}
int main()
{
TestTimer t1("The whole thing");
UseArray();
UseVector();
UseVectorPushBack();
return 0;
}
是我做错了吗?还是我刚刚打破了这个性能神话?
我在 Visual Studio 2005 中使用 Release模式.
在 Visual C++ , #define _SECURE_SCL 0
将 UseVector
减少一半(减少到 4 秒)。这真的很大,IMO。
最佳答案
使用以下内容:
g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseArray completed in 2.196 seconds
UseVector completed in 4.412 seconds
UseVectorPushBack completed in 8.017 seconds
The whole thing completed in 14.626 seconds
所以数组的速度是 vector 的两倍。
但是在更详细地查看代码之后,这是意料之中的;当您两次运行 vector 并且仅运行一次数组时。注意:当你 resize()
vector 时,你不仅在分配内存,而且还通过 vector 运行并调用每个成员的构造函数。
稍微重新排列代码,使 vector 只初始化每个对象一次:
std::vector<Pixel> pixels(dimensions * dimensions, Pixel(255,0,0));
现在再次做同样的时间:
g++ -O3 Time.cpp -I <MyBoost>
./a.out
UseVector completed in 2.216 seconds
vector 现在的性能只比数组差一点。 IMO 这种差异是微不足道的,可能是由一大堆与测试无关的事情引起的。
我还要考虑到您没有正确初始化/销毁 UseArrray()
方法中的 Pixel 对象,因为没有调用构造函数/析构函数(这可能不是问题简单的类但稍微复杂一点的类(即带有指针或带有指针的成员)都会导致问题。
关于c++ - std::vector 比普通数组慢很多吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664272/
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
DBMS 供应商使用 SQL 方言特性来区分他们的产品,同时声称支持 SQL 标准。 'Nuff 说。 您编写的任何 SQL 示例是否无法转换为 SQL:2008 标准 SQL? 具体来说,我说的是
多年来,我一直在考虑这个问题,但从未成功实现过。我说的是一个快速、高效的 C 函数,它在输入中接受一个整数值(例如 16 位),并在输出中给出完全不同的相同位大小的数字,但“考虑到”所有数字已经给出了
当标准 iPhone UI 控件变得过于平淡,并且您希望简单的记分应用程序通过颜色、动画、非标准 GUI 字体和背景壁纸等流行时。 ,为这样的事情集成游戏引擎有意义吗? 我对 Unity3D 和 To
这是我的第一个问题,所以如果我没有正确地标记标签,我很抱歉。我尝试过...这是我的问题:我希望有人能告诉我如何为普通的表格 View 创建 2 行节标题。我遇到的问题是:1)我找不到可以模仿默认 1
所以我一直在开发一个仅使用普通 JavaScript 的“非常简单”的计算器。但我不知道为什么它现在起作用了。 这是我的 JavaScript 和 HTML 代码: (function() { "
我正在尝试编写一个函数来满足以下要求: 给定一个对象和一个键,“getElementsThatEqual10AtProperty”返回一个数组,其中包含位于给定键处等于 10 的数组的所有元素。 注释
[最终编辑:我觉得有必要做出回应,因为我从这篇文章中学到了很多东西(主要是通过你们,我花了更多的时间来理解CSS..但最后,我真的不知道如何为了使这项工作有效..除了真正破坏html的基本结构..我不
我希望能够将一个函数附加到一个元素上,该函数只有在该元素上单击指定时间后才会运行。 有几个( 1 、 2 、 3 )与在 javascript 中处理鼠标保持相关的问题;但这些问题要么使用 jQuer
我想将泛型函数保存为变量: (defvar *gf* (make-instance 'standard-generic-function) 但是在添加方法时,我必须自己定义call-next-meth
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我有一个普通的 tableView——一个部分——当我滚动时,行出现在标题后面。像这样: 有没有简单的方法来防止这种情况?我认为它与 contentInset 有关,但这也会移动标题,这不是我想要的。
你好,我有一个ajax表单,它返回一个控制“发送”动画的脚本。然而,当淡入开始时,它会停止在 0.1 不透明度。我不确定脚本中有什么问题。任何帮助将不胜感激。 quote_form = documen
这是演示我的问题的代码笔:http://codepen.io/PiotrBerebecki/pen/yaWQwZ 目标是当用户点击时有滚动动画 顶部导航链接,以及 Back to Top 按钮在右下角
在我重新发明轮子之前,纯Java中有类似主题的并发队列吗?我有以下要求: 多个读者/消费者 多名作家/制片人 每条消息都必须由每个(活跃的)消费者消费 在每个消费者阅读一条消息后,它应该变成垃圾(即不
这个问题与 Do MySQL tables need an ID? 有一个无意义的auto_incremental ID作为一个表的PRIMARY KEY,那么我创建其他KEY时,我是否应该在KEY中
我有一个普通 UITableView 并且我想隐藏分隔符。为了隐藏它,我尝试使用以下属性: 我也在 viewDidLoad 中设置了它。 self.tableView.separatorStyle =
var vettore = document.getElementById(id_form).elements; for (var i = 0; i '+vettore_nomi_file[i]; 最
我已经构建了一个非常简单的轮播,但有一个问题。在我的轮播中,我有三张幻灯片,一个上一个按钮和一个下一个按钮。我想要的是当我单击下一个按钮并在最后一张幻灯片上转到第一张幻灯片时。此外,当我单击上一个按钮
我是 javascript 的新手,所以我需要一些帮助。 我正在尝试制作一个简单的插件(当然只是为了学习,以便更好地理解事物),但我遇到了一些麻烦,我将不胜感激。 我的插件是基本的,我正在尝试为 sc
我是一名优秀的程序员,十分优秀!