File:Array List LinkedList Queue (C sharp).svg

Summary

Description
English: Processing time of double[](System.Array), System.Collections.Generic.List<double>, System.Collections.Generic.LinkedList<double>, System.Collections.Generic.Queue<double> on .NET Framework 4.0 with C#. See also #code.
日本語: .NET Framework 4.0のC#での、double[](System.Array), System.Collections.Generic.List<double>, System.Collections.Generic.LinkedList<double>, System.Collections.Generic.Queue<double>の処理時間の比較。コードも参照。
Date
Source Own work
Author aokomoriuta(青子守歌)
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#Array%20List%20LinkedList%20Queue%20(C%20sharp).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#Array%20List%20LinkedList%20Queue%20(C%20sharp).svgCategory:GFDL#Array%20List%20LinkedList%20Queue%20(C%20sharp).svg
You may select the license of your choice.
Category:Self-published work

code

using System;
using System.Collections.Generic;

namespace LWisteria.List
{
	/// <summary>
	/// Listの比較をするメインクラス
	/// </summary>
	static class ListMain
	{
		/// <summary>
		/// エントリーポイント
		/// </summary>
		/// <returns></returns>
		static int Main()
		{
			// ストップウォッチを生成
			System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

			// 最大要素数
			int maxN = 1000000;

			Console.WriteLine("#,Array,List,LinkedList,Queue");

			
			// 要素数を2倍ずつに増やしていく
			for(double m = 10; m < maxN; m *= new System.Random().NextDouble()/2 + 1)
			{
				// intにキャスト
				int n = (int)m;

				Console.Write("{0},", n);

				// ストップウォッチ開始
				stopwatch.Start();

				// Listを生成
				double[] array = new double[n];

				// nまで
				for(int i = 0; i < n; i++)
				{
					// √を計算して追加
					array[i] = System.Math.Sqrt(i);
				}

				// 合計値を初期化して、全要素について
				double sumArray = 0;
				foreach(double value in array)
				{
					// 合計する
					sumArray += value;
				}
				// ストップウォッチ停止
				stopwatch.Stop();

				// 結果出力
				Console.Write("{0},", stopwatch.ElapsedTicks);
				stopwatch.Reset();

				/*****************************************************/

				// ストップウォッチ開始
				stopwatch.Start();

				// Listを生成
				List<double> list = new List<double>();

				// nまで
				for(int i = 0; i < n; i++)
				{
					// √を計算して追加
					list.Add(System.Math.Sqrt(i));
				}

				// 合計値を初期化して、全要素について
				double sumList = 0;
				foreach(double value in list)
				{
					// 合計する
					sumList += value;
				}
				// ストップウォッチ停止
				stopwatch.Stop();

				// 結果出力
				Console.Write("{0},", stopwatch.ElapsedTicks);
				stopwatch.Reset();

				/*****************************************************/

				// ストップウォッチ開始
				stopwatch.Start();

				// LinkedListを生成
				LinkedList<double> linkedList = new LinkedList<double>();

				// nまで
				for(int i = 0; i < n; i++)
				{
					// √を計算して追加
					linkedList.AddLast(System.Math.Sqrt(i));
				}

				// 合計値を初期化して、全要素について
				double sumLinkedList = 0;
				foreach(double value in linkedList)
				{
					// 合計する
					sumLinkedList += value;
				}
				// ストップウォッチ停止
				stopwatch.Stop();

				// 結果出力
				Console.Write("{0},", stopwatch.ElapsedTicks);
				stopwatch.Reset();

				/*****************************************************/

				// ストップウォッチ開始
				stopwatch.Start();

				// Queueを生成
				Queue<double> queue = new Queue<double>();

				// nまで
				for(int i = 0; i < n; i++)
				{
					// √を計算して追加
					queue.Enqueue(System.Math.Sqrt(i));
				}

				// 合計値を初期化して、全要素について
				double sumQueue = 0;
				foreach(double value in queue)
				{
					// 合計する
					sumQueue += value;
				}
				// ストップウォッチ停止
				stopwatch.Stop();

				// 結果出力
				Console.Write("{0},", stopwatch.ElapsedTicks);
				stopwatch.Reset();

				/*****************************************************/

				Console.WriteLine("{0}", sumArray == sumList && sumList == sumLinkedList && sumLinkedList == sumQueue);
			}

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

[Hide]

Category:Images with C sharp source code Category:C Sharp
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:Self-published work