.NET에서 두 가지 적분 값을 나누고 부동 소수점 지수를 얻는 가장 성능있는 방법은 무엇입니까?
-
08-07-2019 - |
문제
C#의 다음 서명을 고려하십시오.
double Divide(int numerator, int denominator);
다음 구현간에 성능 차이가 있습니까?
return (double)numerator / denominator;
return numerator / (double)denominator;
return (double)numerator / (double)denominator;
위의 두 가지 모두 동일한 답을 반환한다고 가정합니다.
다른 동등한 솔루션을 놓쳤습니까?
해결책
IL을 비교해 보셨습니까 (예 : 반사기)?
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
}
아니요 : 정확히 제로 차이가 있습니다.
다른 팁
vb.net을 사용하더라도 실제 부문을 수행하기 전에 분자와 분모 모두 복식으로 변환되므로 예제는 동일합니다.
제휴하지 않습니다 StackOverflow