这编译为更快的code:" N * 3英寸或QUOT; N +(N * 2)QUOT ;? [英] Which compiles to faster code: "n * 3" or "n+(n*2)"?

查看:133
本文介绍了这编译为更快的code:" N * 3英寸或QUOT; N +(N * 2)QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这编译更快code:ANS = N * 3或ANS = N +(N * 2)

Which compiles to faster code: "ans = n * 3" or "ans = n+(n*2)"?

假设n是不是一个int或长,并且它是在一个现代化的Win32英特尔机器上运行。

Assuming that n is either an int or a long, and it is is running on a modern Win32 Intel box.

这会是不同的,如果有一些涉及取消引用,那就是,这其中会更快?

Would this be different if there was some dereferencing involved, that is, which of these would be faster?



long    a;
long    *pn;
long     ans;

...
*pn = some_number;
ans = *pn * 3;

或者


ans = *pn+(*pn*2);

或者是一些人们不必担心因为优化编译器可能会在任何情况下,考虑到这一点?

Or, is it something one need not worry about as optimizing compilers are likely to account for this in any case?

推荐答案

IMO这种微型的优化,除非你有一些外来的编译器的工作是没有必要的。我把可读性放在首位。

IMO such micro-optimization is not necessary unless you work with some exotic compiler. I would put readability on the first place.

这篇关于这编译为更快的code:" N * 3英寸或QUOT; N +(N * 2)QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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