Performance Analysis of the data structure of python

I. Introduction

  - Now everyone differences between Big O algorithms and different functions with understanding. Goal of this section is to tell you Python lists and dictionaries Big O performance operations. Then we will do some time-based experiments to illustrate the expenses and benefits of using these data structures for each data structure. It is important to understand the efficiency of these data structures, because they are the basis module of this blog implement other data structures are used. In this section, we will not explain why this performance. In the latter post, you will see a list of dictionaries and some possible implementations, and how performance is implementation-dependent.

Second list:

  - python designers in the realization of the list of data structures have many options. Each of these options are likely to affect the performance of the list of operations. To help them make the right choices, the way they view the most frequently used list data structure, and optimize the implementation, in order to make the most common operation is very fast.

- In the list of actions of a very common programming task it is to increase a list. We immediately think of two ways to create a longer list, you can use the append method or the concatenation operator. But higher efficiency that these two methods do. This is very important for you, because it can help you to improve the efficiency of your own program by selecting the appropriate tool.

    - Let's look at four different ways, we can generate a list of n from zero digits. First, we will try to create a for loop through the list, and then we will use append instead of stitching. Next, we use the list generator to create a list, and finally, and most obvious way packaging range function by calling the list constructor.

 

 - Let's use when running average of the four timeit module to calculate how much a long way:

    - timeit Module: This module may be used to perform the test section of python code rate / duration.

    - Timer class: class is length / time timeit python module designed to measure code execution speed. Prototype: class timeit.Timer (stmt = 'pass', setup = 'pass').

      - stmt argument: that the upcoming test code block statements.

      - setup: Run's code block when the desired setting.

    - timeit function: timeit.Timer.timeit (number = 100000), the function returns the average time statement execution block number of times.

    - Case:

 

- When using the average run timeit module calculates the above-mentioned four ways of how much longer:

 

Note: You can see above the actual cost of the time, including some call the function, but we can assume the function call overhead in four cases is the same, so we still get a meaningful comparison. Thus, the splicing operation requires 6.54 milliseconds string is not accurate, but the string concatenation function requires 6.54 milliseconds. You can call the test of time required to empty function, and it is subtracted from the above figures. The remaining operations based on other lists you can also use timeit measured to calculate the average time-consuming.

  - Method list of related operations are to be packaged, the underlying algorithm we do not need to analyze time-dependent operation, we direct a given time based on the list table complexity of the operation below for reference:

 

III. Dictionary

  - the second major python is a dictionary data structure. As you may recall, different dictionaries and lists, you can access the dictionary project, rather than by key position.

  - Dictionary of time complexity:

Guess you like

Origin blog.csdn.net/sinat_38682860/article/details/93776373