2016-04-26 86 views
0

我只是做一些測試同步與異步,並寫下面的程序來測試。 也許我做錯了或者我只是不知道/明白異步正確,爲什麼我的程序的異步版本比同步版本慢?

我看到我的同步版本需要318 ms而異步需要18764 ms

static void Main(string[] args) 
     { 
      int num = 25; 
      Task[] tasks = new Task[num]; 

      Stopwatch sw = new Stopwatch(); 
      sw.Start(); 
      for (int i = 0; i < num; i++) 
      { 
       int c = i; 
       tasks[i] = Task.Factory.StartNew(() => { RunAsync(c).Wait(); }); 
      } 
      Task.WaitAll(tasks); 
      sw.Stop(); 
      Console.WriteLine($"FINISHED (Async) in {sw.ElapsedMilliseconds} ms"); 

      sw.Start(); 
      for (int i = 0; i < num; i++) 
      { 
       RunSync(i + 100); 
      } 
      sw.Stop(); 
      Console.WriteLine($"FINISHED (Sync) in {sw.ElapsedMilliseconds} ms"); 

      Console.ReadLine(); 
     } 

     private static void RunSync(int index) 
     { 
      FileStream stream = new FileStream(@"c:\\test\ff\tests.txt", FileMode.Open, FileAccess.Read, FileShare.Read); 
      string pp = Path.Combine(@"c:\\test\ff", "threadS-" + index + ".txt"); 
      FileStream sw = File.Create(pp); 
      byte[] buffer = new byte[1024]; 
      long bytesRead = 0; 
      long bytestoRead = stream.Length; 
      try 
      { 
       while (bytesRead < bytestoRead) 
       { 
        int count = stream.Read(buffer, 0, buffer.Length); 
        bytesRead += count; 
        sw.Write(buffer, 0, count); 
       } 
      } 
      finally 
      { 
       sw.Close(); 
       stream.Close(); 
      } 
     } 

     private async static Task RunAsync(int index) 
     { 
      FileStream stream = new FileStream(@"c:\\test\ff\tests.txt", FileMode.Open, FileAccess.Read, FileShare.Read); 
      int tId = Thread.CurrentThread.ManagedThreadId; 

      string pp = Path.Combine(@"c:\\test\ff", "thread-" + index + ".txt"); 
      FileStream sw = File.Create(pp); 
      byte[] buffer = new byte[1024]; 
      long bytesRead = 0; 
      long bytestoRead = stream.Length; 
      try 
      { 
       while (bytesRead < bytestoRead) 
       { 
        int count = await stream.ReadAsync(buffer, 0, buffer.Length); 
        bytesRead += count; 
        await sw.WriteAsync(buffer, 0, count); 
       } 
      } 
      finally 
      { 
       sw.Close(); 
       stream.Close(); 
      } 
     } 
+1

另外http://stackoverflow.com/questions/27189082/async-await-makes-my-tasks-slow-while-task-start-runs-them-fast – trailmax

+1

和http://stackoverflow.com/a/15666011/809357 – trailmax

+1

很好的解釋爲什麼使用異步http://stackoverflow.com/a/15665968/809357 – trailmax

回答

1

有你在做錯誤有兩件事情,以及你開始的錯誤前提。因此,讓我開始與錯誤的前提:

async/await旨在保持一個應用程序響應或分發工作的許多內核之間 - 他們不一定提高運行時的性能。

換句話說,當您查看整體吞吐量時,您可能需要處理比連續處理工作單元更多的工作。但是,該閾值將隨着任何給定時間的工作量而變化。

正確處理async/await意味着不要將較舊的Task函數與較新的支持混合使用。這會失去異步函數的所有好處並增加同步開銷。 當您想要等待後臺工作完成時,切勿致電Task.Wait()Task.WaitAll(tasks)這迫使一個線程完全暫停並且無響應,直到後臺工作完成。

你要做出如下調整:

 for (int i = 0; i < num; i++) 
     { 
      int c = i; 
      tasks[i] = RunAsync(c); 
     } 
     await Task.WhenAll(tasks); 

因爲你不能讓你的Main功能async,你可能需要的是呼叫轉移到另一個功能,所以你可以做異步/ AWAIT協議。

0

異步調用不一定有確保的執行時間。當調度程序決定資源可用時,它們將被調用。因此,完成它們的等待時間可能比同時調用同一方法的時間要高,因爲保證在調用時實時同步調用。

相關問題