2012-01-06 2 views
-1

다음 두 프로그램 간의 성능 차이를 확인하려고 시도 중입니다. 그러나 나는 아무런 차이점도 발견하지 못했다. 이게 정상인가? 윈도우 코어 2 듀오 M/C 비주얼 스튜디오에서 실행 임 2010 Express Edition을계산 집약적 인 C# 프로그램의 이점이 있습니까?

프로그램 1 (100 개 실행에 대해 평균 : 824.11 밀리 초) :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.Threading; 


namespace MultiThreading 
{ 
    class Program 
    { 
     public static Stopwatch stopwatch; 
     static void Main(string[] args) 
     { 
      stopwatch = new Stopwatch(); 
      stopwatch.Start(); 
      //Thread t = new Thread(WriteY); 
      //t.Start(); 
      for (int i = 0; i < 10000; i++) 
      { 
       Console.Write("x{0} ", i); 
      } 

      WriteY(); 

      Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds); 
      Console.ReadLine(); 
     } 

     static void WriteY() 
     { 
      for (int i = 0; i < 10000; i++) 
      { 
       Console.Write("y{0} ", i); 
      } 
      //Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds); 
      //Console.ReadLine(); 
     } 

프로그램 2 (평균 100 실행 : 828.11 MS) :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Diagnostics; 
using System.Threading; 


namespace MultiThreading 
{ 
    class Program 
    { 
     public static Stopwatch stopwatch; 
     static void Main(string[] args) 
     { 
      stopwatch = new Stopwatch(); 
      stopwatch.Start(); 
      Thread t = new Thread(WriteY); 
      t.Start(); 
      for (int i = 0; i < 10000; i++) 
      { 
       Console.Write("x{0} ", i); 
      } 

      //WriteY(); 

      Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds); 
      Console.ReadLine(); 
     } 

     static void WriteY() 
     { 
      for (int i = 0; i < 10000; i++) 
      { 
       Console.Write("y{0} ", i); 
      } 
      //Console.WriteLine("Time taken in milliseconds: {0}", stopwatch.ElapsedMilliseconds); 
      //Console.ReadLine(); 
     } 
    } 
} 
+2

-1. 그대는 어떤 계산도하지 않으므로 "계산 집약적 인"것은 이것이 의미하는 기본적인 오해입니다. Isntead 당신이 할 일은 콘솔 출력 메커니즘에 의해 직렬화되는 Console.WriteLine IO입니다. – TomTom

답변

8

내 생각 엔 모두가 아마 한 번에 하나의 스레드 만이 액세스 할 수 있도록 자원 (화면)을 고정 할 필요가 Console.Write의 속도에 구속되어 있다는 점이다.

+0

정확합니다. 서로 얽매이지 않는 다른 스레드의 문자열로 분명합니다. – TomTom

4

항상 Console.WriteLine을 사용하기 때문에 응용 프로그램의 IO 바인딩이 문제입니다. IO를 사용하지 않는 무언가를하면 부스트가 보일 것입니다.

그리고 다른 답변에서 언급 한 바와 같이 WriteLine 실제로 동기화 않습니다 Calling Console.WriteLine from multiple threads

+0

'Console.WriteLine'이 없다면, 프로그램은 (거의) 거의 순간적으로 끝날 것이며, 두 번째 예제에서 회전하는 스레드에 더 많은 오버 헤드가 소요됩니다. 나는 첫번째 것을 끝내기를 기대할 것이다. 물론 루프 몸체가 없으면 컴파일러는 루프를 생략하여 (릴리스 모드에서) 스레드를 회전시키지 않고 스레드를 회전시키는 척도로 만듭니다. – spender

+0

@spender 예, 그것은 모두'WriteLine'에 의해 대체됩니다. 당신이 말한 것처럼 그것이 아무것도 대체되지 않으면 아마도 아무것도 대체되지 않을 것입니다. 그러나 큰 행렬 곱셈으로 바뀌면 실행 시간이 50 % 가량 줄어들 수 있습니다 (캐시 등에 따라 다름) –

관련 문제