【发布时间】:2016-06-09 12:31:11
【问题描述】:
我正在将高性能 C++ 应用程序重写为 C#。 C# 应用程序明显比 C++ 原始应用程序慢。分析告诉我 C# 应用程序花费大部分时间来访问数组元素。因此,我创建了一个简单的数组访问基准。我得到的结果与others doing a similiar comparison 完全不同。
C++ 代码:
#include <limits>
#include <stdio.h>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int main(void)
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
int xRepLen = 100 * 1000;
int xRepCount = 1000;
unsigned short * xArray = new unsigned short[xRepLen];
for (int xIdx = 0; xIdx < xRepLen; xIdx++)
xArray[xIdx] = xIdx % USHRT_MAX;
int * xResults = new int[xRepLen];
for (int xRepIdx = 0; xRepIdx < xRepCount; xRepIdx++)
{
// in each repetition, find the first value, that surpasses xArray[xIdx] + 25 - i.e. we will perform 25 searches
for (int xIdx = 0; xIdx < xRepLen; xIdx++)
{
unsigned short xValToBreach = (xArray[xIdx] + 25) % USHRT_MAX;
xResults[xIdx] = 0;
for (int xIdx2 = xIdx + 1; xIdx2 < xRepLen; xIdx2++)
if (xArray[xIdx2] >= xValToBreach)
{
xResults[xIdx] = xIdx2; break;
}
if (xResults[xIdx] == 0)
xResults[xIdx] = INT_MAX;
}
}
high_resolution_clock::time_point t2 = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(t2 - t1).count();
cout << "Elasped miliseconds " << duration;
getchar();
}
C# 代码:
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace arrayBenchmarkCs
{
class Program
{
public static void benchCs()
{
unsafe
{
int xRepLen = 100 * 1000;
int xRepCount = 1000;
ushort[] xArr = new ushort[xRepLen];
for (int xIdx = 0; xIdx < xRepLen; xIdx++)
xArr[xIdx] = (ushort)(xIdx % 0xffff);
int[] xResults = new int[xRepLen];
Stopwatch xSw = new Stopwatch(); xSw.Start();
fixed (ushort * xArrayStart = & xArr [0])
{
for (int xRepIdx = 0; xRepIdx < xRepCount; xRepIdx++)
{
// in each repetition, go find the first value, that surpasses xArray[xIdx] + 25 - i.e. we will perform 25 searches
ushort * xArrayEnd = xArrayStart + xRepLen;
for (ushort* xPtr = xArrayStart; xPtr != xArrayEnd; xPtr++)
{
ushort xValToBreach = (ushort)((*xPtr + 25) % 0xffff);
int xResult = -1;
for (ushort * xPtr2 = xPtr + 1; xPtr2 != xArrayEnd; xPtr2++)
if ( *xPtr2 >= xValToBreach)
{
xResult = (int)(xPtr2 - xArrayStart);
break;
}
if (xResult == -1)
xResult = int.MaxValue;
// save result
xResults[xPtr - xArrayStart] = xResult;
}
}
} // fixed
xSw.Stop();
Console.WriteLine("Elapsed miliseconds: " + (xSw.ElapsedMilliseconds.ToString("0"));
}
}
static void Main(string[] args)
{
benchCs();
Console.ReadKey();
}
}
}
在我的工作计算机 (i7-3770) 上,C++ 版本比 C# 版本快大约 2 倍。在我的家用计算机 (i7-5820K) 上,C++ 比 C# 版本快 1.5 倍。两者都在 Release 中测量。我希望通过在 C# 中使用指针可以避免数组边界检查,并且两种语言的性能相同。
所以我的问题如下:
- 其他人发现 C# 的速度与 C++ 相同?
- 如果不通过指针,如何将 C# 性能提升到 C++ 级别?
- 不同计算机上不同加速的驱动因素可能是什么?
非常感谢任何提示, 丹尼尔
【问题讨论】:
-
我希望您在没有调试器的情况下执行基准测试(在 Visual Studio 中使用 CTRL+F5 而不是 F5)
-
@xanatos:是的。不过感谢您的快捷方式。
-
ushort * xArrayEnd = xArrayStart + xRepLen; 你可以把它移到
for循环之外 -
@xanatos:确实,性能差异保持不变。
-
您链接的基准测试的作者看起来偏向于 C# 而不是 C++。再加上他不共享基准代码的事实看起来很可疑……最后,
std::vector::operator[]()不检查边界。std::vector::at()确实,他一直都错了。
标签: c# c++ arrays performance