Người chiến thắng chung cuộc giữa việc sử dụng NP.SUM, NP.min và tất cả dường như là NP.min về tốc độ cho các mảng lớn:
N = 1000000
def func_sum[x]:
my_list = np.random.randn[N]
return np.sum[my_list < x ]==0
def func_min[x]:
my_list = np.random.randn[N]
return np.min[my_list] >= x
def func_all[x]:
my_list = np.random.randn[N]
return all[i >= x for i in my_list]
.
Hiệu suất của "tất cả" phụ thuộc rất nhiều vào khi phần tử đầu tiên không đáp ứng các tiêu chí được tìm thấy, np.sum cần thực hiện một chút hoạt động, NP.min là nhẹ nhất về các tính toán trong trường hợp chung .
Khi các tiêu chí gần như được đáp ứng ngay lập tức và tất cả các vòng lặp thoát nhanh, tất cả các chức năng chỉ chiến thắng một chút so với NP.min:
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
Nhưng khi "tất cả" cần phải trải qua tất cả các điểm, nó chắc chắn tồi tệ hơn nhiều và NP.min thắng:
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
Nhưng sử dụng
np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
0 >>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
1>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
2>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3 >>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
4>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5 >>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
6>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
3>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
5>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
6>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
7>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8 >>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
9>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes2
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes5
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes6
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes7
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
def
0
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8 >>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
2>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes2
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes5
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes6
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes7
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
def
0
5 np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
Input : list = [10, 20, 30, 40, 50]
given value = 20
Output : No
Input : list = [10, 20, 30, 40, 50]
given value = 5
Output : Yes
0
np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5 >>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8Implementation:
Phương pháp 1: Danh sách truyền tải
Bằng cách đi qua trong danh sách, chúng ta có thể so sánh mọi yếu tố và kiểm tra xem tất cả các phần tử trong danh sách đã cho có lớn hơn giá trị đã cho hay không. & NBSP;
Python
>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
3>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
5>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
6>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
8>>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
7np.sum[my_list> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
3Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes2
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes5
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes6
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes7
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
def
0
>>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
8 >>> %timeit func_sum[-10]
10 loops, best of 3: 36.2 ms per loop
>>> %timeit func_min[-10]
10 loops, best of 3: 35.2 ms per loop
>>> %timeit func_all[-10]
10 loops, best of 3: 230 ms per loop
2np.sum[my_list>> %timeit func_sum[10]
10 loops, best of 3: 36.1 ms per loop
>>> %timeit func_min[10]
10 loops, best of 3: 35.1 ms per loop
>>> %timeit func_all[10]
10 loops, best of 3: 35 ms per loop
5 Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes0
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes5
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes6
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes7
Input : list = [10, 20, 30, 40, 50] given value = 20 Output : No Input : list = [10, 20, 30, 40, 50] given value = 5 Output : Yes4
def
0