相关疑难解决方法(0)

通过转换为uint而不是检查负值来执行范围检查是否更有效?

我在.NET的List源代码中偶然发现了这段代码:

// Following trick can reduce the range check by one
if ((uint) index >= (uint)_size) {
  ThrowHelper.ThrowArgumentOutOfRangeException();
}
Run Code Online (Sandbox Code Playgroud)

显然这比(?)更有效 if (index < 0 || index >= _size)

我很好奇这个技巧背后的理由.单个分支指令真的比两个转换要贵uint吗?或者是否还有一些其他优化会使这个代码比另外的数字比较更快?

为了解决房间里的大象:是的,这是微优化,不,我不打算在我的代码中到处使用它 - 我只是好奇;)

c# performance micro-optimization numeric-conversion range-checking

77
推荐指数
5
解决办法
4439
查看次数

RyuJIT没有充分利用SIMD内在函数

我正在运行一些使用的C#代码,System.Numerics.Vector<T>但据我所知,我没有得到SIMD内在函数的全部好处.我正在使用Visual Studio Community 2015和Update 1,而我的clrjit.dll是v4.6.1063.1.

我正在使用英特尔酷睿i5-3337U处理器,它实现了AVX指令集扩展.因此,我想,我应该能够在256位寄存器上执行大多数SIMD指令.例如,拆卸中应包含的指令vmovups,vmovupd,vaddups,等...,并且Vector<float>.Count应该返回8,Vector<double>.Count应该是4,等等......但是,这不是我所看到的.

相反,我的拆卸包含指令等movups,movupd,addups等...以下代码:

WriteLine($"{Vector<byte>.Count} bytes per operation");
WriteLine($"{Vector<float>.Count} floats per operation");
WriteLine($"{Vector<int>.Count} ints per operation");
WriteLine($"{Vector<double>.Count} doubles per operation");
Run Code Online (Sandbox Code Playgroud)

生产:

16 bytes per operation
4 floats per operation
4 ints per operation
2 doubles per operation
Run Code Online (Sandbox Code Playgroud)

我哪里错了?要查看所有项目设置等,可在此处获得该项目.

c# sse simd avx ryujit

15
推荐指数
1
解决办法
1428
查看次数

为什么C#arithmetic on double似乎比long算术快?

以下代码的令人惊讶的输出显示double上的算术比long更快100%:

Test_DivOperator Float算术测量时间:15974.5024 ms.

Test_DivOperator整数算术测量时间:28548.183 ms.

使用的构建设置是.Net4.5 C#5.0(平台目标:x64)

使用的硬件是Intel Core i5-2520M(运行Windows7 64Bit)

注意:使用的运算符(此处为除法)确实会影响结果,除法最大化此观察结果

const int numOfIterations = 1; //this value takes memory access out of the game
const int numOfRepetitions = 500000000; //CPU bound application
Random rand = new Random();
double[] Operand1 = new double[numOfIterations];
double[] Operand2 = new double[numOfIterations];
double[] Operand3 = new double[numOfIterations];

long[] Int64Operand1 = new long[numOfIterations];
long[] Int64Operand2 = new long[numOfIterations];
long[] Int64Operand3 = new long[numOfIterations];

for (int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c# floating-point double performance long-integer

6
推荐指数
1
解决办法
1328
查看次数