J
John Mark Howell
I had a customer call about some C# code they had put together that was
handling some large arrays. The performance was rather poor. The C# code
runs in about 22 seconds and the equivalent C++.Net code runs in 0.3
seconds. Can someone help me understand why the C# code performance is so
poor? I rewote the C# code to use a single dimenional array and the time
went down to about 3 seconds, but that's still no explaination as to why the
two dimenional array performance is so bad. I tried this on both C# 1.1 and
C# 2.0.
The original code was:
public void TestLoop4OldMethod()
{
double[,] emisbase = new double[1000,8784];
double[,] vombase = new double[1000,8784];
int Iteration_Index;
int Hourly_Index;
double differenceInSeconds;
DateTime myDateTime1;
DateTime myDateTime2;
myDateTime1 = DateTime.UtcNow;
for (int i1 = 0; i1 < 10; )
{
for (Hourly_Index = 0; Hourly_Index < 8784; )
{
for (Iteration_Index = 0; Iteration_Index < 1000; )
{
emisbase[Iteration_Index, Hourly_Index] = Iteration_Index *
Iteration_Index;
vombase[Iteration_Index, Hourly_Index] = emisbase[Iteration_Index,
Iteration_Index];
Iteration_Index++;
}
Hourly_Index++;
}
//Console.WriteLine("Here we are - Loop 4: {0}", i1);
i1++;
}
myDateTime2 = DateTime.UtcNow;
TimeSpan ts = myDateTime2 - myDateTime1;
differenceInSeconds = ts.TotalMilliseconds / 1000;
Console.WriteLine("RunTime in Seconds - Left Most Index + array
reference: {0} ", differenceInSeconds);
Console.WriteLine(" ");
}
It runs in about 22 seconds. Here I rewrote the code in C++ and it runs in
0.3 seconds:
// -------------------------------------------------------------------------
-------------------------------------------
int Iteration_Index;
int Hourly_Index;
double differenceInSeconds;
DateTime myDateTime1;
DateTime myDateTime2;
double * emisbase = new double[1000,8784];
double * vombase = new double[1000,8784];
myDateTime1 = DateTime::UtcNow;
for (int i1 = 0; i1 < 10; )
{
for (Hourly_Index = 0; Hourly_Index < 8784; Hourly_Index++)
{
for (Iteration_Index = 0; Iteration_Index < 1000; Iteration_Index++)
{
emisbase[Iteration_Index, Hourly_Index] = Iteration_Index *
Iteration_Index;
vombase[Iteration_Index, Hourly_Index] = emisbase[Iteration_Index,
Iteration_Index];
//Iteration_Index++;
}
//Hourly_Index++;
}
//Console::Write(S"Here we are - Loop 4: ");
//Console::WriteLine(Convert::ToString(i1));
i1++;
}
myDateTime2 = DateTime::UtcNow;
TimeSpan ts = myDateTime2 - myDateTime1;
differenceInSeconds = ts.TotalMilliseconds / 1000;
Console::Write(S"RunTime in Seconds - Left Most Index + array reference:
");
Console::WriteLine(Convert::ToString(differenceInSeconds));
// -------------------------------------------------------------------------
-------------------------------------------
handling some large arrays. The performance was rather poor. The C# code
runs in about 22 seconds and the equivalent C++.Net code runs in 0.3
seconds. Can someone help me understand why the C# code performance is so
poor? I rewote the C# code to use a single dimenional array and the time
went down to about 3 seconds, but that's still no explaination as to why the
two dimenional array performance is so bad. I tried this on both C# 1.1 and
C# 2.0.
The original code was:
public void TestLoop4OldMethod()
{
double[,] emisbase = new double[1000,8784];
double[,] vombase = new double[1000,8784];
int Iteration_Index;
int Hourly_Index;
double differenceInSeconds;
DateTime myDateTime1;
DateTime myDateTime2;
myDateTime1 = DateTime.UtcNow;
for (int i1 = 0; i1 < 10; )
{
for (Hourly_Index = 0; Hourly_Index < 8784; )
{
for (Iteration_Index = 0; Iteration_Index < 1000; )
{
emisbase[Iteration_Index, Hourly_Index] = Iteration_Index *
Iteration_Index;
vombase[Iteration_Index, Hourly_Index] = emisbase[Iteration_Index,
Iteration_Index];
Iteration_Index++;
}
Hourly_Index++;
}
//Console.WriteLine("Here we are - Loop 4: {0}", i1);
i1++;
}
myDateTime2 = DateTime.UtcNow;
TimeSpan ts = myDateTime2 - myDateTime1;
differenceInSeconds = ts.TotalMilliseconds / 1000;
Console.WriteLine("RunTime in Seconds - Left Most Index + array
reference: {0} ", differenceInSeconds);
Console.WriteLine(" ");
}
It runs in about 22 seconds. Here I rewrote the code in C++ and it runs in
0.3 seconds:
// -------------------------------------------------------------------------
-------------------------------------------
int Iteration_Index;
int Hourly_Index;
double differenceInSeconds;
DateTime myDateTime1;
DateTime myDateTime2;
double * emisbase = new double[1000,8784];
double * vombase = new double[1000,8784];
myDateTime1 = DateTime::UtcNow;
for (int i1 = 0; i1 < 10; )
{
for (Hourly_Index = 0; Hourly_Index < 8784; Hourly_Index++)
{
for (Iteration_Index = 0; Iteration_Index < 1000; Iteration_Index++)
{
emisbase[Iteration_Index, Hourly_Index] = Iteration_Index *
Iteration_Index;
vombase[Iteration_Index, Hourly_Index] = emisbase[Iteration_Index,
Iteration_Index];
//Iteration_Index++;
}
//Hourly_Index++;
}
//Console::Write(S"Here we are - Loop 4: ");
//Console::WriteLine(Convert::ToString(i1));
i1++;
}
myDateTime2 = DateTime::UtcNow;
TimeSpan ts = myDateTime2 - myDateTime1;
differenceInSeconds = ts.TotalMilliseconds / 1000;
Console::Write(S"RunTime in Seconds - Left Most Index + array reference:
");
Console::WriteLine(Convert::ToString(differenceInSeconds));
// -------------------------------------------------------------------------
-------------------------------------------