[Python] When does numpy actually improve performance?

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have repeatedly heard that using a numpy array on large arrays has better performance over using a python list, but I cannot find a concise explanation to what circumstances it needs to see improvement. Here is what I want to know:

    • What operations/functions actually compute faster than with a list
    • How does size affect performance? Numpy is apparently faster with bigger sizes, but where is the cutoff? (also how does the cutoff change with multidimensional arrays)

    Some example code problems I am encountering

    def problem1(list):
    #list["string", size: 2.000.00]
    for i in list:
    # accessing is the biggest slowdown.
    # Best times so far: List < Numpy.array < Pandas.Series
    val1 = list
    if i != 0:
    val2 = list[i-1]
    if i >= 10:
    val3 = list[i-10]
    do_something(val1, val2, val3)

    def problem2(list)
    #list["string", size: 2.000.00]
    # List operations.
    # Best times so far: Pandas.Series < List < Numpy.array
    list.string.slice(x, y)
    list.shift(x) == list
    list.apply(func)

    def problem3(list1, list2)
    #list1["string", size: 2.000.00]
    #list2[special, size: 100.000]
    # A LOT of searches, and a lot of getting and setting.
    # Best times so far (for list2; might be split up between the search/index and set/get):
    # (dict, Numpy.array) < (Pandas.Index, dict) < (Pandas.Index, Numpy.array) < (list, Pandas.Series) < (Pandas.Series)
    for i in list1:
    seperate_function(i)

    def seperate_function
    if i in list2:
    val_new = list2.index(i)
    if list2 == val1:
    list2 = val2
    else:
    # do something
    else:
    # do something

    Continue reading...

Compartilhe esta Página