Performance data structure python

2019-11-03  16:07:33

## Comparison * list * and * dict * Operation

Types of list dict
index i key
Add to append、extend、insert d[key] = value
delete pop、remove* pop
Update l[i] = value d[key] = value
Positive check l[i]、l[i:j] d[key]、copy
Reverse Lookup index(value)、count(value) /
other reverse、sort has_key、update

In principle, the best performance for common operations

# list

List: The most common operations have

Assignment Index Value + Press: `l [i] = v`` v = l [i] `

+ List Growth:

  - append()

  - __add()__

  - "+"

The method of generating four kinds of the first n integers list

# Loop connection 
DEF test1 (): 
    L = []
     for I in Range (1000 ): 
        L = L + [I] 

# the append () method 
DEF test2 (): 
    L = []
     for I in Range (1000 ): 
        L .append (I) 

# listing derived formula 
DEF Test3 (): 
    L = [I for I in Range (1000 )] 

# Range () function call to the forwarding list 
DEF Test4 (): 
    L = list (Range (1000))

Performance Comparison

from timeit import Timer

t1 = Timer("test1()", "from __main__ imporrt test1")
print("concat %f seconds\n" % t1.timeit(number = 1000))

t2 = Timer("test2()", "from __main__ imporrt test2")
print("append %f seconds\n" % t2.timeit(number = 1000))

t3 = Timer("test3()", "from __main__ imporrt test3")
print("comprehension %f seconds\n" % t3.timeit(number = 1000))

t4 = Timer("test4()", "from __main__ imporrt test4")
print("list range %f seconds\n" % t4.timeit(number = 1000))

- timeit module Timer.timeit () method [number] parameter indicates how many times repeated calls

Results Run 1 <2 <3 <4

concat 1.082888 seconds
append 0.054237 seconds
comprehension 0.027933 seconds
list range 0.011302 seconds

 ## list.pop operation

Comparative pop () and pop (i)

import timeit
popzero = timeit.Timer("x.pop(0)", "from __main__ import x")
popend = timeit.Timer("x.pop()", "from __main__ import x")

x = list(range(2000000))
print(popzero.timeit(number=1000))

x = list(range(2000000))
print(popend.timeit(number=1000))

operation result

1.5929707000000235 
5.389999989802163e-05

Compare the two time growth trend

print("\tpop(0)\t\t\tpop()")
for i in range(1000000,100000001,1000000):
    x = list(range(i))
    pt = popend.timeit(number=1000)
    x = list(range(i))
    pz = popzero.timeit(number=1000)
    print("%15.5f, %15.5f"%(pz,pt))
	pop(0)	      pop()
        0.79530,         0.00007
        1.62498,         0.00006
        2.71965,         0.00007
        3.78712,         0.00006
        5.04768,         0.00006
        6.15274,         0.00006
        6.96183,         0.00007
        7.83566,         0.00007
        9.28867,         0.00007

 Linear growth visible: no growth

 

# dict

The most common operations

- the value of get ()

- assignment set ()

- existence contains (in)

Performance are O (1)

import random
for i in range(10000,100001,10000):
    t = timeit.Timer("random.randrange(%d) in x" % i, "from __main__ import random, x")
    x = list(range(i))
    lst_time = t.timeit(number=1000)
    x = {j:None for j in range(i)}
    d_time = t.timeit(number=1000)
    print("%d,%10.3f,%10.3f" % (i,lst_time,d_time))

Operating results (size, lists, dictionaries)

    10000,     0.047,     0.001
    20000,     0.085,     0.001
    30000,     0.129,     0.001
    40000,     0.179,     0.001
    50000,     0.220,     0.001
    60000,     0.255,     0.001
    70000,     0.311,     0.001
    80000,     0.355,     0.001
    90000,     0.376,     0.001
    100000,     0.414,     0.001

Linear growth visible: irrespective of the size of

O (n): O (1)

For more information see the official wiki

https://wiki.python.org/moin/TimeComplexity

Guess you like

Origin www.cnblogs.com/ilyyfan/p/11788222.html