月经淋漓不尽吃什么药| 心经讲的是什么| 番号是什么意思| 麻疹的症状是什么| 梦到抓了好多鱼是什么意思| 脚背有痣代表什么| 风寒感冒吃什么药好| 容祖儿老公叫什么名字| 胆小怕事是什么生肖| 立春吃什么食物| 引产是什么意思| 口臭口苦口干吃什么药| 什么龙什么虎| 男朋友发烧该说些什么| 为什么要补钾| 日照香炉生紫烟是什么意思| 终结者是什么意思| 鼻头发黑是什么原因| 耳朵里面疼什么原因| 失眠用什么药好| 什么人容易得多囊卵巢| 桃花什么时候开花| 杰字属于五行属什么| fe是什么元素| ccg是什么意思| 叹服是什么意思| fsw是什么意思| 双子座和什么星座最配| 什么野菜降血糖| 六根不净是什么意思| 墨菲定律是什么| im是什么意思| 宫腔占位什么意思| 乙肝不能吃什么东西| 为什么叫买东西不叫买南北| 天然是什么意思| 深覆合是什么样子的| 膈是什么器官| 53岁属什么| 睡觉容易惊醒是什么原因| 红斑狼疮是什么引起的| 心肺气虚吃什么中成药| 伯父是什么关系| 风疹病毒抗体阳性是什么意思| 蚊子怕什么| 秋季养胃吃什么好| 子宫后位什么意思| 烂嘴是什么原因| 什么颜色加什么颜色等于什么颜色| 橘猫是什么品种| 肚子胀恶心想吐是什么原因| 京五行属什么| 一纸空文是什么意思| 什么是普世价值| 小肚鸡肠是什么意思| 冰乙酸是什么| 食指是什么经络| 白内障是什么原因引起的| 6月15号是什么星座| 世界上最高的高原是什么| 日语亚麻跌是什么意思| 火牛命五行缺什么| 好男儿志在四方是什么生肖| 肝的作用和功能是什么| 榴莲有什么品种| 天蝎座喜欢什么样的女生| 内膜厚吃什么掉内膜| 朱元璋原名叫什么| 七月十日是什么星座| 巨蟹是什么星座| 骨裂什么症状| 阿奇霉素和头孢有什么区别| 酷儿是什么意思| 贫血应该吃什么| 葡萄籽有什么功效和作用| 汉高祖叫什么名字| 谷丙转氨酶偏高吃什么好| 叶赫那拉氏是什么旗| 药流后吃什么药| 擦是什么意思| 三代试管是什么意思| 苦荞是什么| 五心烦热是什么意思| 台湾什么时候收回| 苦荞茶喝了有什么好处| 挑担是什么关系| 9月29是什么星座| 吃什么能让胸变大| 左肋骨下方隐隐疼痛是什么原因| 罗飞鱼是什么鱼| 银芽是什么菜| 低血糖有什么危险| 女生爱出汗是什么原因| 人乳头瘤病毒感染是什么意思| 女生腰疼是什么原因| 息肉和囊肿有什么区别| 95年什么生肖| 溶菌酶是什么| 女生经常手淫有什么危害| 四川有什么山| 小孩睡觉出很多汗是什么原因| 两个方一个土读什么| pm2.5是什么意思| 人少了一魄什么反应| 什么就像什么造句| 馊主意是什么意思| 做梦梦到踩到屎是什么意思| 为什么不要看电焊火花| 黄金为什么值钱| 鸟代表什么生肖| 神经节是什么| 黄瓜长什么样| 1974年属虎是什么命| 游龙戏凤是什么意思| 经常打饱嗝是什么原因| 老花眼是什么症状| 支气管炎咳嗽吃什么药好得快| 芝兰是什么意思| 小妾是什么意思| 初一不能做什么| 沄字五行属什么| 什么是av| 胃炎能吃什么水果| 舌头上有红点是什么原因| nmol是什么单位| 花千骨最后结局是什么| 站街女是什么意思| 三聚净戒是指什么戒| 吃什么能补雌激素| 腹股沟淋巴结肿大挂什么科| 豆浆配什么主食当早餐| 土霉素主要是治疗什么病| 儿童内热吃什么去内热| tr是什么| 民间故事有什么| 投胎什么意思| 滋阴是什么意思| 为什么射不出来| 狗上皮过敏是什么意思| 什么是结扎| 什么是反射| 头顶头发稀少是什么原因| 君子兰用什么土最好| 大象是什么意思| 心病是什么意思| 牙齿痛什么原因| 全科医学科是什么科| 降压药有什么副作用| 蛋白是什么东西| 什么奶茶最贵| 赤什么意思| 嘴里发酸是什么原因| 吃桂圆干有什么好处和坏处| 肝脏不好吃什么食物才能养肝护肝| 原配是什么意思| 萃是什么意思| 支气管哮喘是什么原因引起的| 眼睛为什么老是流眼泪| 鹿茸有什么功效| 耳朵发炎吃什么药| 现在什么年| venes保温杯是什么品牌| 7月17号是什么星座| 周二右眼皮跳是什么预兆| 什么鱼没刺| 月经腰疼是什么原因引起的| art是什么意思| 口腔溃疡缺什么| 宫颈肥大有什么症状| 省纪委副书记是什么级别| 混合性皮肤用什么护肤品比较好| 眼皮肿什么原因引起的| 臻字五行属什么的| 晚上为什么不能剪指甲| 什么叫收缩压和舒张压| 梦见老公穿新衣服是什么意思| 癫痫病吃什么药| 喜欢白色的女人是什么性格| 大象喜欢吃什么| 黄加黑变成什么颜色| 迪丽热巴的全名叫什么| 脾是什么器官| 吃孕酮片有什么副作用| 蜗牛有什么特点| 什么的饰品| 花生对胃有什么好处| apc是什么意思| 同工同酬什么意思| 四月十号是什么星座| 月经期间喝什么汤好| 版心是什么| 怀孕早期吃什么| 怀男孩和女孩有什么区别| 心肌炎是什么病严重吗| 尿频尿急尿不尽挂什么科| 大姨妈来了喝红糖水有什么功效| 最好的油是什么油| 什么是清宫| 人为什么会得甲母痣| 女生的小鸡鸡长什么样| 生姜什么时候吃最好| 晚饭吃什么英语怎么说| 1947年属什么生肖| 相思病是什么意思| 火字旁的字有什么| 试管都有什么方案| 钳子什么牌子好| 水鱼什么意思| 明月几时有的下一句是什么| 总出汗是什么原因| 廿二是什么意思| 吞咽困难是什么感觉| 马齿苋煮水喝有什么功效| 2023年属什么| 碱性磷酸酶高是什么病| 斗志昂扬是什么意思| 蜻蜓点水的目的是什么| 十年大运是什么意思| 云代表什么动物| 岚字五行属什么| 福建有什么特产| 最好的烟是什么牌子| PA医学上是什么意思| 小孩肚子疼拉肚子吃什么药| 桃花劫是什么意思| 事半功倍什么意思| 画眉是什么| 手机卡顿是什么原因| 宫颈阳性是什么意思| 做梦梦见蜘蛛是什么意思| 帝女花讲的是什么故事| 成人改名字需要什么手续| 视力突然模糊是什么原因引起的| 胰腺占位是什么意思| 玛瑙对人体有什么好处| 中二是什么意思| 甘油三酯高是什么原因造成的| 荨麻疹吃什么药最好| 钵钵鸡是什么| 见红的血是什么颜色| 手发麻发木是什么病的前兆| 肾结石有什么症状哪里疼| 整夜失眠是什么病| 什么是紫癜| 丰富多腔的腔是什么意思| 来姨妈吃什么水果好| 加是什么生肖| 耄耋读什么| 炙是什么意思| 耳朵真菌感染用什么药| 什么叫肿瘤| 铜钱癣用什么药| 死鱼是什么意思| 纵容是什么意思| 支气管扩张什么症状| 卤水点豆腐是什么意思| 女性什么时候排卵| 素鲍鱼是什么做的| wink是什么意思| 什么降血糖| 腋毛有什么作用| 子宫腺肌症吃什么药最有效| 现在小麦什么价格| 内分泌失调看什么科| 百度Jump to content

