Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9364

【C#】ループステートメントの速度を検証してみた その2

$
0
0

前回、C#におけるループステートメントの速度検証をまとめてみました。検証方法が未熟な箇所が多々あったため、今回は@Midoliyさんにご紹介いただきましたBenchmarkDotNetを用いて検証を実施します。

実施すること

  1. 1億個の要素を持った配列およびリストを用意する。
  2. 準備した配列、リストに対して検証する。
    1. 単純にぐるぐる回す。
    2. ぐるぐる回しながら2の倍数を足し込んでいく。

対象のループステートメント

  1. for
  2. foreach
  3. do
  4. do-while
  5. LINQ(Query形式):足し込み検証のみ実施
  6. LINQ(Method形式):足し込み検証のみ実施

結果

単純にぐるぐる回す(配列)

MethodMeanErrorStdDevMedian
BenchFor_Array29.08 ms0.1590 ms0.1410 ms29.05 ms
BenchForEach_Array48.22 ms0.0428 ms0.0334 ms48.22 ms
BenchDo_Array28.93 ms0.0829 ms0.0735 ms28.93 ms
BenchDoWhile_Array36.09 ms2.6406 ms7.7859 ms33.17 ms

単純にぐるぐる回す(リスト)

MethodMeanErrorStdDevMedian
BenchFor_List36.53 ms0.4746 ms0.4439 ms36.40 ms
BenchForEach_List248.81 ms10.3583 ms12.3308 ms244.77 ms
BenchDo_List36.49 ms0.4889 ms0.4573 ms36.30 ms
BenchDoWhile_List36.19 ms0.1416 ms0.1182 ms36.19 ms

※配列とリストでforeachの速度がなぜここまで変わるのかは、@Tokeiyaさんの配列に対するforとforeachが非常にわかりやすかったです。

2の倍数の足し込み(配列)

MethodMeanErrorStdDevMedian
BenchForWithCalc_Array151.77 ms0.3014 ms0.2819 ms151.79 ms
BenchForEachWithCalc_Array136.51 ms0.1996 ms0.1867 ms136.46 ms
BenchDoWithCalc_Array151.72 ms0.2464 ms0.2305 ms151.65 ms
BenchDoWhileWithCalc_Array151.03 ms1.1535 ms1.0225 ms150.97 ms
BenchLINQQuery_Array611.51 ms5.0230 ms4.6985 ms613.74 ms
BenchLINQMethod_Array589.09 ms11.6493 ms11.4412 ms586.88 ms

2の倍数の足し込み(リスト)

MethodMeanErrorStdDevMedian
BenchForWithCalc_List169.25 ms0.3809 ms0.3563 ms169.37 ms
BenchForEachWithCalc_List299.09 ms3.3102 ms3.0963 ms300.62 ms
BenchDoWithCalc_List172.82 ms2.8077 ms2.1921 ms173.18 ms
BenchDoWhileWithCalc_List169.38 ms0.5669 ms0.4734 ms169.22 ms
BenchLINQQuery_List814.73 ms5.5605 ms5.2013 ms816.72 ms
BenchLINQMethod_List825.85 ms7.5451 ms6.6885 ms825.91 ms
Mean   : Arithmetic mean of all measurements
Error  : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Median : Value separating the higher half of all measurements (50th percentile)

検証ソース

usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;usingSystem.Collections.Generic;usingSystem.Linq;namespaceBenchmarks{publicclassLoopBenchmarkTest{privatestaticreadonlyintlistCnt=100_000_000;// 要素数privatestaticreadonlyint[]numArray;// 検証配列privatestaticreadonlyList<int>numList;// 検証リストstaticLoopBenchmarkTest(){numArray=Enumerable.Range(0,listCnt).ToArray();numList=newList<int>(numArray);}#region Array
[Benchmark]publiclongBenchFor_Array(){for(vari=0;i<=numArray.Length-1;i++){}return0;}[Benchmark]publiclongBenchForEach_Array(){foreach(varnuminnumArray){}return0;}[Benchmark]publiclongBenchDo_Array(){vari=0;while(i<=numArray.Length-1){i++;}return0;}[Benchmark]publiclongBenchDoWhile_Array(){vari=0;do{i++;}while(i<=numArray.Length-1);return0;}#endregion#region List
[Benchmark]publiclongBenchFor_List(){for(vari=0;i<=numList.Count-1;i++){}return0;}[Benchmark]publiclongBenchForEach_List(){foreach(varnuminnumList){}return0;}[Benchmark]publiclongBenchDo_List(){vari=0;while(i<=numList.Count-1){i++;}return0;}[Benchmark]publiclongBenchDoWhile_List(){vari=0;do{i++;}while(i<=numList.Count-1);return0;}#endregion
#region ArrayWithCalc
[Benchmark]publiclongBenchForWithCalc_Array(){vartotal=0;for(vari=0;i<=numArray.Length-1;i++){if(i%2==0)total+=numArray[i];}returntotal;}[Benchmark]publiclongBenchForEachWithCalc_Array(){vartotal=0;foreach(varnuminnumArray){if(num%2==0)total+=num;}returntotal;}[Benchmark]publiclongBenchDoWithCalc_Array(){vari=0;vartotal=0;while(i<=numArray.Length-1){if(i%2==0)total+=numArray[i];i++;}returntotal;}[Benchmark]publiclongBenchDoWhileWithCalc_Array(){vari=0;vartotal=0;do{if(i%2==0)total+=numArray[i];i++;}while(i<=numArray.Length-1);returntotal;}[Benchmark]publiclongBenchLINQQuery_Array(){vartotal=(fromxinnumArraywherex%2==0select(long)x).Sum();returntotal;}[Benchmark]publiclongBenchLINQMethod_Array(){vartotal=numArray.Where(x=>x%2==0).Sum(x=>(long)x);returntotal;}#endregion
#region ListWithCalc
[Benchmark]publiclongBenchForWithCalc_List(){vartotal=0;for(vari=0;i<=numList.Count-1;i++){if(i%2==0)total+=numList[i];}returntotal;}[Benchmark]publiclongBenchForEachWithCalc_List(){vartotal=0;foreach(varnuminnumList){if(num%2==0)total+=num;}returntotal;}[Benchmark]publiclongBenchDoWithCalc_List(){vari=0;vartotal=0;while(i<=numList.Count-1){if(i%2==0)total+=numList[i];i++;}returntotal;}[Benchmark]publiclongBenchDoWhileWithCalc_List(){vari=0;vartotal=0;do{if(i%2==0)total+=numList[i];i++;}while(i<=numList.Count-1);returntotal;}[Benchmark]publiclongBenchLINQQuery_List(){vartotal=(fromxinnumListwherex%2==0select(long)x).Sum();returntotal;}[Benchmark]publiclongBenchLINQMethod_List(){vartotal=numList.Where(x=>x%2==0).Sum(x=>(long)x);returntotal;}#endregion}classProgram{staticvoidMain(string[]args){BenchmarkRunner.Run<LoopBenchmarkTest>();}}}

Viewing all articles
Browse latest Browse all 9364

Latest Images

Trending Articles