Built Performance Test Method Python each data type list

test environment

Codes are referred to herein in MacOS system CentOS7 test, Python is version 3.6.8.

The test module

Python test module is built timeit modules:

timeit module can be used to test a small Python code execution speed.

Timer class

class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>)

Timer is a measure of small pieces of code execution speed class.

stmt parameter is to be tested code statement (statment);

setup parameter is required when running the code set;

timer parameter is a timer function, and platform-dependent.

Timer class method timeit

timeit.Timer.timeit(number=1000000)

The method of execution speed of the object tested Timer class statement. The number argument is the number of tests when the test code, the default is 1,000,000. The method returns the average execution time consuming code, a type of seconds float.

Built-in method of performance testing list

We know that, to generate a list of list of formula or append, insert, extend these methods can be used, and now we look at the efficiency of these methods:

from timeit import Timer


def test_list():
    lst = list(range(1000))

def test_generation():
    lst = [i for i in range(1000)]


def test_append():
    lst = []
    for i in range(1000):
        lst.append(i)

def test_add():
    lst = []
    for i in range(1000):
        lst += [i]

#In the head of the list INSERT 
DEF test_insert_zero (): 
    LST = []
     for I in Range (1000 ): 
        lst.insert (0, I) 

# at end of the list INSERT 
DEF test_insert_end (): 
    LST = []
     for I in Range (1000 ): 
        lst.insert ( -1 , I) 

DEF test_extend (): 
    LST = [] 
    lst.extend (List (Range ( 1000 ))) 


T1 = the Timer ( " test_list () " , "from __main__ import test_list")
print(f"test_list takes {t1.timeit(number=1000)} seconds")

t2 = Timer("test_generation()","from __main__ import test_generation")
print(f"test_generation takes {t2.timeit(number=1000)} seconds")

t3 = Timer("test_append()","from __main__ import test_append")
print(f"test_append takes {t3.timeit(number=1000)} seconds")

t4 = Timer("test_add()","from __main__ import test_add")
print(f"test_add takes {t4.timeit(number=1000)} seconds")

t5 = Timer("test_insert_zero()","from __main__ import test_insert_zero")
print(f"test_insert_zero takes {t5.timeit(number=1000)} seconds")

t6 = Timer("test_insert_end()","from __main__ import test_insert_end")
print(f"test_insert_end takes {t6.timeit(number=1000)} seconds")

t7 = Timer("test_extend()","from __main__ import test_extend")
print(f"test_extend takes {t7.timeit(number=1000)} seconds")

We take a look under MacOS system, the results of the above code:

"""
test_list takes 0.012904746999993222 seconds
test_generation takes 0.03530399600003875 seconds
test_append takes 0.0865129750000051 seconds
test_add takes 0.08066114099983679 seconds
test_insert_zero takes 0.30594958500023495 seconds
test_insert_end takes 0.1522782449992519 seconds
test_extend takes 0.017534753999825625 seconds
"""

We can see: list directly using the highest efficiency method strong turn, followed by the use of formula list, and append direct way to increase the efficiency of both followed and fairly; minimum efficiency insert method - and scratch inserted less efficient than inserted from the rear efficiency! Finally, we will turn strong list using the extend method to process efficiency into a new list did not have much reduced.

Then try the Linux system implementation results:

List pop performance test methods

pop can be deleted from the 0 position each element, you can also delete elements (default deletion rearmost element) from the last position, and now we have to test the performance comparison of the two remove elements from different positions:

from timeit import Timer

def test_pop_zero():
    lst = list(range(2000))
    for i in range(2000):
        lst.pop(0)


def test_pop_end():
    lst = list(range(2000))
    for i in range(2000):
        lst.pop()


t1 = Timer("test_pop_zero()","from __main__ import test_pop_zero")
print(f"test_pop_zero takes {t1.timeit(number=1000)} seconds")

t2 = Timer("test_pop_end()","from __main__ import test_pop_end")
print(f"test_pop_end takes {t2.timeit(number=1000)} seconds")

In the results of the implementation of the program under MacOS:

test_pop_zero takes 0.5015365449999081 seconds
test_pop_end takes
0.22170215499954793 seconds

Then we try the results Linux system:

 

We can see: a lot of high-removing elements from the end of the list to remove from the head efficient than efficiency!

A small pit on the insert method of the list

If you want to generate a list [0,1,2,3,4,5], then use the insert method (of course using the insert method efficiency will be much lower, it is recommended to use other methods) so there is a problem, look at this record:

def test_insert():
    lst = []
    for i in range(6):
        lst.insert(-1,i)
        print(lst)

test_insert() 

The results turned out to be the case - the first element has been turned in the final!

[0]
[1, 0]
[1, 2, 0]
[1, 2, 3, 0]
[1, 2, 3, 4, 0]
[1, 2, 3, 4, 5, 0]

~~~

Guess you like

Origin www.cnblogs.com/paulwhw/p/12150685.html