大师用车|威格仕行车记录器面市 即将引发一场

From Wikipedia, the free encyclopedia
百度 1月28日,广西融水苗族自治县上里村,一名村民打算骑着结满冰凌的摩托车外出。

In computer science, program optimization, code optimization, or software optimization is the process of modifying a software system to make some aspect of it work more efficiently or use fewer resources.[1] In general, a computer program may be optimized so that it executes more rapidly, or to make it capable of operating with less memory storage or other resources, or draw less power.

Overview

[edit]

Although the term "optimization" is derived from "optimum",[2] achieving a truly optimal system is rare in practice, which is referred to as superoptimization. Optimization typically focuses on improving a system with respect to a specific quality metric rather than making it universally optimal. This often leads to trade-offs, where enhancing one metric may come at the expense of another. One popular example is space-time tradeoff, reducing a program’s execution time by increasing its memory consumption. Conversely, in scenarios where memory is limited, engineers might prioritize a slower algorithm to conserve space. There is rarely a single design that can excel in all situations, requiring engineers to prioritize attributes most relevant to the application at hand.

Furthermore, achieving absolute optimization often demands disproportionate effort relative to the benefits gained. Consequently, optimization processes usually stop once sufficient improvements are achieved, without striving for perfection. Fortunately, significant gains often occur early in the optimization process, making it practical to stop before reaching diminishing returns.

