2009-05-16 1 views
7

Code Complete 2 (601 및 602 페이지)에는 "공통 작업 비용"표가 있습니다.C#의 일반적인 작업 비용?

기본 작업 정수 할당에 값 1이 부여 된 다음 Java 및 C++에 대해 일반적인 작업의 상대 시간이 나열됩니다. 예 :

        C++  Java 
Integer assignment    1    1 
Integer division     5    1.5 
Floating point square root  15    4 

질문은 누구나이 데이터를 C#으로 받았습니까? 나는 이것이 내가 특별히 문제를 해결하는 데 도움이되지 않을 것이라는 것을 안다. 나는 단지 궁금하다.

+0

을 찾을 수 없습니다 당신이 on his blog 요청한 것과 관련 조치를했다 (그리고 모든 .NET 언어에서 동일합니다.) –

+0

lol @ simplistic benchmarks. – gbjbaanb

+0

조숙 한 최적화의 조숙 한 최적화처럼 보입니다.이 작업은 어셈블리 코드에서 실행되지만 벤치 마크는 실제 런타임에서 실행되며 IMHO에서는 기계 명령어가 아니라 벤치 마크가 느린 지 테스트합니다. –

답변

4

소스에서 직접, Know what things cost.

IIRC 리코 마리아니 (나는 thoe 중 하나가 "DEV"책갈피를 twohudnred에 그것을 알고 ...)하지만, 내가 더 이상

10

나는이 책에서 몇 가지 테스트를 구현했다.

테스트 실행 # 1 : 내 컴퓨터에서 일부 원시 데이터

TestIntegerAssignment 00 : 00 : 00.6680000
TestCallRoutineWithNoParameters 00 : 00 : 00.9780000
TestCallRoutineWithOneParameter 00 : 00 : 00.6580000
TestCallRoutineWithTwoParameters 00 : 00 : 00.9650000
TestIntegerAddition 00 : 00 : 00 : 00 : 00.6410000
TestIntegerSubtraction 00.9630000
TestIntegerMultiplication 00 : 00 : 00.6490000
012,351,TestIntegerDivision 00 : 00 : 00 : 00 : 00.9720000
TestFloatingPointDivision 00.6500000
TestFloatingPointSquareRoot 00 : 00 : 00.9790000
TestFloatingPointSine 00 : 00 : 00.6410000
TestFloatingPointLogarithm 00 : 00 : 41.1410000
TestFloatingPointExp 00 : 00 : 34.6310000

테스트 실행 # 2 : 00 : 00 : 00

TestIntegerAssignment 00.6750000
TestCallRoutineWithNoParameters 00 : 00 : 00.9720000 01,238,007,316,275,523,149 00 : TestCallRoutineWithOneParameter 00 00.6490000
TestCallRoutineWithTwoParameters 00 : 00 : 00.9750000
TestIntegerAddition 00 : 00 : 00.6730000
TestIntegerSubtraction 00 : 00 : 00 : 00 : 01.0300000
TestIntegerMultiplication 00.7000000
TestIntegerDivision 00 : 00 : 01.1120000
TestFloatingPointDivision 00 : 00 : 00.6630000
TestFloatingPointSquareRoot 00 : 00 : 00 : 00 : 00.9860000
TestFloatingPointSine 00.6530000
TestFloatingPointLogarithm 00 : 00 : 39.1150000
TestFloatingPointExp 00 : 00 : 33.8730000

테스트 실행 # 3 :

TestIntegerAssignment 00 : 00 : 00.6590000
TestCallRoutineWithNoParameters 00 : 00 : 00.9700000
TestCallRoutineWithOneParameter 00 : 00 : 00.6680000
TestCallRoutineWithTwoParameters 00 : 00 : 00.9900000
TestIntegerAddition 00 : 00 : 00.6720000
TestIntegerSubtraction 00:00:00.00 : 9,770,000
TestIntegerMultiplication 00 : 00 : 00 00.6580000
TestIntegerDivision 00.9930000
TestFloatingPointDivision 00 : 00 : 00.6740000
TestFloatingPointSquareRoot 00 : 00 : 01.0120000
TestFloatingPointSine 00 : 00 : 00.6700000
TestFloatingPointLogarithm 00 : 00 : 39.1020000
TestFloatingPointExp 00 : 00 : 35.3560000

(,415,139에서 확인할 존 소총의 microbenchmarking 프레임 워크를 사용하여 최적화, AMD 애슬론 X2의 3.0GHz의 컴파일 벤치 마크 당 1 억 개 시험, 093,103,210)

