Summary
While processing a large text file, I came across the following (unexpected) performance degradation that I can't explain. My objectives for this question are:
- Understand what causes the slowdown described below
- Find out how to initialize large non-primitive arrays quickly
The Problem
- Array contains non-primitive reference items
- Not
int[]
butMyComplexType[]
MyComplexType
is a class, not a structMyComplexType
contains somestring
properties
- Not
- Array is pre-allocated
- Array is large
- If an item is created and used without being assigned to the array, program is fast
- If an item is created and then assigned to an array item, program slows down significantly
- I expected array item assignment to be a simple reference assignment, but this does not seem to the the case based on my results for my test program below
Test program
Consider the following C#
program:
namespace Test
{
public static class Program
{
// Simple data structure
private sealed class Item
{
public Item(int i)
{
this.Name = "Hello " + i;
//this.Name = "Hello";
//this.Name = null;
}
public readonly string Name;
}
// Test program
public static void Main()
{
const int length = 1000000;
var items = new Item[length];
// Create one million items but don't assign to array
var w = System.Diagnostics.Stopwatch.StartNew();
for (var i = 0; i < length; i++)
{
var item = new Item(i);
if (!string.IsNullOrEmpty(item.Name)) // reference the item and its Name property
{
items[i] = null; // do not remember the item
}
}
System.Console.Error.WriteLine("Without assignment: " + w.Elapsed);
// Create one million items and assign to array
w.Restart();
for (var i = 0; i < length; i++)
{
var item = new Item(i);
if (!string.IsNullOrEmpty(item.Name)) // reference the item and its Name property
{
items[i] = item; // remember the item
}
}
System.Console.Error.WriteLine(" With assignment: " + w.Elapsed);
}
}
}
It contains two almost-identical loops. Each loop creates one million instances of Item
class. First loop uses the created item and then throws away the reference (not persisting it in the items
array). Second loop uses the created item and then stores the reference in the items
array. Array item assignment is the only difference between the loops.
My Results
When I run
Release
build (optimizations turned on) on my machine, I get the following results:Without assignment: 00:00:00.2193348 With assignment: 00:00:00.8819170
Loop with array assignment is significantly slower than the one without assignment (~4x slower).
If I change
Item
constructor to assign a constant string toName
property:public Item(int i) { //this.Name = "Hello " + i; this.Name = "Hello"; //this.Name = null; }
I get the following results:
Without assignment: 00:00:00.0228067 With assignment: 00:00:00.0718317
Loop with assignment is still ~3x slower than the one without
Finally if I assign
null
to theName
property:public Item(int i) { //this.Name = "Hello " + i; //this.Name = "Hello"; this.Name = null; }
I get the following result:
Without assignment: 00:00:00.0146696 With assignment: 00:00:00.0105369
Once no string is allocated, the version without assignment is finally slightly slower (I assume because all those instances are released for garbage collection)
Questions
Why is array item assignment slowing the test program so much?
Is there an attribute/language construct/etc that will speed up the assignment?
PS: I tried investigating the slowdown using dotTrace, but it was inconclusive. One thing I saw was a lot more string copying and garbage collection overhead in the loop with assignment than in the loop without assignment (even though I expected the reverse).