Levels of optimization

[edit]

Optimization can occur at a number of levels. Typically the higher levels have greater impact, and are harder to change later on in a project, requiring significant changes or a complete rewrite if they need to be changed. Thus optimization can typically proceed via refinement from higher to lower, with initial gains being larger and achieved with less work, and later gains being smaller and requiring more work. However, in some cases overall performance depends on performance of very low-level portions of a program, and small changes at a late stage or early consideration of low-level details can have outsized impact. Typically some consideration is given to efficiency throughout a project – though this varies significantly – but major optimization is often considered a refinement to be done late, if ever. On longer-running projects there are typically cycles of optimization, where improving one area reveals limitations in another, and these are typically curtailed when performance is acceptable or gains become too small or costly. Best practices for optimization during iterative development cycles include continuous monitoring for performance issues coupled with regular performance testing.[3][4]

As performance is part of the specification of a program – a program that is unusably slow is not fit for purpose: a video game with 60 Hz (frames-per-second) is acceptable, but 6 frames-per-second is unacceptably choppy – performance is a consideration from the start, to ensure that the system is able to deliver sufficient performance, and early prototypes need to have roughly acceptable performance for there to be confidence that the final system will (with optimization) achieve acceptable performance. This is sometimes omitted in the belief that optimization can always be done later, resulting in prototype systems that are far too slow – often by an order of magnitude or more – and systems that ultimately are failures because they architecturally cannot achieve their performance goals, such as the Intel 432 (1981); or ones that take years of work to achieve acceptable performance, such as Java (1995), which achieved performance comparable with native code only with HotSpot (1999).[5] The degree to which performance changes between prototype and production system, and how amenable it is to optimization, can be a significant source of uncertainty and risk.

Design level

[edit]

At the highest level, the design may be optimized to make best use of the available resources, given goals, constraints, and expected use/load. The architectural design of a system overwhelmingly affects its performance. For example, a system that is network latency-bound (where network latency is the main constraint on overall performance) would be optimized to minimize network trips, ideally making a single request (or no requests, as in a push protocol) rather than multiple roundtrips. Choice of design depends on the goals: when designing a compiler, if fast compilation is the key priority, a one-pass compiler is faster than a multi-pass compiler (assuming same work), but if speed of output code is the goal, a slower multi-pass compiler fulfills the goal better, even though it takes longer itself. Choice of platform and programming language occur at this level, and changing them frequently requires a complete rewrite, though a modular system may allow rewrite of only some component – for example, for a Python program one may rewrite performance-critical sections in C. In a distributed system, choice of architecture (client-server, peer-to-peer, etc.) occurs at the design level, and may be difficult to change, particularly if all components cannot be replaced in sync (e.g., old clients).

Algorithms and data structures

[edit]

