我在玩D语言,并将我写的一个用于测试性能的C#示例项目进行了移植。我不会说我没有惊讶,因为这是我第一次尝试用D语言编写代码,但是D语言的效率比C#慢得多。
简而言之,C#执行代码需要32秒,而D语言需要54秒。这段代码有点长,无法作为片段展示,如果有人能抽出一点时间解释一下为什么会发生这种情况/我的D代码有什么问题,那就太好了。
以下是我代码的一小部分:
简而言之,C#执行代码需要32秒,而D语言需要54秒。这段代码有点长,无法作为片段展示,如果有人能抽出一点时间解释一下为什么会发生这种情况/我的D代码有什么问题,那就太好了。
以下是我代码的一小部分:
void ProcessReel(int reelIndex, string pattern, Game game)
{
counter++;
if(reelIndex == game.Reels.length)
{
Symbol winningSymbol;
auto patternLength = 0;
auto p3_1 = pattern[0..3];
auto p3_2 = pattern[2..5];
auto p4_1 = pattern[0..4];
auto p4_2 = pattern[1..5];
auto pos = GetFromDict(p3_1, p3_2, game);
if(pos != null)
{
winningSymbol = pos._Symbol;
patternLength = pos.PatternLength;
}
pos = GetFromDict(p4_1, p4_2, game);
if(pos != null)
{
winningSymbol = pos._Symbol;
patternLength = pos.PatternLength;
}
if(pattern in patternTable)
{
auto combination = patternTable[pattern];
winningSymbol = combination._Symbol;
patternLength = combination.PatternLength;
}
if(winningSymbol !is null)
{
winningSymbol.Combinations++;
}
return;
}
auto reel = game.Reels[reelIndex];
for(int i = 0; i < reel.ReelStrip.length; i++)
{
auto p = pattern;
auto sym = reel.ReelStrip[i];
p ~= std.conv.to!string(sym);
ProcessReel(reelIndex + 1, p, game);
}
}
完整代码可以从这里下载。
我正在使用以下编译器参数进行编译。
dmd Test.d -O -release -inline -boundscheck=off
谢谢。
更新
这是C#代码。它应该并行运行,所以里面有一个锁,但我将其串行化以进行公平竞争,然而即使有这个锁,C#仍然比D快。
public static void ProcessReel(int reelIndex, Dictionary<int, Dictionary<byte, WinPossibility>> dict, string pattern, AnalyzedResult result)
{
if (reelIndex == result.Game.ReelCollection[0].Reels.Count) // This is the last reel. Time to analyze those shit :((
{
Symbol winningSymbol = null;
var patternLength = 0;
var p3_1 = pattern.Substring(0, 3);
var p3_2 = pattern.Substring(2, 3);
var p4_1 = pattern.Substring(0, 4);
var p4_2 = pattern.Substring(1, 4);
if (result.PatternTable.ContainsKey(p3_1) || result.PatternTable.ContainsKey(p3_2))
{
var winCombination = Helper.GetFromDict(p3_1, p3_2, result);
var symbol = winCombination.Symbol;
winningSymbol = symbol;
patternLength = winCombination.PatternLength;
}
if (result.PatternTable.ContainsKey(p4_1) || result.PatternTable.ContainsKey(p4_2))
{
var winCombination = Helper.GetFromDict(p4_1, p4_2, result);
var symbol = winCombination.Symbol;
winningSymbol = symbol;
patternLength = winCombination.PatternLength;
}
if (result.PatternTable.ContainsKey(pattern))
{
var winCombination = result.PatternTable[pattern];
var symbol = winCombination.Symbol;
winningSymbol = symbol;
patternLength = winCombination.PatternLength;
}
if (winningSymbol != null)
{
lock (dict)
{
var d = dict[patternLength];
if (d.ContainsKey(winningSymbol.Code))
d[winningSymbol.Code].Combinations += 1;
else
d.Add(winningSymbol.Code, new WinPossibility(winningSymbol, patternLength, 1));
}
}
return;
}
var reel = result.Game.ReelCollection[0].Reels[reelIndex];
//if (reelIndex == 0)
//{
// Parallel.For(0, reel.ReelStrip.Length, r1 =>
// {
// var p = pattern;
// var sym = reel.ReelStrip[r1];
// p += sym;
// ProcessReel(reelIndex + 1, dict, p, result);
// });
//}
//else
//{
for (int r1 = 0; r1 < reel.ReelStrip.Length; r1++)
{
var p = pattern;
var sym = reel.ReelStrip[r1];
p += sym;
ProcessReel(reelIndex + 1, dict, p, result);
}
//}
}
更新2
我刚刚下载了GDCC 4.9。它有所帮助,计算时间现在从54秒降至38秒,但仍然不及.NET/C#...