On the execution efficiency of code (1): The algorithm is the key

Source: Internet
Author: User
Tags sort

When I saw such an article in a blog park some time ago, the brother talked about the key to the efficiency of the program is "short". "The shorter the program, the less executable it is, the more efficient it will be," he says, "and when writing a program," Try to improve our algorithm, and the most important one in the algorithm is to reduce the statement. This sentence appears to be correct on the surface, but I think the performance of this problem can not be "short" this way to think, otherwise it will enter some misunderstandings. I sorted out the idea and I hope I can discuss the problem in detail from several aspects.

First, if you say "Short code is efficient," What do You call "short"? Let's think of it as "less code." If "code less" can produce "high efficiency", then I think that there are some other conditions, such as:

The code is executed in exactly the same order

Each line of code has the same efficiency

However, for programmers who use advanced languages, these two articles are basically not tenable because:

There are conditional jumps in the code, such as loops, so a short piece of code might end up with more "times" than a complex piece of code. This is a very common situation, not a "special case" as the brother said, "two or three lines of code write dead loops."

The efficiency of each line of code is almost impossible. Imagine a i++ and a method call, how can their performance cost be the same? Furthermore, this feature is not specific to the "advanced language", even CPU instructions (we'll talk about this later).

In fact, this is the current programming work can not avoid the status quo, that is, high-level language does not directly reflect the implementation of the machine process. At the same time, we cannot judge the efficiency of the program from the brevity of the surface of the code. As the friend said, one of the key factors affecting the program is the choice of algorithms, but I don't agree with him. The key to algorithm optimization lies in "reducing statements". To some extent, choosing an efficient algorithm is really to reduce the "total" of instruction execution, but it is not done as the article says by "Less variables", "less judgment," but rather a drastic logical change to reduce the overall operation.

In fact, it's easy to cite code brevity, but poor performance algorithms. Take the most common sort algorithm, bubble sort is not easy:

public void BubbleSort(int[] array)
{
   for (int i = array.Length - 1; i >= 0; i--)
   {
     for (int j = 1; j <= i; j++)
     {
       if (array[j - 1] > array[j])
       {
         int temp = array[j - 1];
         array[j - 1] = array[j];
         array[j] = temp;
       }
     }
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.