Given an overall design, a good choice of efficient algorithms and data structures, and efficient implementation of these algorithms and data structures comes next. After design, the choice of algorithms and data structures affects efficiency more than any other aspect of the program. Generally data structures are more difficult to change than algorithms, as a data structure assumption and its performance assumptions are used throughout the program, though this can be minimized by the use of abstract data types in function definitions, and keeping the concrete data structure definitions restricted to a few places. Changes in data structures mapped to a database may require schema migration and other complex software or infrastructure changes.[6]

For algorithms, this primarily consists of ensuring that algorithms are constant O(1), logarithmic O(log n), linear O(n), or in some cases log-linear O(n log n) in the input (both in space and time). Algorithms with quadratic complexity O(n2) fail to scale, and even linear algorithms cause problems if repeatedly called, and are typically replaced with constant or logarithmic if possible.

Beyond asymptotic order of growth, the constant factors matter: an asymptotically slower algorithm may be faster or smaller (because simpler) than an asymptotically faster algorithm when they are both faced with small input, which may be the case that occurs in reality. Often a hybrid algorithm will provide the best performance, due to this tradeoff changing with size.

A general technique to improve performance is to avoid work. A good example is the use of a fast path for common cases, improving performance by avoiding unnecessary work. For example, using a simple text layout algorithm for Latin text, only switching to a complex layout algorithm for complex scripts, such as Devanagari. Another important technique is caching, particularly memoization, which avoids redundant computations. Because of the importance of caching, there are often many levels of caching in a system, which can cause problems from memory use, and correctness issues from stale caches.

Source code level

[edit]

Beyond general algorithms and their implementation on an abstract machine, concrete source code level choices can make a significant difference. For example, on early C compilers, while(1) was slower than for(;;) for an unconditional loop, because while(1) evaluated 1 and then had a conditional jump which tested if it was true, while for (;;) had an unconditional jump . Some optimizations (such as this one) can nowadays be performed by optimizing compilers. This depends on the source language, the target machine language, and the compiler, and can be both difficult to understand or predict and changes over time; this is a key place where understanding of compilers and machine code can improve performance. Loop-invariant code motion and return value optimization are examples of optimizations that reduce the need for auxiliary variables and can even result in faster performance by avoiding round-about optimizations.

Build level

[edit]

Between the source and compile level, directives and build flags can be used to tune performance options in the source code and compiler respectively, such as using preprocessor defines to disable unneeded software features, optimizing for specific processor models or hardware capabilities, or predicting branching, for instance. Source-based software distribution systems such as BSD's Ports and Gentoo's Portage can take advantage of this form of optimization.

Compile level

[edit]

Use of an optimizing compiler tends to ensure that the executable program is optimized at least as much as the compiler can predict.

Assembly level

[edit]

At the lowest level, writing code using an assembly language, designed for a particular hardware platform can produce the most efficient and compact code if the programmer takes advantage of the full repertoire of machine instructions. Many operating systems used on embedded systems have been traditionally written in assembler code for this reason. Programs (other than very small programs) are seldom written from start to finish in assembly due to the time and cost involved. Most are compiled down from a high level language to assembly and hand optimized from there. When efficiency and size are less important large parts may be written in a high-level language.

With more modern optimizing compilers and the greater complexity of recent CPUs, it is harder to write more efficient code than what the compiler generates, and few projects need this "ultimate" optimization step.

Much of the code written today is intended to run on as many machines as possible. As a consequence, programmers and compilers don't always take advantage of the more efficient instructions provided by newer CPUs or quirks of older models. Additionally, assembly code tuned for a particular processor without using such instructions might still be suboptimal on a different processor, expecting a different tuning of the code.

Typically today rather than writing in assembly language, programmers will use a disassembler to analyze the output of a compiler and change the high-level source code so that it can be compiled more efficiently, or understand why it is inefficient.

Run time

[edit]

Just-in-time compilers can produce customized machine code based on run-time data, at the cost of compilation overhead. This technique dates to the earliest regular expression engines, and has become widespread with Java HotSpot and V8 for JavaScript. In some cases adaptive optimization may be able to perform run time optimization exceeding the capability of static compilers by dynamically adjusting parameters according to the actual input or other factors.

