我一直认为这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
Run Code Online (Sandbox Code Playgroud)
这大约慢了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 " << …Run Code Online (Sandbox Code Playgroud) 我std::string什么时候应该使用,什么时候应该char*用来管理charC++中的s 数组?
char*如果性能(速度)至关重要,并且由于内存管理,您愿意接受一些有风险的业务,那么您似乎应该使用它.
是否还有其他需要考虑的方案?
编辑:我编辑了问题及其标题更精确.
考虑以下源代码:
#include <vector>
struct xyz {
xyz() { } // empty constructor, but the compiler doesn't care
xyz(const xyz& o): v(o.v) { }
xyz& operator=(const xyz& o) { v=o.v; return *this; }
int v; // <will be initialized to int(), which means 0
};
std::vector<xyz> test() {
return std::vector<xyz>(1024); // will do a memset() :-(
}
Run Code Online (Sandbox Code Playgroud)
...我怎么能避免vector <>分配的内存用它的第一个元素的副本进行初始化,这是一个O(n)操作我宁愿为了速度而跳过,因为我的默认构造函数什么都不做?
如果不存在通用的解决方案,那么g ++特定的解决方案就可以做到(但我找不到任何属性来执行此操作).
编辑:生成的代码如下(命令行:arm-elf-g ++ - 4.5 -O3 -S -fno-verbose-asm -o-test.cpp | arm-elf-c ++ filt | grep -vE'^ [[: space:]] …
我试图解决C ++中的编码问题,该问题计算素数的数量小于非负数的数量n。
所以我首先想出了一些代码:
int countPrimes(int n) {
vector<bool> flag(n+1,1);
for(int i =2;i<n;i++)
{
if(flag[i]==1)
for(long j=i;i*j<n;j++)
flag[i*j]=0;
}
int result=0;
for(int i =2;i<n;i++)
result+=flag[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
这需要88毫秒,并使用8.6 MB的内存。然后,我将代码更改为:
int countPrimes(int n) {
// vector<bool> flag(n+1,1);
bool flag[n+1] ;
fill(flag,flag+n+1,true);
for(int i =2;i<n;i++)
{
if(flag[i]==1)
for(long j=i;i*j<n;j++)
flag[i*j]=0;
}
int result=0;
for(int i =2;i<n;i++)
result+=flag[i];
return result;
}
Run Code Online (Sandbox Code Playgroud)
这需要28毫秒和9.9 MB。我真的不明白为什么在运行时间和内存消耗上都存在这样的性能差距。我已阅读相类似的问题这一个和那一个,但我仍然困惑。
编辑:我的运行时间与11.5 MB的存储器替换之后减少至40毫秒vector<bool>与vector<char>。
我有一个C++ 03应用程序,其中std::vector<T>类型作为临时缓冲区使用.因此,它们通常会使用std::vector<T>::resize()以确保它们足够大以在使用前保存所需数据.这个函数的C++ 03原型实际上是:
void resize(size_type n, value_type val = value_type());
Run Code Online (Sandbox Code Playgroud)
因此,实际上在调用时resize(),通过添加适当数量的副本来放大矢量val.但是,通常我只需要知道它vector足够大以容纳我需要的数据; 我不需要用任何值初始化它.复制构造新值只是浪费时间.
C++ 11拯救了(我想):在它的规范中,它分为resize()两个重载:
void resize(size_type n); // value initialization
void resize(size_type n, const value_type &val); // initialization via copy
Run Code Online (Sandbox Code Playgroud)
这非常适合C++的理念:只需支付你想要的东西.正如我所指出的那样,我的应用程序不能使用C++ 11,所以当我遇到Boost.Container库时,我很高兴,它表明在其文档中支持这个功能.具体来说,boost::container::vector<T>实际上有三个重载resize():
void resize(size_type n); // value initialization
void resize(size_type n, default_init_t); // default initialization
void resize(size_type n, const value_type &val); // initialization via copy
Run Code Online (Sandbox Code Playgroud)
为了验证我理解了所有内容,我进行了快速测试以验证C++ 11的行为std::vector<T>和boost::container::vector<T> …
想象一下,你有一个简单的矩阵类
template <typename T = double>
class Matrix {
T* data;
size_t row, col;
public:
Matrix(size_t m, size_t n) : row(m), col(n), data(new T[m*n]) {}
//...
friend std::ostream& operator<<(std::ostream& os, const Matrix& m) {
for (int i=0; i<m.row; ++i) {
for (int j=0; j<m.col; ++j)
os<<" "<<m.data[i + j*m.row];
os<<endl;
}
return os;
}
};
Run Code Online (Sandbox Code Playgroud)
有没有办法用初始化列表初始化这个矩阵?我的意思是从初始化列表中获取矩阵和元素的大小.类似下面的代码:
Matrix m = { {1., 3., 4.}, {2., 6, 2.}};
Run Code Online (Sandbox Code Playgroud)
会打印
1 3 4
2 6 2
Run Code Online (Sandbox Code Playgroud)
期待您的回答.谢谢你们.AA
编辑
所以我研究了你的建议来制作一个使用初始化列表初始化元素的通用数组.但这是我能获得的最通用的.如果你们中的任何人有任何建议可以使它成为一个更通用的课程,我将不胜感激.还有几个问题:
为了分配动态内存,我一直在C++中使用向量.但是最近,在阅读一些源代码时,我发现使用"new int [size]"并在一些研究中发现它也分配了动态内存.
谁能给我建议哪个更好?我从算法和ICPC的角度来看?
我知道手动动态内存分配通常是一个坏主意,但它有时比使用更好的解决方案,比方说,std::vector?
举一个粗略的例子,如果我必须存储一个n整数数组,其中n<= 16,比如说.我可以使用它来实现它
int* data = new int[n]; //assuming n is set beforehand
Run Code Online (Sandbox Code Playgroud)
或使用矢量:
std::vector<int> data;
Run Code Online (Sandbox Code Playgroud)
使用一个std::vector或者是否存在实际情况,手动分配动态内存是一个更好的想法,以提高效率,这绝对是一个更好的主意吗?
我正在为具有不同数据结构和技术(向量,数组和OpenMP)的矩阵实现C++乘法,我发现了一个奇怪的情况......我的动态数组版本运行得更好:
时间:
openmp mult_1:time:5.882000 s
array mult_2:time:1.478000 s
我的编译标志是:
/ usr/bin/g ++ -fopenmp -pthread -std = c ++ 1y -O3
C++矢量版
typedef std::vector<std::vector<float>> matrix_f;
void mult_1 (const matrix_f & matrixOne, const matrix_f & matrixTwo, matrix_f & result) {
const int matrixSize = (int)result.size();
#pragma omp parallel for simd
for (int rowResult = 0; rowResult < matrixSize; ++rowResult) {
for (int colResult = 0; colResult < matrixSize; ++colResult) {
for (int k = 0; k < matrixSize; ++k) {
result[rowResult][colResult] += …Run Code Online (Sandbox Code Playgroud) c++ ×9
arrays ×4
vector ×4
c++11 ×3
performance ×2
stl ×2
boost ×1
clang ×1
dynamic ×1
gcc ×1
matrix ×1
memory ×1
nested-lists ×1
openmp ×1
optimization ×1
std ×1
stdstring ×1
stdvector ×1
user-manual ×1