简体   繁体   English

高性能程序,最好是向量数组或向量载体

[英]High performance program, what is better array of vectors or vector of vectors

I'm coding what has to be a high-performance program. 我正在编写必须是高性能程序的代码。

What I have now is an array of vectors of type "A" that I've invented. 我现在所拥有的是我发明的向量类型为“ A”的向量数组。 The "A" is an struct that have a long long and a double. “ A”是具有长long和double的结构。

vector<A>* ord = new vector<A>[X];

Where X is a value that i don't know until executing time. X是直到执行时间我才知道的值。

What I have to do is, for each vector, sort its objects by the long long variable and then perform some operations. 对于每个向量,我要做的是按照long long变量对它的对象进行排序,然后执行一些操作。

My question is, it's better in terms of performance what I have now `An array of vectors' or it's better to create a vector of vectors like this: 我的问题是,就性能而言,我现在拥有的是“向量数组”会更好,或者创建这样的向量向量会更好:

vector<vector<A>> ord;

Two thinks: 有两种想法:

  • Notice that I don't have to do operations between vectors. 请注意,我不必在向量之间进行操作。
  • I have read in other post that it's a bad idea to use `new' operator, in this case, what I shoud use in my program. 我在其他文章中已经读到,使用'new'运算符是一个坏主意,在这种情况下,我应该在程序中使用什么。

A dynamically allocated array of vectors and a vector of vectors will likely be the same in optimized code. 向量的动态分配数组和向量的向量在优化代码中可能是相同的。 And the vector of vectors takes care of so many problems that unless you have some very unusual problem you should prefer that solution as a matter of course. 向量的向量处理了很多问题,因此除非您遇到一些非常不寻常的问题,否则您当然应该选择该解决方案。

Premature optimization is the root of all evil - Sir Tony Hoare 过早的优化是万恶之源-Tony Hoare爵士

You mention "X is a value that i don't know until executing time", does that mean its static? 您提到“ X是直到执行时才知道的值”,这是否意味着它是静态的?

If you need it to be dynamic use vectors, but if X doesn't change use arrays as it will save you a bit of memory (both will run at pretty much the same speed if X is static). 如果您需要它是动态的,请使用向量,但是如果X不变,请使用数组,因为它将节省您一点内存(如果X是静态的,两者的运行速度几乎相同)。

As for your question on when to use new, I'll defer to this post: When to use "new" and when not to, in C++? 至于关于何时使用new的问题,我将参考本文: 何时使用“ new”,何时不使用C ++?

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

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