Đây là một số phép đo hiệu suất từ máy của tôi. Có liên quan trong trường hợp điều này được thực hiện cho các đầu vào nhỏ trong một vòng lặp dài:
import functools, operator, timeit
import numpy as np
def multiply_numpy[iterable]:
return np.prod[np.array[iterable]]
def multiply_functools[iterable]:
return functools.reduce[operator.mul, iterable]
def multiply_manual[iterable]:
prod = 1
for x in iterable:
prod *= x
return prod
sizesToTest = [5, 10, 100, 1000, 10000, 100000]
for size in sizesToTest:
data = [1] * size
timerNumpy = timeit.Timer[lambda: multiply_numpy[data]]
timerFunctools = timeit.Timer[lambda: multiply_functools[data]]
timerManual = timeit.Timer[lambda: multiply_manual[data]]
repeats = int[5e6 / size]
resultNumpy = timerNumpy.timeit[repeats]
resultFunctools = timerFunctools.timeit[repeats]
resultManual = timerManual.timeit[repeats]
print[f'Input size: {size:>7d} Repeats: {repeats:>8d} Numpy: {resultNumpy:.3f}, Functools: {resultFunctools:.3f}, Manual: {resultManual:.3f}']
Results:
Input size: 5 Repeats: 1000000 Numpy: 4.670, Functools: 0.586, Manual: 0.459
Input size: 10 Repeats: 500000 Numpy: 2.443, Functools: 0.401, Manual: 0.321
Input size: 100 Repeats: 50000 Numpy: 0.505, Functools: 0.220, Manual: 0.197
Input size: 1000 Repeats: 5000 Numpy: 0.303, Functools: 0.207, Manual: 0.185
Input size: 10000 Repeats: 500 Numpy: 0.265, Functools: 0.194, Manual: 0.187
Input size: 100000 Repeats: 50 Numpy: 0.266, Functools: 0.198, Manual: 0.185
Bạn có thể thấy rằng Numpy chậm hơn một chút so với các đầu vào nhỏ hơn, vì nó phân bổ một mảng trước khi nhân lên. Ngoài ra, coi chừng tràn trong Numpy.
Trong bài đăng này, bạn sẽ học cách nhân tất cả các yếu tố trong danh sách bằng ngôn ngữ lập trình Python.Python programming language.
Danh sách là một chuỗi các giá trị được lập chỉ mục và đặt hàng như một mảng. Nó là có thể thay đổi, có nghĩa là chúng ta có thể thay đổi thứ tự của các yếu tố trong một danh sách. Một danh sách trong Python là một cấu trúc dữ liệu tuyến tính có thể giữ các yếu tố không đồng nhất. Nó là linh hoạt để thu nhỏ và phát triển, và không cần phải khai báo nó.list is a sequence of indexed and ordered values like an array. It is mutable, which means we can change the order of elements in a list. A list in Python is a linear data structure that can hold heterogeneous elements. It is flexible to shrink and grow, and there is no need to declare it.
Có nhiều cách khác nhau trong Python để nhân tất cả các yếu tố trong một danh sách.
Nhân tất cả các yếu tố trong danh sách bằng phương pháp truyền tải
Ở đây, chúng tôi đã lấy một 'sản phẩm' biến và khởi tạo nó với giá trị 1. Tiếp theo, chúng tôi đã đi qua danh sách cho đến khi kết thúc và nhân mọi yếu tố với sản phẩm. Giá trị của 'sản phẩm' cuối cùng là kết quả cuối cùng.
# Python program to multiply all elements in the
# list using traversal
def multiplyList[xList] :
product = 1
for x in xList:
product = product * x
return product
list1 = [11, 3, 2]
list2 = [8, 2, 5]
print["Product of first list: ",multiplyList[list1]]
print["Product of second list: ",multiplyList[list2]]
Đầu ra của mã trên:
Product of first list: 66
Product of second list: 80
Nhân tất cả các yếu tố trong danh sách bằng numpy.prod []
Phương thức numpy prod [] trả về sản phẩm của mảng các phần tử trên một trục nhất định. Phương pháp này trả về một số nguyên hoặc giá trị nổi tùy thuộc vào kết quả nhân. Trong ví dụ đã cho, chúng tôi nhập mô -đun Numpy và sử dụng phương thức NP.Prod [] để có được sản phẩm của tất cả các yếu tố trong danh sách.prod[] method returns the product of the array of elements over a given axis. This method returns an integer or a float value depending on the multiplication result. In the given example, we import the numpy module and use the np.prod[] method to get the product of all the elements in the list.
# Python program to multiply all elements in the
# list using numpy
import numpy as np
list1 = [6, 4, 2]
list2 = [8, 2, 5]
# Multiply elements of list using numpy.prod[]
product1 = np.prod[list1]
product2 = np.prod[list2]
print["Product of first list: ",product1]
print["Product of second list: ",product2]
Đầu ra của mã trên:
Product of first list: 48
Product of second list: 80
Nhân tất cả các yếu tố trong danh sách bằng numpy.prod []
Phương thức numpy prod [] trả về sản phẩm của mảng các phần tử trên một trục nhất định. Phương pháp này trả về một số nguyên hoặc giá trị nổi tùy thuộc vào kết quả nhân. Trong ví dụ đã cho, chúng tôi nhập mô -đun Numpy và sử dụng phương thức NP.Prod [] để có được sản phẩm của tất cả các yếu tố trong danh sách.reduce[] function is defined in the functools module. It accepts a function as the first argument and an iterable as the second argument. We can use this function to multiply all the elements of the list by providing operator.mul in the first argument and list in the second.
# Python program to multiply all elements in the
# list using functools and operator
# importing functools for reduce[]
import functools
# importing operator for operator functions
import operator
a_list = [5, 2, 7, 4]
product = functools.reduce[operator.mul, a_list]
print["Product of elements of the list: ",product]
Đầu ra của mã trên:
Product of elements of list: 280
Nhân tất cả các yếu tố trong danh sách bằng numpy.prod []
Phương thức numpy prod [] trả về sản phẩm của mảng các phần tử trên một trục nhất định. Phương pháp này trả về một số nguyên hoặc giá trị nổi tùy thuộc vào kết quả nhân. Trong ví dụ đã cho, chúng tôi nhập mô -đun Numpy và sử dụng phương thức NP.Prod [] để có được sản phẩm của tất cả các yếu tố trong danh sách.math.prod[] method to easily multiply the elements of the list. The math.prod is a new function and available from Python 3.8.
# Import math library
import math
list1 = [11, 2, 4]
list2 = [3, 6, 5]
result1 = math.prod[list1]
result2 = math.prod[list2]
print["Product of first list: ",result1]
print["Product of second list: ",result2]
Đầu ra của mã trên:
Product of first list: 88
Product of second list: 90