2 Timeit module, data structure in Python

Source: Internet
Author: User
Tags python list

1. Timeit Module: Code Event measurement Module
The Timeit module can be used to test the execution speed of a small piece of Python code.


class Timeit. Timer (stmt='pass', setup='pass', Timer=<timer function >)    A timer is a class that measures the speed of a small piece of code execution. The    stmt parameter is the code statement to be tested (statment); the setup    parameter is the setting required to run the code; The    timer parameter is a timer function that is related to the platform. 

Timeit. Timer.timeit (number=1000000)    The object method in the timer class that tests the execution speed of a statement. The    number parameter is the test count when the code is tested, which defaults to 1 million times.    method returns the average time spent executing code, and the number of seconds for a float type. 

2. Event efficiency for Python list operations (1) List generation method comparison

defT1 (): Li= []     forIinchRange (10000): Li.append (i)defT2 (): Li= []     forIinchRange (10000): Li= Li +[i]#Li + = [i]deft3 (): Li= [I forIinchRange (10000)]deft4 (): Li= List (Range (10000))defT5 (): Li= []     forIinchRange (10000): Li.extend ([i]) fromTimeitImportTimertimer1= Timer ("T1 ()","From __main__ import T1")        #"T1 ()" To detect the function (str),        #"from __main__ import T1" which file is imported fromPrint("append-->", Timer1.timeit (1000))        #number parameter is the test count when testing the codeTimer2= Timer ("T2 ()","From __main__ import T2")Print("+ --", Timer1.timeit (1000)) Timer3= Timer ("T3 ()","From __main__ import T3")Print("[I for I in range (10000)]-->", Timer1.timeit (1000)) Timer4= Timer ("t4 ()","From __main__ import T4")Print("List (range (10000))--", Timer1.timeit (1000)) Timer5= Timer ("T5 ()","From __main__ Import T5")Print("List (Extend (10000))- -", Timer1.timeit (1000))

(2) Pop operation test
x = Range (2000000) Pop_zero= Timer ("x.pop (0)","From __main__ import x")Print("Pop_zero", Pop_zero.timeit (number=1000),"seconds") x= Range (2000000) Pop_end= Timer ("X.pop ()","From __main__ import x")Print("Pop_end", Pop_end.timeit (number=1000),"seconds")#(' Pop_zero ', 1.9101738929748535, ' seconds ')#(' Pop_end ', 0.00023603439331054688, ' seconds ')

To test the pop operation: from the results, the last element of the pop is much more efficient than the first POP element

You can try the list append (value) and insert (0,value), that is, a back insert and a front insert???

3. Time complexity of list and dict

4. Introduction of data structure

How do we use the types in Python to save student information for a class? What if you want to get their information quickly through the student's name?

Lists and dictionaries can store student information for a class, but when you want to get information about a classmate in a list, you need to iterate through the list with an O (n) time complexity.

When using the dictionary storage, students ' names can be used as the keys of the dictionary, student information as the value, and then the query without traversing can quickly get to the student information, its time complexity of O (1).

Such a way of data organization, we call him the structure of the organization of different data structures, the structure of a different structure to solve a set of how to save the form of what is the style of its preservation

In the above questions we can select a list or dictionary in Python to store student information. Lists and dictionaries are two of the data structures that Python built to encapsulate for us.

5. Data structure Concept

Data is an abstract concept that is categorized into basic types in the programming language. such as: Int,float,char and so on. Data elements are not independent, there are specific relationships, and these relationships are structures.

The data structure index is the relationship between the elements in the object.

Python gives us a lot of out-of-the-box data structure types that are self-defined and do not require our own definition of data structures called Python's built-in data structures, such as lists, tuples, dictionaries, and so on.

Some data organization, the Python system does not have a direct definition, we need to define how to implement this data organization, which is called Python extension data structure, such as stacks, queues and so on.

6, the difference between the algorithm and data structure

Data structures only statically describe the relationships between the elements of a dataset.

Efficient programs require the design and selection of algorithms based on data structures.

program = data structure + algorithm

Conclusion: The algorithm is designed to solve the practical problem, and the data structure is the problem carrier that the algorithm needs to deal with.


7. Abstract data type

An abstract data type (ADT) means a mathematical model and a set of operations defined on this mathematical model. That is, the data type and the data type of the operation is bundled together to encapsulate.

The purpose of introducing abstract data types is to separate the representations of data types and the implementation of operations on data types from the references to these data types and operations in the program.

There are five of the most commonly used data operations:

    • Insert
    • Delete
    • Modify
    • Find
    • Sort

2 Timeit module, data structure in Python

Related Article

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.