z̃Rs[1słɂ́HmC#^VBnF.NET TIPS - IT

z̃Rs[1słɂ́HmC#^VBnF.NET TIPS

zRs[ɂ́Afor^foreach[vg@邪AArrayNXCopy\bhĝԊȒPőx̖ʂłLłB

» 2017N0426 0500 J
[R{NFCBluewaterSoft^Microsoft MVP for Windows Development]

Aږڎ

@zRs[̂for[vforeach[v̂ʓ|ƎvƂ͂Ȃ낤H@ArrayNXiSystemOԁj̋@\g΁Aꂪ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

zRs[ɂ́H

@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

3ʂ̕@ŔzRs[R\[Av̗iFC#AFVBj
2zRs[ꍇAfor[vł2d[vɂȂAforeach[vł̓CfbNX̊Ǘʓ|BŌɎArrayNXCopy\bhł́Aꂪ1sōςނ̂B

zRs[鎞Ԃv

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

z̃Rs[ɂ鎞Ԃ𑪒肷iC#j
5000‚̔zA3ʂ~23񂸂ƒRs[ĂBǂ̂炢̎Ԃ邩́AۂɎĂ݂Ăقi炭1ȂjBƂ́A[XrhɂāAiVisual Studioł͂ȂjGNXv[[Ȃǂ璼ڋNB
M҂̊‹ł́ÃR[h̏ꍇAArrayNXCopy\bhfor[v2炢Bfor[vforeach[vł́Aforeach[v̕⑬iz̏ꍇ̓CfbNXǗif̂ŁAt]邾낤jB̂悤Ȑx̑x̏ꍇA₿ƂR[ḧႢɂďʂւ邱Ƃ悭Bł邾ۂɋ߂̃eXgƂȂ悤ɐS|ĂقB

܂Ƃ

@ArrayNXCopy\bhg΁i1zɌ肷ȂzCopyTo\bhłjAz̃Rs[1sŏBxIɂLȂ̂ŁARs[Ȃ牽̏Ƃ̂łȂ΁ACopy\bhgB

u.NET TIPSv

Copyright© Digital Advantage Corp. All Rights Reserved.

RSSɂ‚

ACeBfBAIDɂ‚

[}KWo^

IT̃[}KẂA AׂĖłBЃ[}KWwǂB

archive