2008-11-12 5 views
1

는 C#으로 다음과 같은 서명을 고려해 다음 구현 사이의 성능 차이가두 정수 값을 나누고 .NET에서 부동 소수점 지수를 얻는 가장 좋은 방법은 무엇입니까?

double Divide(int numerator, int denominator); 

있습니까?

return (double)numerator/denominator; 

return numerator/(double)denominator; 

return (double)numerator/(double)denominator; 

위의 두 가지 모두 같은 대답을 반환한다고 가정합니다.

다른 해결책이 누락 되었습니까?

답변

5

IL을 비교해 보았습니까 (예 : Reflector)?

static double A(int numerator, int denominator) 
{ return (double)numerator/denominator; } 

static double B(int numerator, int denominator) 
{ return numerator/(double)denominator; } 

static double C(int numerator, int denominator) 
{ return (double)numerator/(double)denominator; } 

될 세 (이름을 제공하거나 소요)

:

.method private hidebysig static float64 A(int32 numerator, int32 denominator) cil managed 
{ 
    .maxstack 8 
    L_0000: ldarg.0 // pushes numerator onto the stack 
    L_0001: conv.r8 // converts the value at the top of the stack to double 
    L_0002: ldarg.1 // pushes denominator onto the stack 
    L_0003: conv.r8 // converts the value at the top of the stack to double 
    L_0004: div  // pops two values, divides, and pushes the result 
    L_0005: ret  // pops the value from the top of the stack as the return value 
} 

그래서 더 : 정확히 제로 차이가 없다.

1

VB.NET을 사용하는 경우에도 분자와 분모는 실제 나누기를 수행하기 전에 double로 변환되므로 예제가 동일합니다.

관련 문제