zRs[ɂ́Afor^foreach[vg@邪AArrayNXCopy\bhĝԊȒPőx̖ʂłLłB
@zRs[̂for[vforeach[v̂ʓ|ƎvƂ͂Ȃ낤H@ArrayNXiSystemOԁj̋@\gAꂪ1sŏ̂łB
@List<T>NXȂǂ֗̕ȃRNVg킸ɂ킴킴zĝ́AxdĂꍇɑ낤BŖ{eł́AzRs[@ƂƂɁARs[ԂvR[h̗ЉB
@ȂAz.NET Framework̍ŏ炠̂A{e͂ȍ~̓e܂łBTvR[ĥ܂ɂ́AVisual Studio 2015i܂͂ȍ~jKvłB
@ArrayNXCopy\bhgƁA1sŃRs[łB
@ɑzłfor[v^foreach[vŃRs[R[ĥ͖ʓ|Ȃ̂B2zfor[v^foreach[v^ArrayNXCopy\bhgĂꂼRs[̃R[hɎiR\[AvjB
@ȂA1z̏ꍇɂ́A̔zCopyTo\bhpłB
using System;
using System.Linq;
using static System.Console;
class Program
{
static void Main(string[] args)
{
// Rs[2z
int[,] src = { { 1, 2, 3 }, { 4, 5, 6 }, };
WriteLine($"Rs[̔zF{string.Join(", ", src.Cast<int>())}");
// óF 1, 2, 3, 4, 5, 6
// iȍ~̏o͗͏ȗj
// for[vŃRs[
int[,] dest1 = new int[src.GetLength(0), src.GetLength(1)];
for (int i = 0; i < src.GetLength(0); i++)
for (int j = 0; j < src.GetLength(1); j++)
dest1[i,j] = src[i,j];
WriteLine($"for[vŃRs[F{string.Join(", ", dest1.Cast<int>())}");
// foreach[vŃRs[
int[,] dest2 = new int[src.GetLength(0), src.GetLength(1)];
int index0 = 0, index1 = 0;
foreach (var n in src)
{
dest2[index0, index1++] = n;
if (index1 >= src.GetLength(1))
{
index0++; index1 = 0;
}
}
WriteLine($"foreach[vŃRs[F{string.Join(", ", dest2.Cast<int>())}");
// Array.CopyŃRs[
int[,] dest3 = new int[src.GetLength(0), src.GetLength(1)];
Array.Copy(src, dest3, src.Length);
WriteLine($"Array.CopyŃRs[F{string.Join(", ", dest3.Cast<int>())}");
ReadKey();
}
}
Imports System.Console
Module Module1
Sub Main()
' Rs[2z
Dim src As Integer(,) = {{1, 2, 3}, {4, 5, 6}}
WriteLine($"Rs[̔zF{String.Join(", ", src.Cast(Of Integer)())}")
' óF 1, 2, 3, 4, 5, 6
' iȍ~̏o͗͏ȗj
' For[vŃRs[
Dim dest1(src.GetLength(0) - 1, src.GetLength(1) - 1) As Integer
For i As Integer = 0 To (src.GetLength(0) - 1)
For j As Integer = 0 To (src.GetLength(1) - 1)
dest1(i, j) = src(i, j)
Next
Next
WriteLine($"For[vŃRs[F{String.Join(", ", dest1.Cast(Of Integer)())}")
' For Each[vŃRs[
Dim dest2(src.GetLength(0) - 1, src.GetLength(1) - 1) As Integer
Dim Index0 As Integer = 0, index1 As Integer = 0
For Each n In src
dest2(Index0, index1) = n
index1 += 1
If (index1 >= src.GetLength(1)) Then
Index0 += 1 : index1 = 0
End If
Next
WriteLine($"For Each[vŃRs[F{String.Join(", ",
dest2.Cast(Of Integer)())}")
' Array.CopyŃRs[
Dim dest3(src.GetLength(0) - 1, src.GetLength(1) - 1) As Integer
Array.Copy(src, dest3, src.Length)
WriteLine($"Array.CopyŃRs[F{String.Join(", ",
dest3.Cast(Of Integer)())}")
ReadKey()
End Sub
End Module
@ArrayNXCopy\bh́A1sŏ邾łȂAfor[v^foreach[vƂiReleaserh̏ꍇAM҂̎ł́A1z2炢jB
@List<T>NXȂǂ֗̕ȃWFlbNRNVg킸ɔzgŔAɏxCɂĂ邩炾낤BŁAɎ3ʂ̔zRs[@i1zj̑xĂ݂邽߂̃TvR[hɎBX̂ŁAR[hC#݂̂̂̂ƂĂB|CǵAŏ͈̐肵Ȃ̂ŁAOďԂ̕ς߂邱Ƃi̗ł͎s̃ʂ͈肵Ă邽߁AIȃKx[WRNV͂ĂȂjB
using System;
using System.Linq;
using static System.Console;
class Program
{
static void Main(string[] args)
{
const int Length = 50000000; // eXgɎgz̒
int[] a = new int[Length]; // Rs[̔z
for (int i = 0; i < Length; i++)
a[i] = i;
int[] b = new int[Length]; // Rs[̔z
// eXgp̒萔ƎsԊi[p̔z
const int LoopCount = 23;
long[] result1 = new long[LoopCount];
long[] result2 = new long[LoopCount];
long[] result3 = new long[LoopCount];
// oߎԌvɎgXgbvEHb`
var sw = new System.Diagnostics.Stopwatch();
// eXg{
for (int i=0; i< LoopCount; i++)
{
WriteLine($"{i+1}");
result1[i] = ArrayCopyTest1(a, b, sw);
result2[i] = ArrayCopyTest2(a, b, sw);
result3[i] = ArrayCopyTest3(a, b, sw);
WriteLine();
}
// ʂZoiŏ3͏Oĕϒl߂j
double ave1 = result1.Skip(3).Average();
double ave2 = result2.Skip(3).Average();
double ave3 = result3.Skip(3).Average();
WriteLine($"for[vŃRs[iρjF{ave1:#,##0.000}~b");
WriteLine($"foreach[vŃRs[iρjF{ave2:#,##0.000}~b");
WriteLine($"Array.CopyŃRs[iρjF{ave3:#,##0.000}~b");
// R\[Ă܂̂h
ReadKey();
}
static long ArrayCopyTest1(int[] a, int[] b, System.Diagnostics.Stopwatch sw)
{
sw.Restart();
for (int i = 0; i < a.Length; i++)
b[i] = a[i];
sw.Stop();
WriteLine($"for[vŃRs[F{sw.ElapsedMilliseconds:#,##0}~b");
Array.Clear(b, 0, b.Length);
return sw.ElapsedMilliseconds;
}
static long ArrayCopyTest2(int[] a, int[] b, System.Diagnostics.Stopwatch sw)
{
sw.Restart();
int index = 0;
foreach (var n in a)
b[index++] = n;
sw.Stop();
WriteLine($"foreach[vŃRs[F{sw.ElapsedMilliseconds:#,##0}~b");
Array.Clear(b, 0, b.Length);
return sw.ElapsedMilliseconds;
}
static long ArrayCopyTest3(int[] a, int[] b, System.Diagnostics.Stopwatch sw)
{
sw.Restart();
Array.Copy(a, b, a.Length);
sw.Stop();
WriteLine($"Array.CopyŃRs[F{sw.ElapsedMilliseconds:#,##0}~b");
Array.Clear(b, 0, b.Length);
return sw.ElapsedMilliseconds;
}
}
@ArrayNXCopy\bhgi1zɌ肷ȂzCopyTo\bhłjAz̃Rs[1sŏBxIɂLȂ̂ŁARs[Ȃ牽̏Ƃ̂łȂACopy\bhgB
p\o[WF.NET Framework 1.0ȍ~iTvR[hɂ͂ȍ~̋@\^\܂ށj
JeSFNXECu@ΏہFz
gpCuFArrayNXiSystemOԁj
֘ATIPSFC#Ŕz錾ɂ́H
֘ATIPSFVB.NETŔz錾ɂ́H
֘ATIPSFz̃TCYύXɂ́HiResizeҁjm2.0̂݁AC#AVBn
֘ATIPSFzƎ̏Ń\[giבւjɂ́H
֘ATIPSFNXɂz\[giבւjɂ́HiLINQŁjm3.5AC#AVBn
֘ATIPSFz̕Aɂ́H
֘ATIPSF\FNXɐÓI\bhĂяoɂ́HmC# 6.0n
֘ATIPSFVB.NETŃNXȗă\bh萔𗘗pɂ́H
֘ATIPSFlEl߂0߂ŕɂ́HmC#AVBn
֘ATIPSFVisual StudioŃR\[EAvP[ṼfobOsɃR}hEvvgȂ悤ɂɂ́H
Copyright© Digital Advantage Corp. All Rights Reserved.