Js from time to explain the complexity and space complexity

1. blog background

Today there are colleagues in the checking code, because of the function write performance is not very good, was reconstructed to fight back, and think very thin fear, and today we share one with js explain the complexity and time complexity of the space blog

2. The complexity of representation

We have seen before, so you might see a bunch of things

T(n) = O(f(n)) 
S(n) = O(f(n)) 

This is called the Big O notation, where T represents the total time required to perform the algorithm

The total space required for the algorithm represented S

f (n) represents the total number of code execution

for example

function go(n) { 
  var item = 0;      // 这里执行了一次
  for (var i = 0; i < n; i++) {   //这里执行了N次
    for (var j = 0; j < n; j++) {     //这里执行了n*n次
      item = item + i + j;     //这里执行了n*n次
    }
  }
  return item;  //这里执行了一次
}

So on top of this code is 1+n+n*n*2+1=2+n+2n²

That is T (n) = O (f (2 + n + 2n²))

Then he said before the time complexity is the tendency to see a time code execution, so that the N, which is relatively large when those constants will not achieve a decisive role, so this time we ignore these constants, here is an example of the code for a single segment, where the order of the maximum cycle look on it

Therefore, the last time complexity of this code is T (n) = O (n²)

You can think about the plot data in square

3. Time Complexity

Definition 3.1 Time Complexity

First, what is the time complexity, time complexity of this definition if no contact before too, you might think he represents the time a code is executed, it is not true, the time complexity of the algorithm that is an execution time of the algorithm according to a growing trend data scale, not to say that specific time code execution

3.2 Several common time complexity

  • Simple O (n)

    for (var i = 0; i < n; i++) {   
      sum += i;   
    } 

Straightaway, code execution time is completely controlled by N, so that T (n) = O (n)

  • Of course, there is a simpler O (1)

      function total(n) { 
            console.log(1)
      } 

No matter how, without any parameters that affect this function, the code on the bin go again, this code is represented by T (n) = O (1)

  • T(n) = O(n²)

The above example has been said that one of the two cycles, the time complexity of calculation in the case of lifting a plurality of time complexity of the code

function go(i) {
  var sum = 0;
  for (var j = 0; j < i; j++) {
    sum += i;
  }
  return sum;
}
function main(n) {
  var res = 0;
  for (var i = 0; i < n; i++) {
    res = res + go(i); // 这里是重点
  }
}

In the top of the second paragraph of the code kind of code inside the first piece of code calls, so that this is the code inside

go:(1+n)

In the main function inside the time is(1+n*go)=(1+n+n*n)

Therefore, the final time complexity is T (n) = O (n²)

More than 3.3 time block code in complexity

Description of the top distance T (n) = O (n²), is a function calls another function in the inside, this is the time complexity of multiplying two functions.

There is another case, that is to say in how a function inside the code block, but did not call each other, so when this situation, we only need to take the complexity of the largest blocks of code on it

For example,

        function go(n) { 

          for (var i = 0; i < n; i++) {
            for (var j = 0; j < n; j++) {
              console.log(1)
            }
          }


          for (var i = 0; i < n; i++) {
           console.log(2)
          }
        }
        

The upper piece of code, the first code is a two cycle, by way of example, we have learned that the upper complexity is

Below this code, n

So when this case, when N approaches infinity when N is n² not achieve a decisive role, the time complexity of the upper piece of code is to take the maximum value n²

3.4 pairs of order and adding the situation

var i = 1;
while (i <= n) {
        i = i * 10;
}

In this code, you can see while inside, as judged by the condition of each * i is 10, so that it is not the number of the last cycle n times, but that tenth n times, so that this time time complexity = n-10i,
I = logN

It is concluded that the time complexity of T (n) = O (logn)

Then there is a situation that is by changing the variables to increase the number of cycles, empathy is to increase the time complexity

There are other cases such as the time complexity of adding

function go(m,n) {

  for (var i = 0; i < n; i++) {
    console.log(1)
  }

  for (var i = 0; i < m; i++) {
    console.log(2)
  }

}

Look at the top this period, this code inside a function inside there are two cycles, but there are two formal parameters, we can not know n and m in the end who is great who is small, so that the time code at this time complexity is O (m + n)

4. The spatial complexity

Definition of complexity 4.1 space

On top of that so a lot of the complexity of the time, compared to you have a better understanding, and on the name of view, the time complexity is the tendency to see the execution time of the code, then the same token, the space complexity refers to the occupation trend memory

4.2 Common space complexity

Space complexity is no time complexity is so complicated, so few common

After all, I feel that no one has been circulating a variety of tricks declare variables it. . .

If so, please killed. . . .

  • O (1)

    let a = 1;
    let b = 1;
    let c = 1;
    let d = 1;

Very simple, O (1)

  • O (n)

    let arr =Array(n)

See this code, the code creates an array of length n, it is clear that the length of the array is determined according to n, so that the
O (n)

It should explain, there is no use cycle, because if not kept in the loop inside to declare variables, only changing the value of the case is not shelf space complexity

  • O (n²)

    let arr=[]
    for (var i = 0; i < n; i++) {
    arr[i]=i
    for (var j = 0; j < n; j++) {
    arr[i][j]=j
    }
    }

How, a fierce look this code is not very exciting, I think if this is the case, the general will be clubbed to death a. . .

Optimization of complexity

I would like to say before optimization Pirates a picture for everyone to look at the graph of various complexity, facilitate everyone has an intuitive understanding

To give an example of a relatively simple optimization

console.time('a')
function go(n) {
      var item = 0;
      for (var i = 1; i <= n; i++) {
        item += i;
      }
      return item;
}
console.timeEnd('a')

console.time('b')
function go2(n) {
  var item = n*(n+1)/2
  return item;
}
console.timeEnd('b')

go(1000)
go2(1000)

We can look at what print

I hope you forgive me not good at math, I remember before seen an example of arithmetic sequence of number, we can not think of another example of performance optimization

After reading it I hope you can understand these concepts, sometimes this stuff is really important to find a relatively high profile example

Fibonacci is in turn equal to the first two and the third from the start

Fibonacci defined

function Fibonacci(n) {
    if (n <= 1 ) {
        return n;
    } else {
        return Fibonacci(n - 1) + Fibonacci(n - 2);
    }
}

console.time('b')
Fibonacci(????)
console.timeEnd('b')

Interested can try to print it, look at the time, but probably 50 times when you have the browser should not responded, please look up specific plot. . . .

These are some of my understanding of the complexity of time and space complexity, inadequate or wrong, I like to point out to

    o(* ̄▽ ̄*)ブ

Guess you like

Origin www.cnblogs.com/netUserAdd/p/11298263.html
Recommended