Night Python - 2 Nights - crawling

Night Python - 2 Nights - crawling

  I used to imagine that he is a snail, a small shell has its own, afraid of the wind, not afraid of rain, dissolute rivers and lakes, traveled extensively ...... night Maoxiong always interrupted my illusions: "dissolute travel?? ! so you have to climb a monk retired the door, "I know what to say Maoxiong night, so take the opportunity joked:" Python is not reptiles me "(Note: Python = python)?

  "Oh, good English ah."

  "What are you praise me with you?" Wow, night Maoxiong get praise, I am happy to feel the hair more than 3 percent.

  Night Maoxiong just idle, give me a segment set, tuple, list of programs running speed comparison:

 1 # !/usr/bin/env python3
 2 # -*- coding=utf-8 -*-
 3 import timeit
 4 
 5 def func_use_set():
 6     N = 1000
 7     numbers = set(range(N))
 8     for number1 in numbers:
 9         for number2 in numbers:
10             pass
11 
12 def func_use_tuple():
13     N = 1000
14     numbers = tuple(range(N))
15     for number1 in numbers:
16         for number2 in numbers:
17             pass
18 
19 def func_use_list():
20     N = 1000
21     numbers = list(range(N))
22     for number1 in numbers:
23         for number2 in numbers:
24             pass
25 
26 
27 if __name__ == '__main__':
28     print('set:', timeit.timeit(stmt=func_use_set, number=10), 's')
29     print('tuple:', timeit.timeit(stmt=func_use_tuple, number=10), 's')
30     print('list:', timeit.timeit(stmt=func_use_list, number=10), 's')

 

  When given this code, the night Maoxiong asked me, how do you think the speed rankings?

  "Of course, set a fastest traversal time, then, set <tuple <list." I blurted out, maybe have a natural confidence fan of it.

  "Are you sure? Ah ......" Night Maoxiong twist twist his beard thoughtfully, "Have you tried?"

  "No, ah, friends say."

  "Damn it! You have not tried believed ??" Night Maoxiong suddenly press Enter on your keyboard, simply place the keyboard pierced.

  After a few seconds, following the console output data.

1 set: 0.5598647409997284 s
2 tuple: 0.5289756949996445 s
3 list: 0.4945050540000011 s

  "Ah? This is what?" My world view has collapsed ......

  I tried to increase the scale of the change order N 10000, the three also executed swap a bit hard night Maoxiong issued a creak screams ...... long time without results, I change the number again to 5, there is the following data.

1 list: 24.5941286340003 s
2 tuple: 24.454961858999923 s
3 set: 26.473167070000272 s

  Tried many times and the results are set> tuple ≈ list. This time I was dumbfounded: "Night Maoxiong, you are not secretly changed the Python source code?"

  "Ah you want Shane, go back and get your own 'snail shell' give it a try!"

  I think that I do not understand ...... I thought commonly used in Python vehicles such as set, tuple, list, dict them, I understand them, and I also think the code can do anything on the world sea the results, in fact, it is a small snail shell laden, and python immeasurably ah.

  Night Maoxiong keyboard sounded, the program has become such below.

 1 # !/usr/bin/env python3
 2 # -*- coding=utf-8 -*-
 3 import timeit
 4 import random
 5 
 6 def func_use_set():
 7     N = 5000
 8     numbers = set(range(N))
 9     for number1 in numbers:
10         _ = random.randint(0, N-1) in numbers
11 
12 def func_use_tuple():
13     N = 5000
14     numbers = tuple(range(N))
15     for number1 in numbers:
16         _ = random.randint(0, N-1) in numbers
17 
18 def func_use_list():
19     N = 5000
20     numbers = list(range(N))
21     for number1 in numbers:
22         _ = random.randint(0, N-1) in numbers
23 
24 
25 if __name__ == '__main__':
26     print('list:', timeit.timeit(stmt=func_use_list, number=10), 's')
27     print('tuple:', timeit.timeit(stmt=func_use_tuple, number=10), 's')
28     print('set:', timeit.timeit(stmt=func_use_set, number=10), 's')

  "Now come Guess, guess prizes oh ~" Night Maoxiong smirked, it looks really weird, like Alice in Wonderland Cheshire cat bird.

  "I ...... I ...... I ...... wow ah ......" I became king of Stuart, fell off the horse go straight. Program results:

1 list: 2.305914190999829 s
2 tuple: 2.242317064999952 s
3 set: 0.49790156399831176 s

  Ah, this fight has finally set a light to more than four times the performance of the tuple and list cut from his horse. I tried several times, tuple list is indeed better than a little faster, but still in the "≈" level.

  "Understand it." Hey smile night Maoxiong.

  "Understand what? Waiting for you to explain ah ......" I started confused.

  "Also talked about what? Practice makes perfect ah. I do not know how other people like that, but I results of this test is that way, you can also try other ways, might be able to overthrow my conclusion. Overall, set as there hash table, so look for fast operation, but then traverse to die. the 'look' and 'traverse' distinguished, 'fast' is conditional, comes at a price, such as not storing the same data set. "

  "Oh, I learned, but ...... hey, I still indented snail shell go to sleep ......" I yawn, "I am not a night cat, I have to sleep."

  "Well, hard to National Cheng Kung University!"

 

  Night Python, the second night, 2019.10.19.

 

Guess you like

Origin www.cnblogs.com/adjwang/p/11689402.html