File:ForEach method and foreach iteration of array.svg

Original file(SVG file, nominally 500 × 369 pixels, file size: 113 KB)

Captions

Captions

Add a one-line explanation of what this file represents

Summary edit

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

Licensing edit

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.
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.
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.
You may select the license of your choice.

C# source code edit

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; }
		}
	}
}

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current02:45, 7 April 2010Thumbnail for version as of 02:45, 7 April 2010500 × 369 (113 KB)青子守歌 (talk | contribs){{User:青子守歌/own work| |en = Processing time of ForEach method (and CopyTo method) and foreach iteration of array Class in C#, by [http://supportapj.dell.com/support/topics/topic.aspx/jp/shared/support/jp/product_support_ce