次の C# コードは、VS2008 よりも VS2010 でビルドすると実行速度が遅くなるようです: Core i5 Win7 x64 8 GB RAM PC では、VS2008 でビルドされたバージョンは約 7.5 秒で文字列をソートしますが、VS2010 でビルドされたバージョンでは約 9 秒かかります。何故ですか?
私のコードに何か問題がありますか?
VS2010 で並べ替えアルゴリズムが変更されましたか?
基になる CLR に、パフォーマンスを低下させる何か違いはありますか?
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace StringSortCSharp
{
/// <summary>
/// Console app to test string sorting performance in C#.
/// </summary>
class Program
{
/// <summary>
/// Displays the first lines from a vector of strings.
/// </summary>
/// <param name="wishedN">Number of lines to display.</param>
/// <param name="lines">Source lines to display.</param>
private static void DisplayFirst(int wishedN, List<string> lines)
{
int n = Math.Min(wishedN, lines.Count);
for (int i = 0; i < n; i++)
{
Console.WriteLine(" " + lines[i]);
}
Console.WriteLine();
}
/// <summary>
/// Used for random permutation.
/// </summary>
private static Random random = new Random();
/// <summary>
/// Computes a random permutation of the input sequence.
///
/// From:
/// http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp
///
/// </summary>
/// <typeparam name="T">Type stored in the sequences.</typeparam>
/// <param name="sequence">Input sequence.</param>
/// <returns>Random permutation of the input sequence.</returns>
private static IEnumerable<T> RandomPermutation<T>(IEnumerable<T> sequence)
{
T[] retArray = sequence.ToArray();
for (int i = 0; i < retArray.Length - 1; i += 1)
{
int swapIndex = random.Next(i + 1, retArray.Length);
T temp = retArray[i];
retArray[i] = retArray[swapIndex];
retArray[swapIndex] = temp;
}
return retArray;
}
/// <summary>
/// Builds a list of strings used in the performance benchmark.
/// </summary>
/// <returns>Test list of strings.</returns>
private static List<string> BuildTestLines()
{
// Start with "Lorem ipsum", and repeat it several times, adding some suffix strings.
var lorem = new string[]
{
"Lorem ipsum dolor sit amet, consectetuer adipiscing elit.",
"Maecenas porttitor congue massa. Fusce posuere, magna sed",
"pulvinar ultricies, purus lectus malesuada libero,",
"sit amet commodo magna eros quis urna.",
"Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.",
"Pellentesque habitant morbi tristique senectus et netus et",
"malesuada fames ac turpis egestas. Proin pharetra nonummy pede.",
"Mauris et orci."
};
int repeatCount = 200 * 1000;
Console.Write("Building test strings");
var testLines = new List<string>();
Console.Write(" (total string count = {0})", repeatCount * lorem.Length);
Console.Write("...");
for (int i = 0; i < repeatCount; i++)
{
for (int j = 0; j < lorem.Length; j++)
{
// Add more stuff to Lorem strings
testLines.Add(lorem[j] + " (#" + i + ")");
}
}
Console.WriteLine("done.");
DisplayFirst(5, testLines);
Console.WriteLine();
// Shuffle the previously built strings.
Console.Write("Shuffling strings...");
var randomLines = new List<string>(RandomPermutation(testLines));
Console.WriteLine("done.");
DisplayFirst(5, randomLines);
Console.WriteLine();
return randomLines;
}
/// <summary>
/// Sort the input lines.
/// </summary>
/// <param name="lines">Input lines to sort.</param>
private static void Test(List<string> lines)
{
// Stopwatch to measure time performance
var timer = new Stopwatch();
Console.Write("Sorting " + lines.Count + " lines...");
// Sort benchmark
timer.Start();
lines.Sort();
timer.Stop();
Console.WriteLine("done.");
// Display results
DisplayFirst(5, lines);
Console.WriteLine();
Console.WriteLine((timer.ElapsedMilliseconds / 1000.0).ToString(CultureInfo.InvariantCulture) + " seconds elapsed.");
}
static void Main(string[] args)
{
Console.WriteLine("*** Testing String Sorting in C# ***");
Console.WriteLine();
// Build test lines used for the sort benchmark
List<string> testLines = BuildTestLines();
// Run the sort test
Test(testLines);
}
}
}