Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ for循环中的热点_C++_Optimization_Assembly_Profiling_Intel Vtune - Fatal编程技术网

C++ for循环中的热点

C++ for循环中的热点,c++,optimization,assembly,profiling,intel-vtune,C++,Optimization,Assembly,Profiling,Intel Vtune,我正在尝试优化这段代码 static lvh_distance levenshtein_distance( const std::string & s1, const std::string & s2 ) { const size_t len1 = s1.size(), len2 = s2.size(); std::vector<unsigned int> col( len2+1 ), prevCol( len2+1 ); const siz

我正在尝试优化这段代码

static
lvh_distance levenshtein_distance( const std::string & s1, const std::string & s2 )
{
    const size_t len1 = s1.size(), len2 = s2.size();
    std::vector<unsigned int> col( len2+1 ), prevCol( len2+1 );

    const size_t prevColSize = prevCol.size();
    for( unsigned int i = 0; i < prevColSize; i++ )
        prevCol[i] = i;

    for( unsigned int i = 0, j; i < len1; ++i )
    {
        col[0] = i+1;
        const char s1i = s1[i];
        for( j = 0; j < len2; ++j )
        {
            const auto minPrev = 1 + std::min( col[j], prevCol[1 + j] );
            col[j+1] = std::min( minPrev, prevCol[j] + (  s1i == s2[j] ? 0 : 1 ) );

        }
        col.swap( prevCol );
    }
    return prevCol[len2];
}

我不明白一个简单的移动和比较怎么会如此耗时。

探查器无法向您显示最耗时的确切指令,因为所有现代CPU都使用无序和推测性执行。在距离最耗时的指令一行或两行的地方可以看到最大的测量时间,这并不罕见


正如所料,这里最耗时的指令是
cmovbe
(实现
std::min
)。你可以看到它们附近最大的时间:460.772ms和558.298ms
cmovbe
是最耗时的指令,因为它们通常具有较大的延迟,并且更多地依赖于前面的指令。

@Agentlien在32位CPU上不可能执行这样的x86-64代码。64位寄存器
rax
rbx
rcx
rdx
rsi
rdi
r8
r9
r10
r11
r12
r13
r14
r15
r9
rbp编码和cmovbe
(实现
std::min
)。你可以看到它们附近最大的时间:460.772ms和558.298ms
cmovbe
是最耗时的指令,因为它们通常有很大的延迟,并且更依赖于前面的指令。@EvgenyKluev可能有点离题,但你知道valgrind是否可以提供更精确的测量吗?@Nosenseal:我不知道valgrind在这里是否更精确。我想那不太可能。
Code Location   Source Line Assembly    CPU Time
        Block 14:   [Unknown]
0x420c00    31  movq  (%r12), %rcx  19.969ms
0x420c04    30  add $0x1, %r11d [Unknown]
0x420c08    32  test %rbx, %rbx [Unknown]
0x420c0b    30  movl  %r11d, (%r8)  [Unknown]
0x420c0e    31  movzxb  (%rcx,%rdx,1), %r9d 19.964ms
0x420c13    32  jz 0x420c53 <Block 17>  [Unknown]
        Block 15:   [Unknown]
0x420c15    32  movq  (%rbp), %r10  [Unknown]
0x420c19    32  mov %r11d, %edx [Unknown]
0x420c1c    32  xor %ecx, %ecx  39.928ms
0x420c1e    32  xor %edi, %edi  [Unknown]
        Block 16:   [Unknown]
0x420c20    34  add $0x1, %edi  29.994ms
0x420c23    34  mov %edi, %esi  30.956ms
0x420c25    34  movl  (%rax,%rsi,4), %r15d  180.659ms
0x420c29    34  cmp %r15d, %edx 39.896ms
0x420c2c    34  cmovbe %edx, %r15d  19.951ms
0x420c30    35  xor %edx, %edx  460.772ms
0x420c32    34  add $0x1, %r15d 19.946ms
0x420c36    35  cmpb  (%r10,%rcx,1), %r9b   169.659ms  
0x420c3a    35  setnz %dl   49.815ms
0x420c3d    35  addl  (%rax,%rcx,4), %edx   [Unknown]
0x420c40    32  mov %rsi, %rcx               210.615ms  <------------------
0x420c43    32  cmp %edx, %r15d              29.936ms
0x420c46    32  cmovbe %r15d, %edx          29.938ms
0x420c4a    32  cmp %rsi, %rbx              558.298ms  <-------------------
0x420c4d    35  movl  %edx, (%r8,%rsi,4)    19.965ms
0x420c51    32  jnbe 0x420c20 <Block 16>    200.625ms  <-------------------