File:ForEach method and foreach iteration of array.svg

Summary

Description
English: Processing time of ForEach method (and CopyTo method) and foreach iteration of array Class in C, by DELL Vostro 200. This shows that Foreach takes longer than foreach.
日本語: Cの配列で、ForEachメソッド(とCopyToメソッド)を使った場合と、foreachで繰り返しを行なった場合の処理時間(DELL Vostro 200を使用)。 Foreachのほうがforeachより時間がかかっていることがわかる。
Date
Source Own work
Author aokomoriuta(青子守歌)
Other versions
Category:Figures by aokomoriuta

Licensing

I, the copyright holder of this work, hereby publish it under the following licenses:
w:en:Creative Commons
attribution share alike
This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported, 2.5 Generic, 2.0 Generic and 1.0 Generic license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.
Category:CC-BY-SA-3.0,2.5,2.0,1.0#ForEach%20method%20and%20foreach%20iteration%20of%20array.svg
Creative Commons license
Creative Commons Attribution iconCreative Commons Noncommercial icon
This file is licensed under the Creative Commons Attribution-Noncommercial 3.0 Unported license.
You are free:
  • to share – to copy, distribute and transmit the work
  • to remix – to adapt the work
Under the following conditions:
  • attribution – You must attribute the work in the manner specified by the author or :licensor (but not in any way that suggests that they endorse you or your use of the work).
  • noncommercial – You may not use this work for commercial purposes.
Category:CC-BY-NC-3.0
GNU head Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled GNU Free Documentation License.
Category:License migration redundant#ForEach%20method%20and%20foreach%20iteration%20of%20array.svgCategory:GFDL#ForEach%20method%20and%20foreach%20iteration%20of%20array.svg
You may select the license of your choice.
Category:Self-published work

C# source code

using System.Collections.Generic;
using System;

namespace LWisteria.Foreach
{
	/// <summary>
	/// Foreachの性能比較クラス
	/// </summary>
	static class Foreach
	{
		/// <summary>
		/// エントリポイント
		/// </summary>
		/// <returns>終了コード</returns>
		static int Main()
		{
			// ストップウォッチ
			System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
			
			// データ数
			const int N = 10000000;

			// 繰り返す回数
			const int M = 100;
			
			// 2倍ずつ計算
			for(int n = 1; n < N; n *= 2)
			{
				// 計算するデータの生成

				Sample[] samples = new Sample[n];

				for(int i = 0; i < n; i++)
				{
					// 0, 1, 2...に10.5, 11.5, 12.5 ...を格納
					samples[i] = new Sample() { Value = n + i };
				}

				// foreach
				double sum1 = 0;

				stopwatch.Reset();
				stopwatch.Start();
				foreach(Sample sample in samples)
				{
					for(int i = 0; i < M; i++)
					{
						sum1 += System.Math.Sqrt(sample.Value);
					}
				}
				long time1 = stopwatch.ElapsedTicks;

				// Array.Foreachメソッド
				double sum2 = 0;

				stopwatch.Reset();
				stopwatch.Start();
				Array.ForEach<Sample>(samples, (sample) =>
				{
					for(int i = 0; i < M; i++)
					{
						sum2 += System.Math.Sqrt(sample.Value);
					}
				});
				long time2 = stopwatch.ElapsedTicks;

				// 結果の表示
				Console.WriteLine("{0}, {1}, {2}", n, time1, time2);
			}

			// 終了コードを返す
			return Environment.ExitCode;
		}

		/// <summary>
		/// サンプルクラス(参照型)
		/// </summary>
		sealed class Sample
		{
			/// <summary>
			/// 値
			/// </summary>
			public double Value { set; get; }
		}
	}
}
Category:C Sharp Category:Images with C sharp source code
Category:CC-BY-NC-3.0 Category:CC-BY-SA-3.0,2.5,2.0,1.0 Category:C Sharp Category:Figures by aokomoriuta Category:GFDL Category:Images with C sharp source code Category:License migration redundant Category:Pages using deprecated source tags Category:Self-published work