Profile-guided optimization is an ahead-of-time (AOT) compilation optimization technique based on run time profiles, and is similar to a static "average case" analog of the dynamic technique of adaptive optimization.

Self-modifying code can alter itself in response to run time conditions in order to optimize code; this was more common in assembly language programs.

Some CPU designs can perform some optimizations at run time. Some examples include out-of-order execution, speculative execution, instruction pipelines, and branch predictors. Compilers can help the program take advantage of these CPU features, for example through instruction scheduling.

Platform dependent and independent optimizations

[edit]

Code optimization can be also broadly categorized as platform-dependent and platform-independent techniques. While the latter ones are effective on most or all platforms, platform-dependent techniques use specific properties of one platform, or rely on parameters depending on the single platform or even on the single processor. Writing or producing different versions of the same code for different processors might therefore be needed. For instance, in the case of compile-level optimization, platform-independent techniques are generic techniques (such as loop unrolling, reduction in function calls, memory efficient routines, reduction in conditions, etc.), that impact most CPU architectures in a similar way. A great example of platform-independent optimization has been shown with inner for loop, where it was observed that a loop with an inner for loop performs more computations per unit time than a loop without it or one with an inner while loop.[7] Generally, these serve to reduce the total instruction path length required to complete the program and/or reduce total memory usage during the process. On the other hand, platform-dependent techniques involve instruction scheduling, instruction-level parallelism, data-level parallelism, cache optimization techniques (i.e., parameters that differ among various platforms) and the optimal instruction scheduling might be different even on different processors of the same architecture.

Strength reduction

[edit]

Computational tasks can be performed in several different ways with varying efficiency. A more efficient version with equivalent functionality is known as a strength reduction. For example, consider the following C code snippet whose intention is to obtain the sum of all integers from 1 to N:

int i, sum = 0;
for (i = 1; i <= N; ++i) {
  sum += i;
}
printf("sum: %d\n", sum);

This code can (assuming no arithmetic overflow) be rewritten using a mathematical formula like:

int sum = N * (1 + N) / 2;
printf("sum: %d\n", sum);

The optimization, sometimes performed automatically by an optimizing compiler, is to select a method (algorithm) that is more computationally efficient, while retaining the same functionality. See algorithmic efficiency for a discussion of some of these techniques. However, a significant improvement in performance can often be achieved by removing extraneous functionality.

Optimization is not always an obvious or intuitive process. In the example above, the "optimized" version might actually be slower than the original version if N were sufficiently small and the particular hardware happens to be much faster at performing addition and looping operations than multiplication and division.

Trade-offs

[edit]

In some cases, however, optimization relies on using more elaborate algorithms, making use of "special cases" and special "tricks" and performing complex trade-offs. A "fully optimized" program might be more difficult to comprehend and hence may contain more faults than unoptimized versions. Beyond eliminating obvious antipatterns, some code level optimizations decrease maintainability.

Optimization will generally focus on improving just one or two aspects of performance: execution time, memory usage, disk space, bandwidth, power consumption or some other resource. This will usually require a trade-off – where one factor is optimized at the expense of others. For example, increasing the size of cache improves run time performance, but also increases the memory consumption. Other common trade-offs include code clarity and conciseness.

There are instances where the programmer performing the optimization must decide to make the software better for some operations but at the cost of making other operations less efficient. These trade-offs may sometimes be of a non-technical nature – such as when a competitor has published a benchmark result that must be beaten in order to improve commercial success but comes perhaps with the burden of making normal usage of the software less efficient. Such changes are sometimes jokingly referred to as pessimizations.

Bottlenecks

[edit]

Optimization may include finding a bottleneck in a system – a component that is the limiting factor on performance. In terms of code, this will often be a hot spot – a critical part of the code that is the primary consumer of the needed resource – though it can be another factor, such as I/O latency or network bandwidth.

In computer science, resource consumption often follows a form of power law distribution, and the Pareto principle can be applied to resource optimization by observing that 80% of the resources are typically used by 20% of the operations.[8] In software engineering, it is often a better approximation that 90% of the execution time of a computer program is spent executing 10% of the code (known as the 90/10 law in this context).

