表现不如预期 [英] Performance was not as expected

查看:61
本文介绍了表现不如预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我写了一个函数来得到2的幂次幂的指数。以下是代码:



Hi everyone

I wrote a function to get the exponent of a number power of two. Here is the code:

int exp = 0;
while((n = n / 2) > 0) {
//while((n = n >> 1) > 0) {
	exp++;
}
return exp;





然后想到了,好吧,让我们用换档操作员代替换档操作员,以提高性能。



结果不是预期的。 />


使用除法运算符,代码大约需要1秒;使用移位运算符,代码大约需要1.3秒,在相同的条件下。



在调试模式下,我检查了Visual Studio给我的代价不可思议的代码(注意:我对汇编语言一无所知。我注意到汇编代码是相同的,除非我使用除法运算符,还有两个指令。



所以,任何人都可以向我解释原因这次执行差异?在最坏的情况下,我预计会有相同的执行时间。



提前谢谢

Filipe Marques



更新2:为发布模式添加完整的asm代码。



对于divion运算符:



Then came to mind, "Well, let''s replace that division operator to the shift operator, to improve performance".

The result was not the expected.

Using the division operator, the code takes about 1 second; using the shift operator, the code takes about 1.3 seconds, on same conditions.

In debug mode, I checked the disassmebly code gave me by Visual Studio (Note: I don''t understand nothing about assembly language). I noticed that the assembly code was the same, except when I am using the division operator, there are two more instruction.

So, can anyone explain to me the reason of this time execution difference? I expected, in worst case, the same execution time.

Thanks in advance
Filipe Marques

UPDATE 2: Add the full asm code for release mode.

For the divion operator:

PUBLIC  ?get_exponent@@YAHH@Z               ; get_exponent
; Function compile flags: /Ogtp
;   COMDAT ?get_exponent@@YAHH@Z
_TEXT   SEGMENT

get_exponent@@YAHH@Z PROC              ; get_exponent, COMDAT
; _n$ = eax

; 124  :    int exp = 0;
; 125  :    while((n = n / 2) > 0) {

    cdq
    sub eax, edx
    sar eax, 1
    xor ecx, ecx
    test    eax, eax
    jle SHORT $LN7@get_expone
    npad    5
$LL2@get_expone:
    cdq
    sub eax, edx
    sar eax, 1

; 126  :    //while((n = n >> 1) > 0) {
; 127  :        exp++;

    inc ecx
    test    eax, eax
    jg  SHORT $LL2@get_expone
$LN7@get_expone:

; 128  :    }
; 129  :    return exp;

    mov eax, ecx

; 130  : }

    ret 0
?get_exponent@@YAHH@Z ENDP              ; get_exponent





对于班次操作员:



For shift operator:

PUBLIC  ?get_exponent@@YAHH@Z               ; get_exponent
; Function compile flags: /Ogtp
;   COMDAT ?get_exponent@@YAHH@Z
_TEXT   SEGMENT

get_exponent@@YAHH@Z PROC              ; get_exponent, COMDAT
; _n$ = ecx

; 124  :    int exp = 0;
; 125  :    //while((n = n / 2) > 0) {
; 126  :    while((n = n >> 1) > 0) {

    sar ecx, 1
    xor eax, eax
    test    ecx, ecx
    jle SHORT $LN1@get_expone
$LL2@get_expone:
    sar ecx, 1

; 127  :        exp++;

    inc eax
    test    ecx, ecx
    jg  SHORT $LL2@get_expone
$LN1@get_expone:

; 128  :    }
; 129  :    return exp;
; 130  : }

    ret 0
?get_exponent@@YAHH@Z ENDP              ; get_exponent

推荐答案

= eax

; 124:int exp = 0;
; 125:while((n = n / 2)> 0){

cdq
sub < span class =code-keyword> eax , edx
sar < span class =code-keyword> eax , 1
xor < span class =code-keyword> ecx , ecx
test < span class =code-keyword> eax , eax
jle < span class =code-sdkkeyword> SHORT
= eax ; 124 : int exp = 0; ; 125 : while((n = n / 2) > 0) { cdq sub eax, edx sar eax, 1 xor ecx, ecx test eax, eax jle SHORT


LN7 @ get_expone
npad 5
LN7@get_expone npad 5


LL2 @ get_expone:
cdq
sub eax edx
sa r eax 1

; 126:// while((n = n>> 1)> 0){
; 127:exp ++;

inc ecx
test eax eax
jg 简短
LL2@get_expone: cdq sub eax, edx sar eax, 1 ; 126 : //while((n = n >> 1) > 0) { ; 127 : exp++; inc ecx test eax, eax jg SHORT


这篇关于表现不如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