[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using System; using System.Linq; using System.Diagnostics; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); foreach (var number in numbers) { sum += (ulong)number; } stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }実行結果はこうなります。
sum : 4999999950000000 449.1242ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, number => { sum += (ulong)number; }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }実行結果はこうなります。
sum : 1852281973816843 1711.2386ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, number => { lock (numbers) { sum += (ulong)number; } }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }
sum : 4999999950000000 9717.7726ms
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { static void Main(string[] args) { var numbers = Enumerable.Range(0, 100000000).ToArray(); ulong sum = 0; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Parallel.ForEach( numbers, () => (ulong)0, (number, state, localSum) => { return localSum + (uint)number; }, threadLocalSum => { lock (numbers) { sum += threadLocalSum; } }); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } }
sum : 4999999950000000 580.938ms実行時間がさっきよりは短くなりました。
using System; using System.Linq; using System.Diagnostics; using System.Threading.Tasks; class Program { const int iteration = 1000; const int numbers2Count = 10000; static void Main(string[] args) { var numbers = Enumerable.Range(0, iteration); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var sum = serialSum(numbers); //var sum = parallelSum(numbers); stopwatch.Stop(); Console.WriteLine("sum : " + sum); Console.WriteLine(stopwatch.Elapsed.TotalMilliseconds + "ms"); Console.ReadLine(); } private static ulong serialSum(System.Collections.Generic.IEnumerable<int> numbers) { ulong sum = 0; foreach (var number in numbers) { var numbers2 = Enumerable.Range(0, numbers2Count); sum += (ulong)numbers2.Sum(); } return sum; } private static ulong parallelSum(System.Collections.Generic.IEnumerable<int> numbers) { ulong sum = 0; Parallel.ForEach( numbers, () => (ulong)0, (number, state, threadLocalSum) => { return threadLocalSum + (ulong)Enumerable.Range(0, numbers2Count).Sum(); }, threadLocalSum => { lock (numbers) { sum += threadLocalSum; } }); return sum; } }serialSum() : 83.5064ms