What is code optimization?

10,705

Solution 1

Optimization is a very broad term. In general it implies modifying the system to make some of its aspect to work more efficiently or use fewer resources or be more robust. For example, a computer program may be optimized so that it will execute faster or use less memory or disk storage or be more responsive in terms of UI.

Although "optimization" has the same root as "optimal", the process of optimization does not produce a totally optimal system: there's always a trade-off, so only attributes of greatest interest are optimized.

And remember:

The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet. (Michael A. Jackson)

Solution 2

Optimization is the process of modifying a system to make some aspect of it work more efficiently or use fewer resources.

In your case refers mainly to 2 levels:

Design level

At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from a good choice of efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design. In some cases, however, optimization relies on using fancier algorithms, making use of special cases and special tricks and performing complex trade-offs; thus, a fully optimized program can sometimes, if insufficiently commented, be more difficult for less experienced programmers to comprehend and hence may contain more faults than unoptimized versions.

Source code level

Avoiding bad quality coding can also improve performance, by avoiding obvious slowdowns. After that, however, some optimizations are possible which actually decrease maintainability; some, but not all of them can nowadays be performed by optimizing compilers. For instance, using more indirection is often needed to simplify or improve a software, but that indirection has a cost.

Solution 3

Code optimization is making code run faster. There are two primary ways of doing this:

1) Squeezing more work into less cycles. Figure out where the code is doing an extra copy or if there is a branch in a tight loop. This is optimizing in the small.

2) Making your algorithms scale better. You may have heard of "Big O" notation. This is making an algorithm degrade much less quickly with large sets of data.

For instance, if you naively search a phone book for a name you will start on page 1 and read all the names until you find the one you are looking for. This will take a number of instructions scaled by the number of names in the phone book. We call this O(n). Now think about how you really search the phone book. You open to some place toward the middle and see which side the name you are looking for is on. This is called a binary search and scales at the logarithm of the number of names. We call this O(logn). It's much faster.

Remember the first rule of optimization: Measure first. Many man years have been spent optimizing code that wasn't run very much.

Solution 4

When doing code optimization, you take a metric on your code and try to make it more efficient. The metric usually refers to a scarce resource.

Here are common metrics

  • Execution speed (usually the first that comes to mind when saying optimization)
  • Memory consumption
  • Executable size (on embedded systems it can be important)
  • Database access
  • Remote service access (Make it less chatty, caching..)
  • Simplicity, readability, maintainability of the code

After optimization the code should give the same result.

The problem is that you have to make choices. Execution speed often comes with more memory consuption...

You should also alwas consider optimization globally. Having a gain of 10ms in a loop when you then spend 1000ms waiting for a web service is totaly useless.

Solution 5

Optimization has two main purposes:

  • getting your software use less resources, e.g., run faster, be smaller, use less RAM, less hard disk space both when running and when storing documents, less network access, ...

  • getting your software be more maintainable, by refactoring it.

You don't need to optimize as long as no related issue has been raised: It is far more difficult to debug optimized code than to optimize correct code.

Share:
10,705
Abdullah BaMusa
Author by

Abdullah BaMusa

Updated on June 27, 2022

Comments

  • Abdullah BaMusa
    Abdullah BaMusa almost 2 years

    When said this code need some optimization, or can be some how optimized, what does that mean? which kind of code need optimization? How to apply optimization to the code in c#? What the benefits from that?

  • peterchen
    peterchen about 15 years
    I'd broaden "runs faster" to "consumes less ressources".
  • Steve Rowe
    Steve Rowe about 15 years
    I think that is called refactoring. It's not quite the same thing.
  • Abdullah BaMusa
    Abdullah BaMusa about 15 years
    This is useful but more explanation would be appreciated From phone book , Suppose I have thousands of names and every name have one or more phone, in searching of every name with his all phones, Must loop for every name and pick it’s phone. Where to optimize here. How to apply Big O.
  • SmacL
    SmacL about 15 years
    I tend to agree with Peter Chen that the principal resource that gets exhausted these days is not CPU cycles. IMO it is network bandwidth, hence all the effort optimizing AV codecs etc...
  • Steve Wortham
    Steve Wortham over 12 years
    I prefer the quote from Donald Knuth, "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%." That critical 3% is a particularly important (and often ignored) part of this quote. It's the low hanging fruit. Sometimes the greatest payoffs can be realized with minimal effort. You just have to spend a bit of time measuring and analyzing to find those bottlenecks.