More complex algorithms and data structures perform well with many items, while simple algorithms are more suitable for small amounts of data — the setup, initialization time, and constant factors of the more complex algorithm can outweigh the benefit, and thus a hybrid algorithm or adaptive algorithm may be faster than any single algorithm. A performance profiler can be used to narrow down decisions about which functionality fits which conditions.[9]

Performance profiling therefore provides not only bottleneck detection but rather a variety of methods for optimization guidance. Empirical algorithmics is the practice of using empirical methods, typically performance profiling, to study the behavior of algorithms, for developer understanding that may lead to human-planned optimizations. Profile-guided optimization is the machine-driven use of profiling data as input to an optimizing compiler or interpreter. Some programming languages are associated with tools for profile-guided optimization.[10] Some performance profiling methods emphasize enhancements based on cache utilization.[11] Other benefits of performance profiling may include improved resource management and an enhanced user experience.[12]

In some cases, adding more memory can help to make a program run faster. For example, a filtering program will commonly read each line and filter and output that line immediately. This only uses enough memory for one line, but performance is typically poor, due to the latency of each disk read. Caching the result is similarly effective, though also requiring larger memory use.

When to optimize

[edit]

Optimization can reduce readability and add code that is used only to improve the performance. This may complicate programs or systems, making them harder to maintain and debug. As a result, optimization or performance tuning is often performed at the end of the development stage.

Donald Knuth made the following two statements on optimization:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%"[13]

(He also attributed the quote to Tony Hoare several years later,[14] although this might have been an error as Hoare disclaims having coined the phrase.[15])

"In established engineering disciplines a 12% improvement, easily obtained, is never considered marginal and I believe the same viewpoint should prevail in software engineering"[13]

"Premature optimization" is a phrase used to describe a situation where a programmer lets performance considerations affect the design of a piece of code. This can result in a design that is not as clean as it could have been or code that is incorrect, because the code is complicated by the optimization and the programmer is distracted by optimizing.

When deciding whether to optimize a specific part of the program, Amdahl's Law should always be considered: the impact on the overall program depends very much on how much time is actually spent in that specific part, which is not always clear from looking at the code without a performance analysis.

A better approach is therefore to design first, code from the design and then profile/benchmark the resulting code to see which parts should be optimized. A simple and elegant design is often easier to optimize at this stage, and profiling may reveal unexpected performance problems that would not have been addressed by premature optimization.

In practice, it is often necessary to keep performance goals in mind when first designing software, but the programmer balances the goals of design and optimization.

Modern compilers and operating systems are so efficient that the intended performance increases often fail to materialize. As an example, caching data at the application level that is again cached at the operating system level does not yield improvements in execution. Even so, it is a rare case when the programmer will remove failed optimizations from production code. It is also true that advances in hardware will more often than not obviate any potential improvements, yet the obscuring code will persist into the future long after its purpose has been negated.

Macros

[edit]

Optimization during code development using macros takes on different forms in different languages.

In some procedural languages, such as C and C++, macros are implemented using token substitution. Nowadays, inline functions can be used as a type safe alternative in many cases. In both cases, the inlined function body can then undergo further compile-time optimizations by the compiler, including constant folding, which may move some computations to compile time.

In many functional programming languages, macros are implemented using parse-time substitution of parse trees/abstract syntax trees, which it is claimed makes them safer to use. Since in many cases interpretation is used, that is one way to ensure that such computations are only performed at parse-time, and sometimes the only way.

Lisp originated this style of macro,[citation needed] and such macros are often called "Lisp-like macros". A similar effect can be achieved by using template metaprogramming in C++.

In both cases, work is moved to compile-time. The difference between C macros on one side, and Lisp-like macros and C++ template metaprogramming on the other side, is that the latter tools allow performing arbitrary computations at compile-time/parse-time, while expansion of C macros does not perform any computation, and relies on the optimizer ability to perform it. Additionally, C macros do not directly support recursion or iteration, so are not Turing complete.

As with any optimization, however, it is often difficult to predict where such tools will have the most impact before a project is complete.

Automated and manual optimization

[edit]

See also Category:Compiler optimizations

Optimization can be automated by compilers or performed by programmers. Gains are usually limited for local optimization, and larger for global optimizations. Usually, the most powerful optimization is to find a superior algorithm.

