Uppercase "O" symbol

Source: Internet
Author: User

Typically you will develop a test database. It may only be accessible to 10 people, so your programming and testing work can be done smoothly. Once the development and QA ' d (through quality inspection) is completed, it will be officially launched (released) and people begin to visit the site. Over a year or so, the site's response is getting slower and faster. After adding more database servers that didn't work, your system administrator spent his "vacation" at My.cnf ' s, and even added a lot of memory to the system. It sounds like a program (code) structure that restricts access to high-volume access to a Web site. System to 40000 people user list information can not complete sorting or processing, or array of elements more than 1 million items, even simple check function slows the access speed. Why? What the hell is going on?

Most PHP developers have never heard of Big-o, but Big-o may be more important than any other part in web development (well, perhaps in addition to system and OS Programming:)). The code block may be executed by only 10 users a day, but the same code is likely to run thousands of times per minute.

What is Big-o?

The Big-o or Landau notation (Landau notation) is a mathematical principle that defines a function scale that studies the function of functions in the context of parametric changes. In other words: Big-o will tell you that a strpos () function has a time-to-run relationship when it receives a string parameter of 10 and 1 million character lengths, respectively. It will not tell you the definite run time, but it (Big-o) will tell you the trend relationship between parameters and run time.

Well, maybe you'll ask: What's going on here?

Specifically, ...

Suppose you have a function that sorts the data in the array according to certain criteria. For example, sort by the alphabetical order of the last name. Everyone can imagine that 10 users would be much quicker to sort than 10 million users. But how much faster or better: how long will the latter take? Once you know the answer to the question, you will be aware of the speed at which your code (System) is running in a large number of user-accessed environments. You will understand the primary bottleneck of your system: your code or your hardware? In other words, you start to realize the scalability of the system.

Example 1:

Suppose you have a function IsEven ():

