[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Copy arrays quickly in C#

It's easy enough to use a for loop to copy an array, but for most arrays you can get even better performance by using the Array class's Copy method. This takes as parameters the source and destination arrays. A third parameter tells how many entries to copy. Optional parameters let you copy from one part of the source array to another part of the destination array.

The following code compares the times needed to copy an integer array by using a for loop and by using Array.Copy.

// Run the trials. private void btnGo_Click(object sender, EventArgs e) { lblForLoop.Text = ""; lblArrayCopy.Text = ""; Cursor = Cursors.WaitCursor; Application.DoEvents(); int num_items = int.Parse(txtNumItems.Text); int num_trials = int.Parse(txtNumTrials.Text); int[] array1 = new int[num_items]; int[] array2 = new int[num_items]; // Initialize the first array. for (int i = 0; i < num_items; i++) { array1[i] = i; } DateTime start_time, stop_time; TimeSpan elapsed; // Use a for loop. start_time = DateTime.Now; for (int trial = 0; trial < num_trials; trial++) { for (int i = 0; i < num_items; i++) { array2[i] = array1[i]; } } stop_time = DateTime.Now; elapsed = stop_time - start_time; lblForLoop.Text = elapsed.TotalSeconds.ToString("0.00") + " seconds"; Application.DoEvents(); // Use Array.Copy. start_time = DateTime.Now; for (int trial = 0; trial < num_trials; trial++) { Array.Copy(array1, array2, array1.Length); } stop_time = DateTime.Now; elapsed = stop_time - start_time; lblArrayCopy.Text = elapsed.TotalSeconds.ToString("0.00") + " seconds"; Cursor = Cursors.Default; }

The code gets the number of items it should copy in each array and the number of trials it should perform. Then for each trial, the code uses a for loop to copy the array's values and displays the elapsed time. It then copies the values using Array.Copy. The code that performs the two kinds of trials is highlighted in blue.

If you look at the picture shown above, you'll see that Array.Copy is much faster, in this example taking about 1/17th as long as a for loop.

Note that the total time is extremely small (only about 7 nanoseconds per item using a for loop) in either case. Also note that a for loop may be faster for very small arrays.

A bigger benefit to Array.Copy than the speed advantage is the fact that the code is more concise and easier to read, at least when you get used to it.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.