Optimizing a whole system is usually undertaken by programmers because it is too complex for automated optimizers. In this situation, programmers or system administrators explicitly change code so that the overall system performs better. Although it can produce better efficiency, it is far more expensive than automated optimizations. Since many parameters influence the program performance, the program optimization space is large. Meta-heuristics and machine learning are used to address the complexity of program optimization.[16]

Use a profiler (or performance analyzer) to find the sections of the program that are taking the most resources – the bottleneck. Programmers sometimes believe they have a clear idea of where the bottleneck is, but intuition is frequently wrong.[citation needed] Optimizing an unimportant piece of code will typically do little to help the overall performance.

When the bottleneck is localized, optimization usually starts with a rethinking of the algorithm used in the program. More often than not, a particular algorithm can be specifically tailored to a particular problem, yielding better performance than a generic algorithm. For example, the task of sorting a huge list of items is usually done with a quicksort routine, which is one of the most efficient generic algorithms. But if some characteristic of the items is exploitable (for example, they are already arranged in some particular order), a different method can be used, or even a custom-made sort routine.

After the programmer is reasonably sure that the best algorithm is selected, code optimization can start. Loops can be unrolled (for lower loop overhead, although this can often lead to lower speed if it overloads the CPU cache), data types as small as possible can be used, integer arithmetic can be used instead of floating-point, and so on. (See algorithmic efficiency article for these and other techniques.)

Performance bottlenecks can be due to language limitations rather than algorithms or data structures used in the program. Sometimes, a critical part of the program can be re-written in a different programming language that gives more direct access to the underlying machine. For example, it is common for very high-level languages like Python to have modules written in C for greater speed. Programs already written in C can have modules written in assembly. Programs written in D can use the inline assembler.

Rewriting sections "pays off" in these circumstances because of a general "rule of thumb" known as the 90/10 law, which states that 90% of the time is spent in 10% of the code, and only 10% of the time in the remaining 90% of the code. So, putting intellectual effort into optimizing just a small part of the program can have a huge effect on the overall speed – if the correct part(s) can be located.

Manual optimization sometimes has the side effect of undermining readability. Thus code optimizations should be carefully documented (preferably using in-line comments), and their effect on future development evaluated.

The program that performs an automated optimization is called an optimizer. Most optimizers are embedded in compilers and operate during compilation. Optimizers can often tailor the generated code to specific processors.

Today, automated optimizations are almost exclusively limited to compiler optimization. However, because compiler optimizations are usually limited to a fixed set of rather general optimizations, there is considerable demand for optimizers which can accept descriptions of problem and language-specific optimizations, allowing an engineer to specify custom optimizations. Tools that accept descriptions of optimizations are called program transformation systems and are beginning to be applied to real software systems such as C++.

Some high-level languages (Eiffel, Esterel) optimize their programs by using an intermediate language.

Grid computing or distributed computing aims to optimize the whole system, by moving tasks from computers with high usage to computers with idle time.

Time taken for optimization

[edit]

Sometimes, the time taken to undertake optimization therein itself may be an issue.

Optimizing existing code usually does not add new features, and worse, it might add new bugs in previously working code (as any change might). Because manually optimized code might sometimes have less "readability" than unoptimized code, optimization might impact maintainability of it as well. Optimization comes at a price and it is important to be sure that the investment is worthwhile.

An automatic optimizer (or optimizing compiler, a program that performs code optimization) may itself have to be optimized, either to further improve the efficiency of its target programs or else speed up its own operation. A compilation performed with optimization "turned on" usually takes longer, although this is usually only a problem when programs are quite large.

In particular, for just-in-time compilers the performance of the run time compile component, executing together with its target code, is the key to improving overall execution speed.

See also

[edit]

References

