简体   繁体   中英

Small loop of several huge nested loops vs huge loop of small nested loops performance?

I have a server that accesses and gets data in the format of a multidimensional array so the end result is:

 [
    [
        [n1t1:1, n1s1:2, n1o1:5],
        [n1t2:3, n1s2:8, n1o2:9]
    ],
    [
        [n2t1:9, n2s1:3, n2o1:2],
        [n2t2:5, n2s2:1, n2o2:7]
    ],
    [
        [n3t1:4, n3s1:9, n3o1:2],
        [n3t2:7, n3s2:1, n3o2:5]
    ]
 ]

I need to go through that array, access only s1 values and store them into a new array that will be returned as a result.

Option 1:

result = []
parent_enum = 0
while len(array) > parent_enum:
    child_enum = 0
    result.append([])
    while len(child_enum) > array_num:
        result[parent_enum].append(array[parent_enum][child_enum][1])
        child_enum += 1
    parent_enum += 1

Option 2:

result = [[] for i in range(len(array))]
parent_enum = 0
while len(array[0]) > parent_enum:
    child_enum = 0
    while len(array) > child_enum:
         result[child_enum].append(array[child_enum][parent_enum][1])
         child_enum += 1
    parent_enum += 1

Is there a difference and if so, which way would be more efficient and fast? Considering the size of a 2nd dimension is up to 20 and 3rd dimension is up to 500

The following code should be more readable and have good performance by using builtin functions.

data = [ ...your data... ]
result = map(lambda first:  # for each first-level entry
                 map(lambda second:  # for each second-level entry within first
                         second[1],  # return the second value
                     first
                 ),
             data
         )
[
    [
        2,
        8
    ],
    [
        3,
        1
    ],
    [
        9,
        1
    ]
 ]

Why not using simple list comprehension:

arr = [
    [
        ["n1t1:1", "n1s1:2", "n1o1:5"],
        ["n1t2:3", "n1s2:8", "n1o2:9"]
    ],
    [
        ["n2t1:9", "n2s1:3", "n2o1:2"],
        ["n2t2:5", "n2s2:1", "n2o2:7"]
    ],
    [
        ["n3t1:4", "n3s1:9", "n3o1:2"],
        ["n3t2:7", "n3s2:1", "n3o2:5"]
    ]
 ]


result = [[arr_lev3[1] for arr_lev3 in arr_lev2] for arr_lev2 in arr]

print(result)

Sample output:

[['n1s1:2', 'n1s2:8'], ['n2s1:3', 'n2s2:1'], ['n3s1:9', 'n3s2:1']]

And it's more than 2 times faster than map approach:

In [38]: %timeit result = [[arr_lev3[1] for arr_lev3 in arr_lev2] for arr_lev2 in arr]
753 ns ± 2.24 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [39]: %timeit result2 = list(map(lambda first: list(map(lambda second: second[1], first)), arr))
1.63 µs ± 20.4 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM