- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我看过一些关于这个的讨论。许多人说他们应该是相同的速度。但是我自己做了一些测试。在我看来,使用 std::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;
};
using namespace std;
int main(int argc, char** argv)
{
// timing for vector calculations
{
int n = 100000;
std::vector<double> a(n),b(n),c(n);
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
TestTimer t("vector");
for ( long int j = 0; j < 1000; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
// timing for array calculations
{
int n = 100000;
double a[n],b[n],c[n];
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
TestTimer t("array");
for ( long int j = 0; j < 1000; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
}
然后我使用 -O0 或 -O3 使用 icpc 编译并运行了代码(g++ 给出了非常相似的结果。我重复了几次,结果是一样的。):
icpc test.C -o test.x -O3 -Fa -g
./test.x
vector completed in 0.06 seconds
array completed in 0.03 seconds
icpc test.C -o test.x -O0 -Fa -g
./test.x
vector completed in 0.269 seconds
array completed in 0.279 seconds
看起来与 -O3 一样,使用数组计算比使用 vector 快 2 倍。我查看了汇编代码(对于循环中的部分)。对于使用-O0 编译的汇编代码, vector 和数组看起来是一样的。但是当用-O3 编译时,它们看起来很不一样。 (见下文)我有点理解代码试图将数据从内存移动到寄存器(使用 movapsx),进行乘法运算(mulpdx)并将数据移回(moapsx)。但是 movsdq、movhpdq 和 movapsx 之间有什么不同,为什么编译器会为数组和 vector 生成不同的代码?
使用 vector 的 -O3 汇编代码(循环部分):
movsdq (%rbx,%r13,8), %xmm0
movsdq 0x10(%rbx,%r13,8), %xmm1
movsdq 0x20(%rbx,%r13,8), %xmm2
movsdq 0x30(%rbx,%r13,8), %xmm3
movhpdq 0x8(%rbx,%r13,8), %xmm0
movhpdq 0x18(%rbx,%r13,8), %xmm1
movhpdq 0x28(%rbx,%r13,8), %xmm2
movhpdq 0x38(%rbx,%r13,8), %xmm3
mulpdx (%r9,%r13,8), %xmm0
mulpdx 0x10(%r9,%r13,8), %xmm1
mulpdx 0x20(%r9,%r13,8), %xmm2
mulpdx 0x30(%r9,%r13,8), %xmm3
movapsx %xmm0, (%r15,%r13,8)
movapsx %xmm1, 0x10(%r15,%r13,8)
movapsx %xmm2, 0x20(%r15,%r13,8)
movapsx %xmm3, 0x30(%r15,%r13,8)
使用数组的 -O3 汇编代码(循环部分):
movapsx (%rbx,%rcx,8), %xmm0
movapsx 0x10(%rbx,%rcx,8), %xmm1
movapsx 0x20(%rbx,%rcx,8), %xmm2
movapsx 0x30(%rbx,%rcx,8), %xmm3
mulpdx (%rsi,%rcx,8), %xmm0
mulpdx 0x10(%rsi,%rcx,8), %xmm1
mulpdx 0x20(%rsi,%rcx,8), %xmm2
mulpdx 0x30(%rsi,%rcx,8), %xmm3
movapsx %xmm0, (%r13,%rcx,8)
movapsx %xmm1, 0x10(%r13,%rcx,8)
movapsx %xmm2, 0x20(%r13,%rcx,8)
movapsx %xmm3, 0x30(%r13,%rcx,8)
编辑:我已更新代码以测试更多案例:
#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;
};
using namespace std;
int main(int argc, char** argv)
{
int n = 100;
int N = 10000000;
// timing for vector calculations
{
std::vector<double> a(n),b(n),c(n);
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
{
TestTimer t("vector1");
for ( long int j = 0; j < N; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
}
// timing for vector calculations
{
std::vector<double> a(n),b(n),c(n);
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
{
TestTimer t("vector2");
for ( long int j = 0; j < N; j ++ )
for ( int i = 0; i < n; i ++ )
a[i]=b[i]*c[i];
}
}
// timing for array calculations
{
double a[n],b[n],c[n];
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
{
TestTimer t("array");
for ( long int j = 0; j < N; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
}
// timing for malloc calculations
{
double *a,*b,*c;
a=(double*)malloc(sizeof(double)*n);
b=(double*)malloc(sizeof(double)*n);
c=(double*)malloc(sizeof(double)*n);
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
{
TestTimer t("malloc");
for ( long int j = 0; j < N; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
}
// timing for new pointer calculations
{
double *a,*b,*c;
a=new double[n];
b=new double[n];
c=new double[n];
double* aa = &a[0];
double* bb = &b[0];
double* cc = &c[0];
{
TestTimer t("new pointer");
for ( long int j = 0; j < N; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
}
}
对于 n=100,N=10000000,我得到:
g++ test.C -o test.x -O3 -Fa -g
./test.x
vector1 completed in 0.487 seconds
vector2 completed in 0.504 seconds
array completed in 1.624 seconds
malloc completed in 0.409 seconds
new pointer completed in 0.502 seconds
icpc test.C -o test.x -O3 -Fa -g
./test.x
vector1 completed in 0.318 seconds
vector2 completed in 0.319 seconds
array completed in 0.216 seconds
malloc completed in 0.295 seconds
new pointer completed in 0.289 seconds
对于 n=100000,N=10000,我得到:
g++ test.C -o test.x -O3 -Fa -g
./test.x
vector1 completed in 0.699 seconds
vector2 completed in 0.648 seconds
array completed in 0.397 seconds
malloc completed in 0.428 seconds
new pointer completed in 0.464 seconds
icpc test.C -o test.x -O3 -Fa -g
./test.x
vector1 completed in 0.632 seconds
vector2 completed in 0.616 seconds
array completed in 0.308 seconds
malloc completed in 0.357 seconds
new pointer completed in 0.322 seconds
最佳答案
您没有测量与 std::vector
相关的
任何
除了运行它们的析构函数和释放内存所花费的时间,这不需要为阵列完成。
尝试将计时器放在一个新的范围内,这样它会在您销毁 vector 之前停止计时,您应该会看到非常相似的时间:
{
TestTimer t("vector");
for ( long int j = 0; j < 1000; j ++ )
for ( int i = 0; i < n; i ++ )
aa[i]=bb[i]*cc[i];
}
原因是 std::vector
内部只有一个数组,所以通过获取第一个元素的地址并对该数组执行所有计算,您不会对 vector 。完全没有意义的测试。
如果您测试了 a[i] = b[i] * c[i]
将显示在使用 vector 时是否存在差异。
目标代码中的微小差异可能是由于编译器不够智能,无法区分&a[0]
和&b[0]
和&c[0]
当这些地址来自 vector 的起始指针时,它们不会相互混淆,而它可以为在堆栈上声明的数组指明这一点。
关于c++ - C++ std::vector 是否比 C99 可变长度数组慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26536351/
为什么禁用类型像 type t = A of int | B of string * mutable int 虽然允许此类类型: type t = A of int | B of string * i
我正在寻找一种类似结构的数据结构,我可以从中创建多个实例并具有某种类型提示而不是不可变的。 所以我有这样的东西: class ConnectionConfig(NamedTuple): nam
我需要转到引用的结构: class SearchKnot { var isWord : Bool = false var text : String = "" var to
如sec 10.4.3中所述 当控制进入执行时,执行以下步骤 功能对象F(调用者)中包含的功能代码的上下文 提供thisArg,而调用方提供argumentsList: 如
i make a game that start display Activity indicator And activity indicator bottom display UiLable wi
编辑:我在这里不断获得支持。只是为了记录,我认为这不再重要。自从我发布它以来我就不再需要它了。 我想在 Scala 中执行以下操作... def save(srcPath: String, destP
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
如果您在Kotlin中访问List类型的Java值,则将获得(Mutable)List!类型。 例如。: Java代码: public class Example { public stati
我编写了 str 类(内置)的以下扩展,以便执行以下操作:假设我有字符串 "Ciao" ,通过做"Ciao" - "a"我想要的结果是字符串 "Cio" 。这是执行此操作的代码,并且运行良好: cla
使用可变对象作为 Hashmap 键是一种不好的做法吗?当您尝试使用已修改足以更改其哈希码的键从 HashMap 中检索值时,会发生什么? 例如,给定 class Key { int a; /
我正在为我的公司设计一个数据库来管理商业贷款。每笔贷款都可以有担保人,可以是个人或公司,在借款业务失败时作为财务支持。 我有 3 个表:Loan、Person 和 Company,它们存储明显的信息。
我使用二进制序列化从 C# 类中保存 F# 记录。一切正常: F#: type GameState = { LevelStatus : LevelStatus
import javax.swing.JOptionPane; public class HW { public static void main(String[] args) { Strin
使用 flatbuffer mutable 有多少性能损失? 是否“正确”使用 FlatBuffers 来拥有一个应该可编辑的对象/结构(即游戏状态) 在我的示例中,我现在有以下类: class Ga
std::function create_function (args...) { int x = initial_value (args...); return [x] () mut
我需要在 for 循环中找到用户输入的字符。我通常会这样做 如果(句子[i] == 'e') 但是因为在这里,'e' 将是一个单字母字符变量,我不知道如何获取要比较的值。我不能只输入 if (sent
我有一个这样的算法: let seed: Foo = ... let mut stack: Vec = Vec::new(); stack.push(&seed); while let Some(ne
这个问题可能看起来非常基础,但我很难弄清楚如何做。我有一个整数,我需要使用 for 循环来循环整数次。 首先,我尝试了—— fn main() { let number = 10; // An
如果我有以下结构: struct MyStruct { tuple: (i32, i32) }; 以及以下函数: // This will not compile fn function(&mut s
我希望在每个 session 的基础上指定列的默认值。下面的脚本不起作用,但描述了我想如何使用它。我目前使用的是 MySQL 5.5.28,但如果需要可以升级。 CREATE TABLE my_tbl
我是一名优秀的程序员,十分优秀!