[edit]
  1. ^ Robert Sedgewick, Algorithms, 1984, p. 84.
  2. ^ Antoniou, Andreas; Lu, Wu-Sheng (2021). Practical Optimization (PDF). Texts in Computer Science (2nd ed.). Springer. p. 1. doi:10.1007/978-1-0716-0843-2. ISBN 978-1-0716-0841-8.
  3. ^ "Performance Optimization in Software Development: Speeding Up Your Applications". Retrieved 12 July 2025.
  4. ^ Agrawal, Amit. "Maximizing Efficiency: Implementing a Performance Monitoring System". Retrieved 12 July 2025.
  5. ^ Düppe, Ingo. "Hitchhiker's Guide to Java Performance: The Past, the Present, and the Future". Retrieved 12 July 2025.
  6. ^ Mullins, Craig S. "The Impact of Change on Database Structures". Retrieved 12 July 2025.
  7. ^ Adewumi, Tosin P. (2025-08-06). "Inner loop program construct: A faster way for program execution". Open Computer Science. 8 (1): 115–122. doi:10.1515/comp-2018-0004.
  8. ^ Wescott, Bob (2013). The Every Computer Performance Book, Chapter 3: Useful laws. CreateSpace. ISBN 978-1482657753.
  9. ^ Krauss, Kirk J. "Performance Profiling with a Focus". Retrieved 15 August 2017.
  10. ^ "Profile-guided Optimization". Retrieved 12 July 2025.
  11. ^ The Valgrind Developers (2006). "5.2.2". Valgrind User Manual. Network Theory Ltd.
  12. ^ Kodlekere, Ranjana. "Performance Profiling: Explained with Stages". Retrieved 12 July 2025.
  13. ^ a b Knuth, Donald (December 1974). "Structured Programming with go to Statements". ACM Computing Surveys. 6 (4): 268. CiteSeerX 10.1.1.103.6084. doi:10.1145/356635.356640. S2CID 207630080.
  14. ^ The Errors of TeX, in Software—Practice & Experience, Volume 19, Issue 7 (July 1989), pp. 607–685, reprinted in his book Literate Programming (p. 276).
  15. ^ "Premature optimization is the root of all evil". hans.gerwitz.com. Retrieved 2025-08-06. Hoare, however, did not claim it when I queried him in January of 2004
  16. ^ Memeti, Suejb; Pllana, Sabri; Binotto, Alécio; Ko?odziej, Joanna; Brandic, Ivona (26 April 2018). "Using meta-heuristics and machine learning for software optimization of parallel computing systems: a systematic literature review". Computing. 101 (8). Springer Vienna: 893–936. arXiv:1801.09444. Bibcode:2018arXiv180109444M. doi:10.1007/s00607-018-0614-9. S2CID 13868111.

Further reading

[edit]
英雄联盟msi是什么 http什么意思 什么车最长脑筋急转弯 腰椎膨出是什么意思 世界上最长的英文单词是什么
消防队属于什么编制 卵圆孔未闭挂什么科 什么减肥药最管用 大三阳吃什么药好 4月23日是什么日子
大义是什么意思 胆囊大是什么原因 脸色发红什么原因 梨状肌综合症吃什么药 ips屏幕是什么意思
阳痿吃什么中成药 月建是什么意思 多囊卵巢综合征吃什么药 头发秃一块是什么原因 冷漠是什么意思
evol是什么意思hcv9jop8ns0r.cn 505是什么意思hcv8jop2ns2r.cn 夜间盗汗是什么原因hcv7jop6ns3r.cn 制片人是做什么的hcv8jop5ns6r.cn 塔丝隆是什么面料hcv8jop7ns8r.cn
胎停了有什么症状hcv9jop2ns2r.cn 经常打屁是什么原因hcv8jop9ns5r.cn 象牙白适合什么肤色hcv8jop6ns6r.cn 先父什么意思hcv9jop5ns1r.cn 鼻子出血吃什么药hcv9jop0ns8r.cn
什么是华盖hcv8jop0ns1r.cn lp 什么意思hcv9jop6ns0r.cn crp高是什么感染hcv8jop3ns8r.cn 喜上眉梢是什么意思hcv9jop2ns7r.cn 丘疹性荨麻疹用什么药hcv7jop9ns4r.cn
鹅翅膀下垂是什么原因fenrenren.com 西洋菜俗称叫什么shenchushe.com 深紫色配什么颜色好看hcv8jop0ns5r.cn 下体痒是什么原因hcv9jop5ns1r.cn 阑尾粪石是什么意思hcv8jop1ns2r.cn
百度