使用vector而不是动态声明的数组 [英] Using vector instead of an dynamically declared array

查看:104
本文介绍了使用vector而不是动态声明的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近得到了一个提示(猜测在哪里,呵呵)并直接指向了矢量类
。现在,我没有问题使用

,但是我担心性能问题。


哪个最快(明显更快,更适合任何

方式等)在以下列表中。


a)std :: vector?


b)a带数组和整数的struct?

struct {int * arr = new arr [m]; int m}


c)将int保存为实例变量?

int * arr = new arr [m];

this.arrSize = m;


在程序中,会有很多循环和迭代

而且我应该使用C ++,如果这对于回答abc问题非常重要。


-

V?nligen Kerstin Viltersten

(酷长颈鹿)

解决方案

2月15日,08:59,The Cool Giraffe < giraf ... @ viltersten.comwrote:


我最近得到了一个提示(猜测在哪里,呵呵)并直接

指出到矢量类。现在,我没有使用

的问题,但我担心性能问题。



我可以问为什么?你不是在考虑过早优化吗?

你呢?


哪个最快(明显更快,更适合任何

方式等)在以下列表中。


a)std :: vector?


b)带数组的结构和整数?

struct {int * arr = new arr [m]; int m}


c)将int保存为实例变量?

int * arr = new arr [m];

this.arrSize = m;



了解其中哪一个更快的唯一方法是测量所有这些的速度。请记住,编译器有开关

和可以改变结果的设置,所以你需要知道如何设置你的特定编译器以便不要偏向结果。

暗示你在一个编译器上的结果可能不适用于另一个编译器。


还记得那个虽然表演通常是你的事情,但是大部分时间你都不应该担心(或者更具体地说,

除非你知道你有一个问题),有时候

确实很重要。编写你的实现

std :: vector的人知道所以它会被仔细编写,以免不必要地使用

时间或空间。而且,当然,它已经受到了大量的测试和调试(任何已经使用了与你相同的实施方式的b $ b),它赢得了''对于任何一只手来说都是如此 -

你写的替换品。


而不是问自己哪三种选择中最快的,

问自己哪个最不可能导致代码中出现bug。


在程序中,会有很多循环和迭代

和我应该使用C ++,如果这对于b / b
回答abc-questions有任何重要性。



如果您应该使用C ++,请使用std :: vector。专注于编写

代码,以最快的方式使您获得清晰,正确的程序。

您的时间是最重要的优化资源。重新发明

轮是不可能的。
http://www.parashift.com/c++-faq-lit....html#faq-34.1

Gavin Deane


The Cool Giraffe写道:


我有一个最近暗示(猜测在哪里,呵呵)并直接指向了矢量类
。现在,我没有问题使用

,但是我担心性能问题。


哪个最快(明显更快,更适合任何

方式等)在以下列表中。


a)std :: vector?


b)a带数组和整数的struct?

struct {int * arr = new arr [m]; int m}


c)将int保存为实例变量?

int * arr = new arr [m];

this.arrSize = m;


在程序中,会有很多循环和迭代

而且我应该使用C ++,如果这对于回答abc问题非常重要。



所有三种解决方案同样快。它们之间没有明显的区别




话虽如此,使用std :: vector。它旨在解决您的问题,并解决了它们。


- John Ratliff


The Cool Giraffe写道:


我最近得到了一个提示(猜测在哪里,呵呵)并直接

指向矢量类。现在,我没有问题使用

,但是我担心性能问题。


哪个最快(明显更快,更适合任何

方式等)在以下列表中。


a)std :: vector?


b)a带数组和整数的struct?

struct {int * arr = new arr [m]; int m}


c)将int保存为实例变量?

int * arr = new arr [m];

this.arrSize = m;


在程序中,会有很多循环和迭代

而且我应该使用C ++ ,如果这对于b / b
的任何重要性都能回答abc-questions。



如果你只迭代它,那么上面所有3个版本的

应该是相同的速度。在迭代上。为了证明这一点,只需使用各种版本对您的代码进行基准测试

并自行查看(别忘了启用

至少内联函数优化编译器,这样std :: vector的大多数方法得到了内联)。


