哪个更快? [英] Which if faster?

查看:62
本文介绍了哪个更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




这是一个与一般指针有关的问题(C或C ++)。

以下哪项更快,为什么?


for(int i = 0; i< N; i ++)

= ... a [i] ...


(或)


for(int i = 0; i< N; i ++)

= ... * a ++ ....

''''是一个动态分配的任何有效标量类型的数组和

大小为N.我已经读过某个地方,后者更快。但是不能理解为什么?b $ b了解为什么?或者这取决于架构或编译器?

Hi,

This is a question that pertains to pointers in general (C or C++).
Which of the following is faster and why?

for (int i = 0; i < N; i++)
= ... a[i]...

(or)

for (int i = 0; i < N; i++)
= ...*a++....
''a'' is a dynamically allocated array of any valid scalar type and of
size N. I have read somewhere that the latter is faster. But could not
understand why? Or is that dependent on architecture or compiler?

推荐答案

以下哪项更快,为什么?
Which of the following is faster and why?

>

for(int i = 0; i< N; i ++)

=。 .. a [i] ...


(或)


for(int i = 0; i< N; i ++)

= ... * a ++ ....


''''是一个动态分配的任何有效标量类型的数组和

size N.我读过某个地方,后者更快。但是不能理解为什么?b $ b了解为什么?或者这取决于架构或编译器?
>
for (int i = 0; i < N; i++)
= ... a[i]...

(or)

for (int i = 0; i < N; i++)
= ...*a++....
''a'' is a dynamically allocated array of any valid scalar type and of
size N. I have read somewhere that the latter is faster. But could not
understand why? Or is that dependent on architecture or compiler?



两个答案:

1)对于任何合理的现代优化编译器,它们应该是

相同的速度。

2)这种微优化几乎无关紧要;更好地使用
花时间修复慢速算法,处理缓慢的IO等等。无论是什么

a profiler告诉你的是代码的一部分*实际上*采取

时间。


Michael

Two answers:
1) For any reasonably modern optimizing compiler, they should be the
same speed.
2) This kind of micro-optimization will almost never matter; better to
spend time on fixing slow algorithms, handling slow IO, etc. Whatever
a profiler tells you is the part of the code that is *actually* taking
time.

Michael




Ganesh写道:

Ganesh wrote:




这是一个与一般指针有关的问题(C或C ++)。

以下哪项更快,为什么?


for(int i = 0; i< N; i ++)

= .. .a [i] ...


(或)


for(int i = 0; i< N; i ++)

= ... * a ++ ....


''''是一个动态分配的任何有效标量类型的数组和

size N.我读过某个地方,后者更快。但是不能理解为什么?b $ b了解为什么?或者这取决于架构或编译器?
Hi,

This is a question that pertains to pointers in general (C or C++).
Which of the following is faster and why?

for (int i = 0; i < N; i++)
= ... a[i]...

(or)

for (int i = 0; i < N; i++)
= ...*a++....
''a'' is a dynamically allocated array of any valid scalar type and of
size N. I have read somewhere that the latter is faster. But could not
understand why? Or is that dependent on architecture or compiler?



可能是因为第一个意味着演员阵容。


具有讽刺意味的是,你可能会更快。 with:

for(size_t i = 0; i< N; ++ i)

{

//你的选择
}

Probably because the first implies a cast.

Ironically, you''ld probably get "faster" with:
for( size_t i = 0; i < N; ++i)
{
// your choice
}


Ganesh发布:
Ganesh posted:

以下哪项更快为什么?


for(int i = 0; i< N; i ++)

= ... a [i] ...


(或)


for(int i = 0; i< N; i ++)

= .. 。* a ++ ....
Which of the following is faster and why?

for (int i = 0; i < N; i++)
= ... a[i]...

(or)

for (int i = 0; i < N; i++)
= ...*a++....



在大多数系统中,以下是最快的:


int * p = arr ;

int const * const pover = arr + len;


do * p ++ = ...

while(pover! = p);


在具有CPU指令同时接收指针和

偏移的系统上,以下是最快的:


size_t i = 0;


do arr [i ++] = ...

while(len!= i);


在编写便携式代码时,我选择前者。


-


Frederick Gotham


On most systems, the following is the fastest:

int *p = arr;
int const *const pover = arr+len;

do *p++ = ...
while (pover != p);

On systems which have a CPU instruction which takes both a pointer and an
offset, the following is fastest:

size_t i = 0;

do arr[i++] = ...
while (len != i);

When writing portable code, I opt for the former.

--

Frederick Gotham


这篇关于哪个更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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