출처 :

class TestBenchmark 
{ 
[Benchmark] 
public static void TestIntegerAssignment() 
{ 
int i = 1; 
int j = 2; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j; 
    } 
} 

[Benchmark] 
public static void TestCallRoutineWithNoParameters() 
{ 
    for (int x = 0; x < 1000000000; x++) 
    { 
     TestStaticRoutine(); 
    } 
} 

[Benchmark] 
public static void TestCallRoutineWithOneParameter() 
{ 
    for (int x = 0; x < 1000000000; x++) 
    { 
     TestStaticRoutine2(5); 
    } 
} 

[Benchmark] 
public static void TestCallRoutineWithTwoParameters() 
{ 
    for (int x = 0; x < 1000000000; x++) 
    { 
     TestStaticRoutine3(5,7); 
    } 
} 

[Benchmark] 
public static void TestIntegerAddition() 
{ 
    int i = 1; 
    int j = 2; 
    int k = 3; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j + k; 
    } 
} 

[Benchmark] 
public static void TestIntegerSubtraction() 
{ 
    int i = 1; 
    int j = 6; 
    int k = 3; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j - k; 
    } 
} 

[Benchmark] 
public static void TestIntegerMultiplication() 
{ 
    int i = 1; 
    int j = 2; 
    int k = 3; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j * k; 
    } 
} 


[Benchmark] 
public static void TestIntegerDivision() 
{ 
    int i = 1; 
    int j = 6; 
    int k = 3; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j/k; 
    } 
} 

[Benchmark] 
public static void TestFloatingPointDivision() 
{ 
    float i = 1; 
    float j = 6; 
    float k = 3; 

    for (int x = 0; x < 1000000000; x++) 
    { 
     i = j/k; 
    } 
} 

[Benchmark] 
public static void TestFloatingPointSquareRoot() 
{ 
    double x = 1; 
    float y = 6; 

    for (int x2 = 0; x2 < 1000000000; x2++) 
    { 
     x = Math.Sqrt(6); 
    } 
} 

[Benchmark] 
public static void TestFloatingPointSine() 
{ 
    double x = 1; 
    float y = 6; 

    for (int x2 = 0; x2 < 1000000000; x2++) 
    { 
     x = Math.Sin(y); 
    } 
} 

[Benchmark] 
public static void TestFloatingPointLogarithm() 
{ 
    double x = 1; 
    float y = 6; 

    for (int x2 = 0; x2 < 1000000000; x2++) 
    { 
     x = Math.Log(y); 
    } 
} 

[Benchmark] 
public static void TestFloatingPointExp() 
{ 
    double x = 1; 
    float y = 6; 

    for (int x2 = 0; x2 < 1000000000; x2++) 
    { 
     x = Math.Exp(6); 
    } 
} 

private static void TestStaticRoutine() { 

} 

private static void TestStaticRoutine2(int i) 
{ 

} 

private static void TestStaticRoutine3(int i, int j) 
{ 

} 

private static class TestStaticClass 
{ 

} 
+4

그게 바로 거기에 헌신;) –

+0

나는 (Console.WriteLine 같은) 결과로 뭔가를하고 싶다. 절대적으로 최적화되지 않았 음을 확신한다. 아마 그렇지는 않지만 ... –

+0

그의 손에 너무 많은 시간을 가진 사람 (고맙게도) – inspite

1

그것은 합리적인 질문,하지만 특히 자바와 C#에서, 본 적이 거의 모든 성능 문제가 아래로 삶은 : 추상화의

  • 너무 많은 레이어 , 및
  • 이벤트 기반 통지 스타일 코딩에 의존합니다.

기본 작업과 관련이 거의 없거나 전혀 없습니다.

추상화 문제는 작업량이 무거워 질 때까지 괜찮습니다. 각 계층은 일반적으로 작은 성능 패널티를 나타내며, 이는 복잡한 방식으로 누적됩니다. 이 시점에서 해결 방법이 필요합니다. (나는 StringBuilder가 그러한 대안의 예라고 생각한다.)

주기적인 프로세스에 의해 일관성있게 유지되는 단순한 데이터 구조와는 달리 이벤트 기반 통지 스타일 코딩의 문제점은 간단한 액션처럼 보일 수있다. 속성을 값으로 설정하는 것과 같이 데이터 구조 전체에 걸쳐 예상되는 것보다 훨씬 많은 작업을 수행하면 파급 효과가 발생할 수 있습니다.