Tag: 不安全

将System.Decimal转换为System.Guid

我有一个大字典,其中键是十进制的,但System.Decimal的GetHashCode()非常糟糕。 为了certificate我的猜测,我运行了一个带有100.000 neigboring小数的for循环并检查了分布。 100.000个不同的十进制数仅使用2个(两个!!!)不同的哈希码。 十进制表示为16个字节。 就像Guid一样! 但是Guid的GetHashCode()发行版非常好。 如何在C#中将小数转换为Guid尽可能便宜? 不安全的代码没问题! 编辑:请求测试,所以这里是代码: decimal d = 96000000000000000000m; Dictionary hashcount = new Dictionary(); int length = 100000; for (int i = 0; i < length; i++) { int hashcode = d.GetHashCode(); int n; if (hashcount.TryGetValue(hashcode, out n)) { hashcount[hashcode] = n + 1; } else { hashcount.Add(hashcode, 1); } […]

如何在VB.Net中使用不安全的代码?

我想知道VB.NET相当于以下C#代码: unsafe { byte* pStart = (byte*)(void*)writeableBitmap.BackBuffer; int nL = writeableBitmap.BackBufferStride; for (int r = 0; r < 16; r++) { for (int g = 0; g < 16; g++) { for (int b = 0; b < 16; b++) { int nX = (g % 4) * 16 + b; int nY = r*4 […]

真正的不安全代码性能

我知道不安全的代码更适合访问Windows API之类的东西并进行不安全的类型转换,而不是编写更高性能的代码,但我想问你是否注意到使用它时在实际应用程序中有任何显着的性能改进与安全的c#代码相比。

C#不安全值类型数组到字节数组转换

我使用扩展方法将float数组转换为字节数组: public static unsafe byte[] ToByteArray(this float[] floatArray, int count) { int arrayLength = floatArray.Length > count ? count : floatArray.Length; byte[] byteArray = new byte[4 * arrayLength]; fixed (float* floatPointer = floatArray) { fixed (byte* bytePointer = byteArray) { float* read = floatPointer; float* write = (float*)bytePointer; for (int i = 0; i < […]