Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A perfect example is described in book J.Richter "CLR via C#", 3 edition, Ch. 16: The following C# code demonstrates three techniques (safe, jagged, and unsafe), for accessing a two-dimensional array:</p> <pre><code>using System; using System.Diagnostics; public static class Program { private const Int32 c_numElements = 10000; public static void Main() { const Int32 testCount = 10; Stopwatch sw; // Declare a two-dimensional array Int32[,] a2Dim = new Int32[c_numElements, c_numElements]; // Declare a two-dimensional array as a jagged array (a vector of vectors) Int32[][] aJagged = new Int32[c_numElements][]; for (Int32 x = 0; x &lt; c_numElements; x++) aJagged[x] = new Int32[c_numElements]; // 1: Access all elements of the array using the usual, safe technique sw = Stopwatch.StartNew(); for (Int32 test = 0; test &lt; testCount; test++) Safe2DimArrayAccess(a2Dim); Console.WriteLine("{0}: Safe2DimArrayAccess", sw.Elapsed); // 2: Access all elements of the array using the jagged array technique sw = Stopwatch.StartNew(); for (Int32 test = 0; test &lt; testCount; test++) SafeJaggedArrayAccess(aJagged); Console.WriteLine("{0}: SafeJaggedArrayAccess", sw.Elapsed); // 3: Access all elements of the array using the unsafe technique sw = Stopwatch.StartNew(); for (Int32 test = 0; test &lt; testCount; test++) Unsafe2DimArrayAccess(a2Dim); Console.WriteLine("{0}: Unsafe2DimArrayAccess", sw.Elapsed); Console.ReadLine(); } private static Int32 Safe2DimArrayAccess(Int32[,] a) { Int32 sum = 0; for (Int32 x = 0; x &lt; c_numElements; x++) { for (Int32 y = 0; y &lt; c_numElements; y++) { sum += a[x, y]; } } return sum; } private static Int32 SafeJaggedArrayAccess(Int32[][] a) { Int32 sum = 0; for (Int32 x = 0; x &lt; c_numElements; x++) { for (Int32 y = 0; y &lt; c_numElements; y++) { sum += a[x][y]; } } return sum; } private static unsafe Int32 Unsafe2DimArrayAccess(Int32[,] a) { Int32 sum = 0; fixed (Int32* pi = a) { for (Int32 x = 0; x &lt; c_numElements; x++) { Int32 baseOfDim = x * c_numElements; for (Int32 y = 0; y &lt; c_numElements; y++) { sum += pi[baseOfDim + y]; } } } return sum; } } </code></pre> <p>The Unsafe2DimArrayAccess method is marked with the unsafe modifier, which is required to use C#’s fixed statement. To compile this code, you’ll have to specify the /unsafe switch when invoking the C# compiler or check the “Allow Unsafe Code” check box on the Build tab of the Project Properties pane in Microsoft Visual Studio. When I run this program on my machine, I get the following output:</p> <pre><code>00:00:02.0017692: Safe2DimArrayAccess 00:00:01.5197844: SafeJaggedArrayAccess 00:00:01.7343436: Unsafe2DimArrayAccess </code></pre> <p>As you can see, the safe two-dimensional array access technique is the slowest. The safe jagged array access technique takes a little less time to complete than the safe two-dimensional array access technique. However, you should note that creating the jagged array is more time-consuming than creating the multi-dimensional array because creating the jagged array requires an object to be allocated on the heap for each dimension, causing the garbage collector to kick in periodically. So there is a trade-off: If you need to create a lot of “multidimensional arrays” and you intend to access the elements infrequently, it is quicker to create a multi-dimensional array. If you need to create the “multi-dimensional array” just once, and you access its elements frequently, a jagged array will give you better performance. Certainly, in most applications, the latter scenario is more common.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload