简体   繁体   English

几个巨大的嵌套循环的小循环与小的嵌套循环的巨大循环的性能?

[英]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. 我需要遍历该数组,仅访问s1值,并将它们存储到将作为结果返回的新数组中。

Option 1: 选项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: 选项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 考虑到第二维的尺寸最大为20,而第三维的尺寸最大为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: 它比map方法快两倍以上:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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