Why Does Looping Beat Indexing Here?

后端 未结 2 1067
青春惊慌失措
青春惊慌失措 2021-01-04 14:19

A few years ago, someone posted on Active State Recipes for comparison purposes, three python/NumPy functions; each of these accepted the same arguments and returne

2条回答
  •  盖世英雄少女心
    2021-01-04 14:55

    dis for fun:

    dis.dis(calcDistanceMatrixFastEuclidean)

      2           0 LOAD_GLOBAL              0 (len)
                  3 LOAD_FAST                0 (points)
                  6 CALL_FUNCTION            1
                  9 STORE_FAST               1 (numPoints)
    
      3          12 LOAD_GLOBAL              1 (sqrt)
                 15 LOAD_GLOBAL              2 (sum)
                 18 LOAD_GLOBAL              3 (repmat)
                 21 LOAD_FAST                0 (points)
                 24 LOAD_FAST                1 (numPoints)
                 27 LOAD_CONST               1 (1)
                 30 CALL_FUNCTION            3
    
      4          33 LOAD_GLOBAL              4 (repeat)
                 36 LOAD_FAST                0 (points)
                 39 LOAD_FAST                1 (numPoints)
                 42 LOAD_CONST               2 ('axis')
                 45 LOAD_CONST               3 (0)
                 48 CALL_FUNCTION          258
                 51 BINARY_SUBTRACT
                 52 LOAD_CONST               4 (2)
                 55 BINARY_POWER
                 56 LOAD_CONST               2 ('axis')
                 59 LOAD_CONST               1 (1)
                 62 CALL_FUNCTION          257
                 65 CALL_FUNCTION            1
                 68 STORE_FAST               2 (distMat)
    
      5          71 LOAD_FAST                2 (distMat)
                 74 LOAD_ATTR                5 (reshape)
                 77 LOAD_FAST                1 (numPoints)
                 80 LOAD_FAST                1 (numPoints)
                 83 BUILD_TUPLE              2
                 86 CALL_FUNCTION            1
                 89 RETURN_VALUE
    

    dis.dis(calcDistanceMatrixFastEuclidean2)

      2           0 LOAD_GLOBAL              0 (array)
                  3 LOAD_FAST                0 (nDimPoints)
                  6 CALL_FUNCTION            1
                  9 STORE_FAST               0 (nDimPoints)
    
      3          12 LOAD_FAST                0 (nDimPoints)
                 15 LOAD_ATTR                1 (shape)
                 18 UNPACK_SEQUENCE          2
                 21 STORE_FAST               1 (n)
                 24 STORE_FAST               2 (m)
    
      4          27 LOAD_GLOBAL              2 (zeros)
                 30 LOAD_FAST                1 (n)
                 33 LOAD_FAST                1 (n)
                 36 BUILD_TUPLE              2
                 39 LOAD_CONST               1 ('d')
                 42 CALL_FUNCTION            2
                 45 STORE_FAST               3 (delta)
    
      5          48 SETUP_LOOP              76 (to 127)
                 51 LOAD_GLOBAL              3 (xrange)
                 54 LOAD_FAST                2 (m)
                 57 CALL_FUNCTION            1
                 60 GET_ITER
            >>   61 FOR_ITER                62 (to 126)
                 64 STORE_FAST               4 (d)
    
      6          67 LOAD_FAST                0 (nDimPoints)
                 70 LOAD_CONST               0 (None)
                 73 LOAD_CONST               0 (None)
                 76 BUILD_SLICE              2
                 79 LOAD_FAST                4 (d)
                 82 BUILD_TUPLE              2
                 85 BINARY_SUBSCR
                 86 STORE_FAST               5 (data)
    
      7          89 LOAD_FAST                3 (delta)
                 92 LOAD_FAST                5 (data)
                 95 LOAD_FAST                5 (data)
                 98 LOAD_CONST               0 (None)
                101 LOAD_CONST               0 (None)
                104 BUILD_SLICE              2
                107 LOAD_GLOBAL              4 (newaxis)
                110 BUILD_TUPLE              2
                113 BINARY_SUBSCR
                114 BINARY_SUBTRACT
                115 LOAD_CONST               2 (2)
                118 BINARY_POWER
                119 INPLACE_ADD
                120 STORE_FAST               3 (delta)
                123 JUMP_ABSOLUTE           61
            >>  126 POP_BLOCK
    
      8     >>  127 LOAD_GLOBAL              5 (sqrt)
                130 LOAD_FAST                3 (delta)
                133 CALL_FUNCTION            1
                136 RETURN_VALUE
    

    I'm not an expert on dis, but it seems like you would have to look more at the functions that the first is calling to know why they take a while. There is a performance profiler tool with Python as well, cProfile.

提交回复
热议问题