打字优化的代码 [英] wrinting an optimised code

查看:74
本文介绍了打字优化的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写优化代码的基本准则是什么。

为此我应该记住哪些要点?

这是纯粹的架构还是编译器特定的?

是否存在对所有架构有效的一般技术?

解决方案

在文章< 11 ********************** @ g44g2000cwa.googlegroups .com> ;,

< ju *** *******@yahoo.co.in>写道:

:编写优化代码的基本准则是什么。

:应该记住哪些要点?

:这个纯粹的架构或编译器是否具体?

:是否有任何适用于

的通用技术:所有架构?


编写优化代码的最重要指导原则之一是:


不要。


编写精心设计的可维护代码。然后用实际数据对它进行基准测试,看看它是什么时候花了它的时间。

看看你能不能优化它。如果是这样,好;如果没有,那么重新思考

-algorithm-而不是涉及各种

编码技巧。


除非你的代码有一个现实的机会被执行

一遍又一遍,数万次,

每次花费几分钟,写的时间花费了很多/>
聪明的代码通常会超过计算机执行生命周期内保存的计算机总时间。并且

维持优化代码...... Ei Yi Yi !!


的比例。例如,有一些数据集,其中冒泡排序比快速排序更快,因为快速排序有更多的启动开销。不要担心找到完美的

算法,直到你知道投入的时间将是值得的。

C特定的提示:自由地使用''const''和''register''。指针

别名是C中的一个问题,阻塞优化,并且
''const''和''register''给编译器提供关于事物的线索

是-not-别名(或者如果它们是,那就没关系

不考虑某些情况因为你的代码是

含蓄地声称它们没有别名。]类似地,使用指针在C中使用
并不总是更快:使用显式

数组索引可以产生更快的代码,因为编译器

可以知道什么是没有别名的东西,并且有一个更容易的时间

展开循环而不用担心一些微妙的指针

end-value semantic是违反。

-

研究表明,普通读者忽略了他们在.signatures中看到的所有统计数据的106%。


ju ********** @ yahoo.co.in 写道:

什么是t他是编写优化代码的基本指南。
为此我应该记住哪些要点?


1.过早优化是所有邪恶的根源(CARHoare,

通常也错误地归因于刚刚引用Hoare的Knuth)

2.使用好的算法。

3.(仅限专家)不要优化。

这个纯粹的架构或编译器是否具体?


这是一般的。

是否有适用于所有架构的通用技术?




编号

让它工作,如果它要减慢,优化,再次描述配置文件

,看它是否带来任何影响。 />

请阅读comp.lang.c中的过去的文章 - 这个问题已经一次又一次地上涨了

。最佳开始使用FAQ 20.12至20.14


干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。


ju ********** @ yahoo.co.in 写道:


编写优化代码的基本准则是什么。
为此我应该记住哪些要点?
这个纯粹的架构或编译器是否具体?
是否有任何适用于所有架构的通用技术?



快速完成工作的最佳方法是选择

正确的算法。


Erik

-

+ ------------------------------- ---------------------------- +

Erik de Castro Lopo 没有**** @ mega-nerd.com (是的,这是有效的)

+ ------- -------------------------------------------------- - +

Cunnilinugu s和精神病学把我们带到了这个位置。

- Tony Soprano在HBO'的黑道家族中


What are the basic guidelines to write an optimised code.
What points should one keep in mind for this ?
Is this purely architecture or complier specific ?
Are there any general techniques that are valid for
all architectures ?

解决方案

In article <11**********************@g44g2000cwa.googlegroups .com>,
<ju**********@yahoo.co.in> wrote:
:What are the basic guidelines to write an optimised code.
:What points should one keep in mind for this ?
:Is this purely architecture or complier specific ?
:Are there any general techniques that are valid for
:all architectures ?

One of the most important guidelines in writing optimised code is:

Don''t.

Write well-designed maintainable code instead. Then benchmark it
with real data to see where it''s -really- spending it''s time.
See if you can optimize that. If so, good; if not, then rethink
the -algorithm- rather than getting involved with all kinds of
coding tricks.

Unless your code has a realistic chance of being executed
over and over and over again, tens of thousands of times,
taking several minutes each time, the time spent writing the
clever code will often be more than the total amount of computer
time saved during the execution lifetime of the code. And
the maintenance of "optimized" code... Ei Yi Yi!!

Another point to keep in mind is to watch out for the constants
of proportionality. For example, there are data sets for which
a Bubble Sort is faster than Quick Sort, because Quick Sort has
more startup overhead. Don''t fret about finding the perfect
algorithm until you know that the time invested in it will be
worthwhile.
C specific hints: use ''const'' and ''register'' liberally. Pointer
aliasing is a problem in C, blocking optimizations, and
''const'' and ''register'' give the compiler clues about things
that are -not- aliases (or that if they are, that it''s okay
not to take some cases into account because your code is
implicitly asserting that they aren''t aliased.] Similarily,
in C, using pointers is NOT always faster: using explicit
array indexing can produce faster code because the compiler
can know what is not aliased to what, and has an easier time
unrolling loops without worrying that some subtle pointer
end-value semantic is being violated.
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.


ju**********@yahoo.co.in wrote:

What are the basic guidelines to write an optimised code.
What points should one keep in mind for this ?
1. Premature optimization is the root of all evil (C.A.R.Hoare,
often also wrongly ascribed to Knuth who just cited Hoare)
2. Use good algorithms.
3. (Experts only) Do not optimize yet.
Is this purely architecture or complier specific ?
This is general.
Are there any general techniques that are valid for
all architectures ?



No.
Get it to work, profile if it is to slow, optimise, profile again
to see whether it brought any effect.

Please read past articles in comp.lang.c -- this issue has come
up again and again. Best start with FAQ 20.12 to 20.14

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


ju**********@yahoo.co.in wrote:


What are the basic guidelines to write an optimised code.
What points should one keep in mind for this ?
Is this purely architecture or complier specific ?
Are there any general techniques that are valid for
all architectures ?



The best way to get a job done quickly is to choose
the right algoirthm.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo no****@mega-nerd.com (Yes it''s valid)
+-----------------------------------------------------------+
"Cunnilinugus and psychiatry brought us to this."
-- Tony Soprano in HBO''s "the Sopranos"


这篇关于打字优化的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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