function IsEven ($iNumber) {//Method a    return (($iNumber & 1) = = 0);}

This function will determine if a numeric value is an even number, and if so, the return value is true, otherwise the return value is false.

For this function, it doesn't matter whether the parameter passed in is int (1) or int (100000005), and if the function always uses the same time to determine the argument and return the value.

Now, suppose we change the function to the following form:

</pre><pre name= "code" class= "PHP" >function isEven ($iNumber) {//method B    $bEven = true;    for ($i =0; $i!=abs ($iNumber), $i + +) {    if ($bEven = = True) {        $bEven = false;    } else {        $bEven = true;    }    return $bEven;}

Of course, this notation doesn't make much sense. But I occasionally see code that uses inefficient algorithms, so the above example is enough to justify my point of view.

When the function (method B) receives a parameter that is int (1), the FOR Loop statement executes only once. The function runs quickly and no problems arise. However, when we want to determine if the value 1000000 is an even number (the received parameter is int (1000000)), we may encounter a bit of trouble because the for loop must perform 1 million functions to return the judging result.

If we put the function (method a) into a chart, the X-axis represents the parameter $iNumber (range: 1 ~ +), and the Y-axis represents the time it takes for the function to determine the result, then we will get a graph of the horizontal line. This means that the time it takes to get a verdict is always the same regardless of what int values the function receives. This can be marked as O (1), and the performance of this function is optimal. No matter what parameters you pass in, the time it takes the function to get the results is always the same. Code (function) extensibility is a dream come true:)

Now let's analyze the function (method B) together. The same X-axis indicates that the parameter $iNumber, and we will see an ascending line. We use O (n) to mark this function (n means the parameter itself or the number of cycles, the scale of the function operation).

The numerical value of the parameter is directly related to the function operation time.

Example 2:

function Getmoduleinfobyid ($iModuleID) {//Method C    foreach ($this->_amoduleinfo as $aInfo) {        if ($aInfo [' id '] = = $iModuleID) return $aInfo;    }    return null;}

The function (method c) returns the corresponding module information when the parameter ID can be matched on the traversed object, otherwise a null value is returned. This is a very common way to find data by iterating over the array. Such functions can be labeled O (N), where N is the number of module information and the number of $_amoduleinfo elements in the collection.

How do we optimize the performance of this type of function?

It is a good choice to implement the ability to find data without using traversal (iteration). Improving computational efficiency by optimizing code structures (reducing the slope of o-line) is always a hassle:) To do this, we need to make some modifications to the function (method C). For example, if we put the collection element in an associative array and put the element's ID as key (key), as follows:

function Getmoduleinfobyid ($iModuleID) {//Method D    if (Isset ($this->_amoduleinfo[$iModuleID]))        return $this- > _amoduleinfo[$iModuleID];    return null;}

In the function (method D), we don't have to worry about how long the $_amoduleinfo array is. This function does not need to perform a traversal operation, and it does not matter whether it is one or 1 million moduleinfo elements.

However, we need to pay attention to the following points:

What does function isset () work? Can it be labeled with O (1) or O (N)? Or worse? You must know the performance of the function (including the function it calls).

Your check for function performance is based on Php-level. Based on Cpu-level, the result may be another thing. For example, the CPU handles numeric arrays much faster than processing associative arrays (the former does not involve hash lookups).

However, there is little difference in the time in PHP for working with numeric arrays and associative arrays.

The difference of various o

There are many and very complex types of O ' s:) Keep Looking down oh, look at the following O ' s can match the function you write. You may never need an exact formula, but as long as you know that your function performance model is similar to O (log n), rather than O (n), you already know a lot ...

Here is a list of o-functions, sorted by performance favorably to inferior:

O (1)

Constant time, no matter what the parameter is, the time of the function operation is always the same.

O (log N)

The logarithmic time, the function operation time grows rapidly at the beginning, but after a while, the function operation time grows slowly. This is good news when you know there are many objects to process (such as users, articles, comments sort).

O (n) Linear (linear)

The size of the parameter is directly related to the time of the function operation. Twice times the parameter size means twice times the computation time.

O (n^2) quadratic (SQ)

Most of the time, this means that two iterators with the same data set will participate in the operation (like many of the non-optimized sorting algorithms).

O (n^3) Cubic (cubic)

As with quadratic (square), just a little extra loop. This is disastrous for the operational performance of the function.

O (n!) Factorial (factorial)

These graphs are almost straight up (for example, the factorial of 10 is 3628800, which means a lot of iterative operations). If your function performance model is similar to this, it is recommended to override the function, which performs poorly in its operation.


Description : Picture Source--Http://nl.wikipedia.org/wiki/Bestand:Exponential.png

Where the red line is an O (n) linear graph, the Blue line is an O (n^3) cubic chart, and the Green Line is an O (2^n) index graph.

Note that although the Y-value (time) of the Green Line is optimal within the x<=8 range, the Y-value (time) of the Green Line is the worst in the x>10 range.

When we read the data, we statistically query at least half the array elements. Get a random number in the range of 0 to 100, with a 50% chance this random number is less than 50, and there is a 50% chance that the random number is greater than or equal to 50. However, Big-o should consider the worst case scenario. So sequentially searching for an array of length N should be labeled O (n), not O (N/2).

Big-o Precautions

Please note that the O (*) tag does not say anything about the speed of the operation itself. It just tells you that the operation speed and parameters of the function are relative. For example, in some cases, a function of O (n) performance may be faster than a function of O (log n) or O (1) performance. However, there will be a turning point, and the other functions are more computationally fast. You need to find a balance between functions, algorithmic complexity, and computational speed. Test the operation time (performance) of your function repeatedly and compare the results. Their computational performance may not be as bad as you think (perhaps worse than you thought). The code in the test will tell you the result, but you don't know it without testing:)

Summary

Learn about your functions and the performance of their operations. But make sure the performance of the optimization function is not excessive. It is perfect to have an O (1) level of computing performance for each function, but in practice there is no end to optimization, and there is always a need to find a balance between development time, budget and speed, and optimization work.



1. This article is translated by Mathew, and is examined by the programmer architecture

2. This article is translated from https://www.adayinthelifeof.nl/2009/12/21/big-o-notation/

The original Joshua Thijssen December

3. Reprint Please be sure to indicate this article from : Programmer Architecture (No.:archleaner )

4. More articles please scan the code:

Uppercase "O" symbol

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.