-

晕了
http://dizzy.roedu.net


I got a hint recently (guess where, hehe) and was directly
pointed to the vector class. Now, i have no issues using
that way but i''m concerned about the performance issue.

Which is fastest (significantly faster, preferable in any
way, etc.) in the following list.

a) std::vector?

b) a struct with an array and an integer?
struct {int* arr = new arr[m]; int m}

c) saving the int as an instance variable?
int* arr = new arr[m];
this.arrSize = m;

In the program, there will be a lot of looping and iterating
and i''m supposed to use C++, if that''s of any importance to
answer the abc-questions.

--
V?nligen Kerstin Viltersten
(The Cool Giraffe)

解决方案

On 15 Feb, 08:59, "The Cool Giraffe" <giraf...@viltersten.comwrote:

I got a hint recently (guess where, hehe) and was directly
pointed to the vector class. Now, i have no issues using
that way but i''m concerned about the performance issue.

May I ask why? You''re not thinking about premature optimisation are
you?

Which is fastest (significantly faster, preferable in any
way, etc.) in the following list.

a) std::vector?

b) a struct with an array and an integer?
struct {int* arr = new arr[m]; int m}

c) saving the int as an instance variable?
int* arr = new arr[m];
this.arrSize = m;

The only way to know which of a these is faster is to measure the
speed of all of them. And bear in mind that compilers have switches
and settings that can alter the results so you''ll need to know how to
set up your particular compiler so as not to bias the results. The
implication there is that your results on one compiler may not be
applicable to a different compiler.

Also remember that, while performance is usually something you
shouldn''t be worrying about most of the time (or more specifically,
unless and until you know you''ve got a problem), there are times when
it does matter. The people who wrote your implementation of
std::vector knew that so it will be written carefully so as not to use
time or space unnecessarily. And, of course, it has been subject to a
huge amount of testing and debugging already (by anybody who has used
the same implementation as you) which won''t be true for any hand-
rolled replacement you write.

Rather than asking yourself which is fastest of your three options,
ask yourself which is least likely to result in code with bugs in it.

In the program, there will be a lot of looping and iterating
and i''m supposed to use C++, if that''s of any importance to
answer the abc-questions.

If you''re supposed to use C++, use std::vector. Concentrate on writing
code in the way that gets you to a clear, correct program the fastest.
Your time is the most important resource to optimise. Reinventing the
wheel isn''t the way to do that.
http://www.parashift.com/c++-faq-lit....html#faq-34.1

Gavin Deane


The Cool Giraffe wrote:

I got a hint recently (guess where, hehe) and was directly
pointed to the vector class. Now, i have no issues using
that way but i''m concerned about the performance issue.

Which is fastest (significantly faster, preferable in any
way, etc.) in the following list.

a) std::vector?

b) a struct with an array and an integer?
struct {int* arr = new arr[m]; int m}

c) saving the int as an instance variable?
int* arr = new arr[m];
this.arrSize = m;

In the program, there will be a lot of looping and iterating
and i''m supposed to use C++, if that''s of any importance to
answer the abc-questions.

All three solutions are equally fast. There is no appreciable difference
between them.

That being said, use std::vector. It was designed to solve your
problems, and it solves them well.

--John Ratliff


The Cool Giraffe wrote:

I got a hint recently (guess where, hehe) and was directly
pointed to the vector class. Now, i have no issues using
that way but i''m concerned about the performance issue.

Which is fastest (significantly faster, preferable in any
way, etc.) in the following list.

a) std::vector?

b) a struct with an array and an integer?
struct {int* arr = new arr[m]; int m}

c) saving the int as an instance variable?
int* arr = new arr[m];
this.arrSize = m;

In the program, there will be a lot of looping and iterating
and i''m supposed to use C++, if that''s of any importance to
answer the abc-questions.

If you only iterate over it all 3 versions above should have the
same "speed" on iteration. In order to prove this just benchmark your code
with various versions and see for yourself (don''t forget to enable at
least "inline" functions optimizations in your compiler, so that most
methods of std::vector get inlined).

--
Dizzy
http://dizzy.roedu.net


这篇关于使用vector而不是动态声明的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