File:CopyTo and ForEach method and foreach iteration of Dictionary.svg
Size of this PNG preview of this SVG file: 500 × 374 pixels. Other resolutions: 320 × 239 pixels | 640 × 479 pixels | 1,024 × 766 pixels | 1,280 × 957 pixels | 2,560 × 1,915 pixels.
Original file (SVG file, nominally 500 × 374 pixels, file size: 116 KB)
File information
Structured data
Captions
Summary edit
DescriptionCopyTo and ForEach method and foreach iteration of Dictionary.svg |
English: Processing time of ForEach method (and CopyTo method) and foreach iteration of Dictionary Class in C#, by DELL Vostro 200.
This shows that Foreach takes longer than foreach.
日本語: C#のDictionaryクラスで、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:
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.
This file is licensed under the Creative Commons Attribution-Noncommercial 3.0 Unported license. | |
|
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.http://www.gnu.org/copyleft/fdl.htmlGFDLGNU Free Documentation Licensetruetrue |
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;
// 2倍ずつ計算
for(int n = 1; n < N; n *= 2)
{
// 計算するデータの生成
Dictionary<int, Sample> testData = new Dictionary<int, Sample>(n);
for(int i = 0; i < n; i++)
{
// 0, 1, 2...に10.5, 11.5, 12.5 ...を格納
testData.Add(i, new Sample() { Value = n + i });
}
// 普通にforeachで
double sum1 = 0;
stopwatch.Reset();
stopwatch.Start();
foreach(Sample sample in testData.Values)
{
sum1 += sample.Value;
}
long time1 = stopwatch.ElapsedTicks;
// 配列にコピーしてから、Array.Foreachメソッド
double sum2 = 0;
stopwatch.Reset();
stopwatch.Start();
Sample[] samples = new Sample[testData.Count];
testData.Values.CopyTo(samples, 0);
Array.ForEach<Sample>(samples, (sample) =>
{
sum2 += 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/Time | Thumbnail | Dimensions | User | Comment | |
---|---|---|---|---|---|
current | 02:47, 7 April 2010 | 500 × 374 (116 KB) | 青子守歌 (talk | contribs) | {{User:青子守歌/own work| |en = Processing time of ForEach method (and CopyTo method) and foreach iteration of Dictionary Class in C#, by [http://supportapj.dell.com/support/topics/topic.aspx/jp/shared/support/jp/product_suppo |
You cannot overwrite this file.
File usage on Commons
The following page uses this file:
Metadata
This file contains additional information such as Exif metadata which may have been added by the digital camera, scanner, or software program used to create or digitize it. If the file has been modified from its original state, some details such as the timestamp may not fully reflect those of the original file. The timestamp is only as accurate as the clock in the camera, and it may be completely wrong.
Short title | foeach and ForEach method |
---|