受过教育的效率猜测? [英] Educated guesses for efficiency?

查看:66
本文介绍了受过教育的效率猜测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




通常,人们可能会问一个问题:哪个代码

代码段效率更高?。


如果代码不是汇编程序(例如C或C ++),那么

没有确切的答案,因为我们不知道指令集

目标系统,或者编译器将如何映射每个可执行行的

C代码到汇编指令。


我认为自己要相当精通C语言,但我会承认我知道

关于机器代码,指令集等等很少...


在编写完全可移植的代码时,是否有关于如何
编写代码以进行有根据的猜测关于*大多数*目标平台上的效率是多少?


例如,我听说在C的原始机器上/>
意图,有一条CPU指令可以执行下面的




* p ++ = something; / *(p是指针变量)* /


因此,当时使用* p ++是有意义的。作为

在代码中尽可能多。


所以,我的问题是:网上是否有任何类型的指南

讨论了应该在完全可移植的代码中使用的构造,因为

他们可能全面高效吗?

-


Frederick Gotham



Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".

If the code is anything other than assembler (e.g. C or C++), then
there''s no precise answer because we don''t know the instruction set of
the target system, or how the compiler will "map" each executable line of
C code to assembly instructions.

I consider myself to be fairly proficient in C, but I''ll admit I know
very little about machine code, instructions set, and the like...

When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.

So, my question is: Is there any sort of guide available on the web which
discusses constructs which should be used in fully-portable code because
they''re likely to be quite efficient across the board?
--

Frederick Gotham

推荐答案

Frederick Gothamaécrit:
Frederick Gotham a écrit :
通常,人们可能会问问题是:哪个代码
代码段效率更高?

如果代码不是汇编程序(例如C或C ++),那么
没有确切的答案,因为我们不知道目标系统的指令集,或者编译器将如何映射目标系统的指令集。每个可执行行的C / C代码到汇编指令。

我认为自己对C语言相当精通,但我承认我对机器代码知之甚少,指令集等等...

在编写完全可移植的代码时,是否有关于如何编写代码以进行有根据的猜测的指导原则。关于*大多数*目标平台上哪些更有效?

例如,我听说在C的原始机器上,有一个可以执行以下任务的单CPU指令:

* p ++ = something; / *(p是一个指针变量)* /

因此,在那时,使用* p ++是有意义的。尽可能在代码中。

所以,我的问题是:网上是否有任何类型的指南可以讨论应该完全使用的结构 - 便携式代码,因为它们可能全面高效?
Commonly, people may ask a question along the lines of, "Which code
snippet is more efficient?".

If the code is anything other than assembler (e.g. C or C++), then
there''s no precise answer because we don''t know the instruction set of
the target system, or how the compiler will "map" each executable line of
C code to assembly instructions.

I consider myself to be fairly proficient in C, but I''ll admit I know
very little about machine code, instructions set, and the like...

When writing fully-portable code, are there any guidelines as to how to
write your code in order to make an "educated guess" as to what would be
more efficient on *most* target platforms?

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.

So, my question is: Is there any sort of guide available on the web which
discusses constructs which should be used in fully-portable code because
they''re likely to be quite efficient across the board?




没有一个代码片段比另一个更有效,如果他们

是完全相同的。今天的优化编译器相当不错

生成非常紧凑和快速的汇编代码。


在现代工作站中,大多数传统的优化都是如此。喜欢


" a * 2比<<<< 1


不再有效。比指令更重要

本身就是数据布局。


今天的数据布局可以提高效率。内存是

或多或少运行400 MHZ,处理器运行速度超过
2-3GHZ。通过改善数据访问的局部性,您可以通过避免昂贵的主存储器读取来获得大量的效率,这可能会花费您一个周期的10个或更多便宜指令。


数据布局是一个热门的研究课题,我建议你阅读关于这个的CPU供应商手册



对于AMD / Intel世界,两者都制作了优化手册,

是非常有益的阅读。


jacob



There is no one code snippet more efficient than another, if they
are strictly equivalent. Today''s optimizing compilers are quite good
at generating very tight and fast assembly code.

In modern workstations, most of the traditional "optimizations" like

"a*2 is more expensive than a << 1"

are no longer valid. Much more important than the instructions
themselves is the data layout.

Data layout is today where the efficiency can be improved. The memory is
running at more or less 400 MHZ, the processor is running at more than
2-3GHZ. By improving the locality of data accesses you can gain a lot
in efficiency by avoiding expensive main memory reads that can cost you
like 10 or more cheap instructions of one cycle.

Data layout is a hot research subject, and I would recommend you reading
the manuals of the CPU vendor about this.

For the AMD/Intel world, both have produced optimizations manuals that
are a very instructive read.

jacob


Frederick Gotham(在Xn *************************** @ 194.125.133.14)
说:


|所以,我的问题是:网上是否有任何类型的指南

|讨论了应该在全便携式中使用的结构

|代码,因为他们可能全面有效?


如果有,我会倾向于不相信它。编译器确实将任何和所有有效的源代码转换为高效的中间和/或可执行代码。这允许程序员专注于生成_valid_源代码解决方案。


-

Morris Dovey

DeSoto Solar

美国爱荷华州DeSoto
http ://www.iedu.com/DeSoto
Frederick Gotham (in Xn***************************@194.125.133.14)
said:

| So, my question is: Is there any sort of guide available on the web
| which discusses constructs which should be used in fully-portable
| code because they''re likely to be quite efficient across the board?

If there is, I''d be inclined to not trust it. It''s really the compiler
writers'' job to turn any and all valid source code into efficient
intermediate and/or executable codes. This allows the programmer to
focus on producing _valid_ source code solutions.

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto


Frederick Gotham写道:
Frederick Gotham wrote:

例如,我听说在C的原始机器上,有一条CPU指令可以执行以下操作:

* p ++ = something; / *(p是一个指针变量)* /

因此,在那时,使用* p ++是有意义的。尽可能在代码中。

For instance, I heard that on the original machine for which C was
intended, that there was a single CPU instruction which could perform the
following:

*p++ = something; /* (p is a pointer variable) */

Therefore, at the time, it would have made sense to make use of "*p++" as
much as possible in the code.



现代编译器非常擅长优化阵列操作,所以你不需要b
$ b你的方式使用指针。如果你确实使用指针,

任何好的编译器都应该能够转动


* p = something;

++ p ;

为高效代码,当某事时并不涉及冲突

操作。因此,对于需要了解它的人来说,使源代码清晰可见的旧建议比以往任何时候都要好。在++运算符中最有可能获得的
节省了打字和拼写错误。


某种让它狂热的架构更适合


* ++ p =某事;


并且可以检测到性能差异。


Modern compilers are very good at optimizing array operations, so you
shouldn''t go out of your way to use pointers. If you do use pointers,
any good compiler should be able to turn

*p = something;
++p;
into as efficient code, when "something" doesn''t involve conflicting
operations. So the old advice to make your source code as clear as
possible to humans who need to understand it is better than ever. The
most likely gain in the ++ operator is saving in typing and typos.

A certain architecture which has had it fanatics is better suited to

*++p = something;

and the difference in performance could be detected.


这篇关于受过教育的效率猜测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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