Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Program optimization
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Strength reduction== 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 (programming language)|C]] code snippet whose intention is to obtain the sum of all integers from 1 to {{var|N}}: <syntaxhighlight lang="c"> int i, sum = 0; for (i = 1; i <= N; ++i) { sum += i; } printf("sum: %d\n", sum); </syntaxhighlight> This code can (assuming no [[arithmetic overflow]]) be rewritten using a mathematical formula like: <syntaxhighlight lang="c"> int sum = N * (1 + N) / 2; printf("sum: %d\n", sum); </syntaxhighlight> 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 {{var|N}} were sufficiently small and the particular hardware happens to be much faster at performing addition and [[Loop (computing)#Loops|loop]]ing operations than multiplication and division.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)