Hướng dẫn fastest way to sum an array python - cách nhanh nhất để tính tổng một mảng python

Trong số các cách để có được tổng của một danh sách A INT trong Python là hai cách sau:

Hàm tổng tích hợp: Sum (a)

Giảm chức năng với Adder Lambda: Giảm (Lambda X, Y: X + Y, A)

Có bất kỳ lợi thế tốc độ nào để sử dụng một trong hai điều này, hoặc các màn trình diễn của chúng gần như giống nhau?

Trên máy của tôi, hàm "tổng" dường như nhanh hơn phiên bản "giảm" (ít nhất là để tổng hợp 5000 mảng có kích thước 1000).

See:

$ cat doit.py   
from timeit import timeit
print timeit('reduce(lambda x, y: x + y, range(1000))',number=5000)
print timeit('sum(range(1000))',number=5000)

$ python2 doit.py
0.460000038147
0.0599999427795

Cập nhật: Để giải quyết nhận xét, tôi đã cập nhật câu trả lời của mình để bao gồm 'thiết lập' để tạo mảng sẽ được tổng hợp:

$ cat doit2.py
from timeit import timeit

print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
print timeit('sum(a)',setup='a=range(1000)',number=5000)

$ python2 doit2.py
0.530030012131
0.0320019721985

Một lần nữa, phiên bản "tổng" dường như là người chiến thắng rõ ràng.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:

    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50

    Bàn luận Iterating through the array and adding each element to the sum variable and finally displaying the sum.

    Python3

    Đưa ra một loạt các số nguyên, tìm tổng các yếu tố của nó.

    Phương pháp 1: Lặp lại qua mảng và thêm từng phần tử vào biến tổng và cuối cùng hiển thị tổng.

    def _sum(arr):

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0____11
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    3

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    6
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    7
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    9

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0____15
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    6
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    7
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    8

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    9
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    3
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    4

    Sum of the array is  34
    0____12
    Sum of the array is  34
    2

    Sum of the array is  34
    0
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    Sum of the array is  34
    5
    Sum of the array is  34
    6
    Sum of the array is  34
    7
    Sum of the array is  34
    8
    Sum of the array is  34
    7
    sum(iterable) 
    0
    Sum of the array is  34
    7
    sum(iterable) 
    2
    sum(iterable) 
    3

    Sum of the array is  34
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    7
    Sum of the array is  34
    3
    Sum of the array is  34
    4

    Output:

    Sum of the array is  34

    sum(iterable) 
    4
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    sum(iterable) 
    6
    sum(iterable) 
    7
    : O(n), Auxiliary Space: O(1)

    sum(iterable) 
    8
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    Sum of the array is  34
    0
    Using the built-in function sum(). Python provides an inbuilt function sum() which sums up the numbers in the list.

    Syntax:  

    sum(iterable) 

    Độ phức tạp về thời gian: O (n), không gian phụ trợ: O (1) iterable can be anything list, tuples or dictionaries, but most importantly it should be numbered.

    Python3

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0____15
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    6
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    7
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    8

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    9
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    3
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    4

    Sum of the array is  34
    0____12
    Sum of the array is  34
    2

    Sum of the array is  34
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    7
    Sum of the array is  34
    3
    Sum of the array is  34
    4

    Output:

    Sum of the array is  34

    sum(iterable) 
    4
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    sum(iterable) 
    6
    sum(iterable) 
    7
    : O(n), Auxiliary Space: O(1)

    sum(iterable) 
    8
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    Sum of the array is  34
    0
    Using the reduce method. Array.reduce() method is used to iterate over the array and get the summarized result from all elements of array.

    Syntax:

    reduce( function, Array );

    Độ phức tạp về thời gian: O (n), không gian phụ trợ: O (1)

    Phương pháp 2: Sử dụng tổng chức năng tích hợp (). Python cung cấp một tổng số hàm sẵn () tổng hợp các số trong danh sách.

    Đưa ra một loạt các số nguyên, tìm tổng các yếu tố của nó.

    Phương pháp 1: Lặp lại qua mảng và thêm từng phần tử vào biến tổng và cuối cùng hiển thị tổng.

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    6
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    7
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    9

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    0____15
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    6
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    7
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    8

    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    9
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    3
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    4

    Sum of the array is  34
    0____12
    Sum of the array is  34
    2

    Sum of the array is  34
    0
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    Sum of the array is  34
    5
    Sum of the array is  34
    6
    Sum of the array is  34
    7
    Sum of the array is  34
    8
    Sum of the array is  34
    7
    sum(iterable) 
    0
    Sum of the array is  34
    7
    sum(iterable) 
    2
    sum(iterable) 
    3

    Sum of the array is  34
    1
    Input : arr[] = {1, 2, 3}
    Output : 6
    1 + 2 + 3 = 6
    
    Input : arr[] = {15, 12, 13, 10}
    Output : 50
    7
    Sum of the array is  34
    3
    Sum of the array is  34
    4

    Output:

    Sum of the array is  34

    sum(iterable) 
    4
    $ cat doit2.py
    from timeit import timeit
    
    print timeit('reduce(lambda x, y: x + y, a)',setup='a=range(1000)',number=5000)
    print timeit('sum(a)',setup='a=range(1000)',number=5000)
    
    $ python2 doit2.py
    0.530030012131
    0.0320019721985
    
    2
    sum(iterable) 
    6
    sum(iterable) 
    7


    Làm thế nào để bạn tổng hợp toàn bộ một mảng trong Python?

    Bước 1: Khai báo và khởi tạo một mảng ..
    Bước 2: Tổng biến sẽ được sử dụng để tính tổng của các phần tử.Khởi tạo nó thành 0 ..
    Bước 3: Vòng lặp qua mảng và thêm từng phần tử của mảng vào tổng biến là sum = sum + mảng [i] ..

    Sum có nhanh hơn đối với Loop Python không?

    Tại sao vậy?Sum được thực hiện như thế nào?Tổng hợp được thực hiện trong C bên trong trình thông dịch Python, trong khi vòng lặp của bạn phải được giải thích, điều bình thường là nó chậm hơn.Trong các chức năng tích hợp Cpython nhanh hơn nhiều so với bản dịch Pure-Python.it's normal that it's slower. In CPython built-in functions are much faster than the pure-python translation.

    Làm thế nào để bạn tổng hợp hai mảng trong Python?

    Để thêm hai mảng với nhau, chúng tôi sẽ sử dụng phương thức Numpy.ADD (ARR1, ARR2).Để sử dụng phương pháp này, bạn phải đảm bảo rằng hai mảng có cùng chiều dài.Nếu độ dài của hai mảng không giống nhau, thì hãy phát kích thước của mảng ngắn hơn bằng cách thêm số 0 tại các chỉ mục bổ sung.numpy. add(arr1,arr2) method. In order to use this method, you have to make sure that the two arrays have the same length. If the lengths of the two arrays are​ not the same, then broadcast the size of the shorter array by adding zero's at extra indexes.

    SUM () SUM () trong Python là gì?

    Hàm python sum () hàm sum () trả về một số, tổng của tất cả các mục trong một số không thể điều chỉnh được.returns a number, the sum of all items in an iterable.