-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_sort.py
53 lines (42 loc) · 1.2 KB
/
test_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import random
import time
import sys
import timeit
import heapq
def runtime(func):
'''測試執行時間'''
def new_func(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
end = time.time()
cost = round(end - start, 3)
print("cost: {} sec".format(cost))
return new_func
def create():
''' Create 1 million random numbers'''
length = 1000000
# length = 100
nums = [random.randint(0, 99999999)
for i in range(length)] # 将序列nums中的元素顺序打乱
with open("nums.txt", "w") as file: # file size 8.5M
stra = "\n".join([str(i) for i in nums])
file.write(stra)
return nums
@runtime
def use_heapq():
'''最小堆排序算法'''
import heapq
with open('nums.txt', "r") as file:
nums = map(int, file.readlines())
print(heapq.nlargest(10, nums)) # 選出最大的十個
# print (heapq.nsmallest(10, nums)) # 選出最小的十個
@runtime
def use_sort(nums):
'''內建的sort 使用 timsort '''
nums.sort()
print(nums[-10:][::-1]) # 選出最大的十個
return nums
if __name__ == "__main__":
nums = create()
use_sort(nums)
use_heapq()