Stick to original output, click on the blue word to follow me
Author: Qing Han blog: OSChina, Cloud + community, Zhihu and other major platforms.
directory
- Tuples and lists
- 1. Performance analysis of tuples and lists
- 2. Why are lists the most common in Python?
- Timeit has a Timer class
- 4. There is also a direct method to use timeit, timeit.timeit()
- 5. What’s the difference between these two methods?
- 2. Named tuples
- What are the characteristics of named tuples?
Tuples and lists
1. Performance analysis of tuples and lists
Tuples and lists are used to store data. Which is faster when querying in a tuple or a list?
Calculate how long it takes to create tuples and lists: ipython uses the timeit command.
Calculation time module introduction:
import timeit
#timeit.timeit
Copy the code
You can use this module to test the performance of functions.
Install ipython: PIP install ipython
Ipython is an interactive environment, just as we type Python into it. It just has a layer of encapsulation that makes it a little more useful than the Python interactive environment.
Ipython has a command called timeit, which can be followed by a Python expression.
For example, define a list following:
After typing this command, a line of time is returned. This initializes a list in memory. As you can see, it was created ten million times in 48.4ns
As you can see, creating a tuple is much faster than creating a list.
Tuples are more than three times faster than lists. In memory, when we create a list, we divide a region and give a region to the list to store values. Initialization, for example, has 20 places left for it to store values in this list.
When the storage reaches a point where the Python interpreter detects that the list is about to fill up, it makes an expansion of the list.
Expand the storage capacity to 200, and when the storage capacity reaches 150, it finds that the storage capacity is almost full again, and it will continue to expand the storage capacity.
As more data is added to the list, the bottom layer adds to the list.
Initializing a tuple, also 10 million times, takes 12.8ns
A tuple is an immutable type.
For example, if you define a tuple with three elements, the Python interpreter divides it into three cells when storing it.
There are only three pieces of data in there, that’s all, so tuples take up less memory than lists.
Using a list to store 50 items of data and a tuple to store 50 items of data, the tuple takes up much less memory than the list.
2. Why are lists the most common in Python?
Because lists are flexible, you can keep adding elements to them. If the element is fixed, use a tuple.
Timeit has a Timer class.
Take a look at the source code for this class:
Timer =default_timer indicates the number of times to create a list, tuple, etc.
Look at the source code, the default is ten million times:
import timeit This module can be used for performance analysis
def func() :
for i in range(10) :print(i)
# This object has a method called timeit
res = timeit.Timer(func).timeit(100) # Pass in the func function, run it 100 times, and return a time
# timeit.timer (func).timeit(100) # timeit.timer (func).timeit(100
# put in quotes
print(res)
Copy the code
You can see that the time required to run 100 times is 0.0043269999999999975
4. There is also a direct method to use timeit, timeit.timeit()
import timeit This module can be used for performance analysis
def func() :
for i in range(10) :print(i)
# This object has a method called timeit
# res = timeit.timer (func).timeit(100) # res = timeit.timer (func).timeit(100
# timeit.timer (func).timeit(100) # timeit.timer (func).timeit(100
# print(res)
res2 = timeit.timeit('[1, 2, 3]')
print(res2)
Copy the code
The function of this module: we write the function function, you can use it to measure the speed of the function function, the execution of about how long.
The default is 10 million, and the result is as follows:
An error will occur if the list is passed without quotes:
Prompt cannot be called!
5. What’s the difference between these two methods?
They’re actually the same thing.
2. Named tuples
Tuples perform much better than lists. Tuples and lists are evaluated by subscript indexes.
Subscript index values are not very user-friendly, if I know that the data is stored in a tuple, but I don’t know exactly where it is stored in the subscript. In this case, to find this element, you have to find the index first, and then go to get the index, which is very inconvenient.
Dictionaries have an advantage in this respect. The data is stored in the dictionary, and the value can be found by the key. Dictionaries have certain advantages and disadvantages over tuples and lists.
Named tuples can be used to value tuples like dictionaries.
For example, there is a tuple that holds three pieces of data:
To create a namedtuple, you use one of Python’s built-in modules, from collections import namedtuple
import timeit This module can be used for performance analysis
from collections import namedtuple
# namedtuple is a function that creates named tuples
def func() :
for i in range(10) :print(i)
# This object has a method called timeit
# res = timeit.timer (func).timeit(100) # res = timeit.timer (func).timeit(100
# timeit.timer (func).timeit(100) # timeit.timer (func).timeit(100
# print(res)
res2 = timeit.timeit('[1, 2, 3]')
print(res2)
# named tuple
# If you know the exact location of the storage, you can use the subscript to value it. For example, tu = [0]
If I don't know where the name is stored, I can't get it by subscript. Okay
# Named tuples can be evaluated like lists
student_info = namedtuple('info_tuple'['name'.'age'.'gender']) # this function takes two arguments. The first argument is the name of the type that created the named tuple.
# for the second argument, pass a list
The list contains a name to create the named tuple, for example, the first element is named name
# This function call takes 2 arguments and returns an object. This object is called student_info
Student_info creates a named tuple with this object
tu = student_info('qinghan'.18.'nv')
print(tu)
Copy the code
This TU is a named tuple.
Student_info creates the namedtuple type using the namedtuple function: namedtuple(‘info_tuple’,[‘name’,’age’,’gender’]).
An object student_info is returned
Student_info passes in the tuple that defines the element and returns the named tuple.
What are the characteristics of named tuples?
It can be evaluated like a dictionary, by the corresponding key, to find the corresponding value. Named tuples are used more like objects.
Use: name the tuple.name
We can find the value of name:
import timeit This module can be used for performance analysis
from collections import namedtuple
# namedtuple is a function that creates named tuples
def func() :
for i in range(10) :print(i)
# This object has a method called timeit
# res = timeit.timer (func).timeit(100) # res = timeit.timer (func).timeit(100
# timeit.timer (func).timeit(100) # timeit.timer (func).timeit(100
# print(res)
res2 = timeit.timeit('[1, 2, 3]')
print(res2)
# named tuple
# If you know the exact location of the storage, you can use the subscript to value it. For example, tu = [0]
If I don't know where the name is stored, I can't get it by subscript. Okay
# Named tuples can be evaluated like lists
student_info = namedtuple('info_tuple'['name'.'age'.'gender']) # this function takes two arguments. The first argument is the name of the type that created the named tuple.
# for the second argument, pass a list
The list contains a name to create the named tuple, for example, the first element is named name
# This function call takes 2 arguments and returns an object. This object is called student_info
Student_info creates a named tuple with this object
tu = student_info('qinghan'.18.'nv')
print(tu.name)
Copy the code
When you set the named tuple type, it returns an object that contains only the names passed in.
Next, when you create a named tuple, there are as many elements as there are elements, and the names and values of the corresponding elements are one-to-one, no more, no less.
Otherwise, an error will be reported:
Print (type(tu)) #
It returns an object with the same name as the type name.
print(type(student_info))
import timeit This module can be used for performance analysis
from collections import namedtuple
# namedtuple is a function that creates named tuples
def func() :
for i in range(10) :print(i)
# This object has a method called timeit
# res = timeit.timer (func).timeit(100) # res = timeit.timer (func).timeit(100
# timeit.timer (func).timeit(100) # timeit.timer (func).timeit(100
# print(res)
res2 = timeit.timeit('[1, 2, 3]')
print(res2)
# named tuple
# If you know the exact location of the storage, you can use the subscript to value it. For example, tu = [0]
If I don't know where the name is stored, I can't get it by subscript. Okay
# Named tuples can be evaluated like lists
Set the named tuple type
# student_info is a class
student_info = namedtuple('student_info'['name'.'age'.'gender']) # this function takes two arguments. The first argument is the name of the type that created the named tuple.
# for the second argument, pass a list
The list contains a name to create the named tuple, for example, the first element is named name
# This function call takes 2 arguments and returns an object. This object is called student_info
Student_info creates a named tuple with this object
tu = student_info('qinghan'.18.'nv')
print(tu.name)
print(type(tu)) # Look at its type
print(type(student_info))
Student_info is a class so the type of student_info is a type. It's the same if you look at any class.
Copy the code
The public account “QingHAN Software Test” was launched. More original articles: qinghan software test 105+ original articles, welcome to follow and exchange, and unauthorized reprinting by third parties is prohibited.