猿问

.Net,最有效的比较

我有 2 个包含 N 个对象的数组,xs 和 ys。对象是装箱的整数。两个数组中都没有空元素。1 >= N < 50


我可以将它们与以下代码进行比较:


for (var i = 0; i < N; i++)

{

    if (!xs[i].Equals(ys[i]))

    {

        return false;

    }

}


return true;

我的问题是:使用 .Net JIT 或 CLR 技巧或一些转换,我可以进一步优化这个算法吗?


猛跑小猪
浏览 1107回答 1
1回答

开心每一天1111

如果这些没有装箱,那么您还有更多选择,这基本上只会开始为更大的阵列付费。同样如评论中所述,您可以使用并行。但是 plinq 和 TPL 可能不会为您提供如此小的数组带来的任何积极好处,而且肯定会带来更多的开销。public static unsafe bool UnsafeCompare(int[] ary1, int[] ary2){&nbsp; &nbsp;fixed (int* pAry1 = ary1, pAry2 = ary2)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; var pLen = pAry1 + ary1.Length;&nbsp; &nbsp; &nbsp; for (int* p1 = pAry1, p2 = pAry2; p1 < pLen; p1++, p2++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (*p1 != *p2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp;}&nbsp; &nbsp;return true;}或者[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]static extern int memcmp(IntPtr b1, IntPtr b2, IntPtr count);public static unsafe bool UnsafeCompare2(int[] ary1, int[] ary2){&nbsp; &nbsp;fixed (int* pAry1 = ary1, pAry2 = ary2)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; return memcmp((IntPtr)pAry1, (IntPtr)pAry2, (IntPtr)(ary2.Length * sizeof(int))) == 0;&nbsp; &nbsp;}}无论如何,如果你真的追求表现,我会拿出你的基准测试器,看看什么对你有用只是一个有趣的注释,这是 memcmp 的来源(或足够接近)memcmp (const PTR str1, const PTR str2, size_t count){&nbsp; register const unsigned char *s1 = (const unsigned char*)str1;&nbsp; register const unsigned char *s2 = (const unsigned char*)str2;&nbsp; while (count-- > 0)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; if (*s1++ != *s2++)&nbsp; &nbsp; &nbsp; return s1[-1] < s2[-1] ? -1 : 1;&nbsp; &nbsp; }&nbsp; return 0;}基准对此持保留态度,但是这些测试运行了 100,000 次以试图找出细微的差异。&nbsp; &nbsp;----------------------------------------------------------------------------Mode&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Release (64Bit)Test Framework&nbsp; &nbsp;: .NET Framework 4.7.1 (CLR 4.0.30319.42000)----------------------------------------------------------------------------Operating System : Microsoft Windows 10 ProVersion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 10.0.17134----------------------------------------------------------------------------CPU Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Intel(R) Core(TM) i7-2600 CPU @ 3.40GHzDescription&nbsp; &nbsp; &nbsp; : Intel64 Family 6 Model 42 Stepping 7Cores (Threads)&nbsp; : 4 (8)&nbsp; &nbsp; &nbsp; : Architecture&nbsp; : x64Clock Speed&nbsp; &nbsp; &nbsp; : 3401 MHz&nbsp; &nbsp;: Bus Speed&nbsp; &nbsp; &nbsp;: 100 MHzL2Cache&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 1 MB&nbsp; &nbsp; &nbsp; &nbsp;: L3Cache&nbsp; &nbsp; &nbsp; &nbsp;: 8 MB----------------------------------------------------------------------------结果--- Standard input ------------------------------------------------------------| Value&nbsp; &nbsp; |&nbsp; &nbsp; Average |&nbsp; &nbsp; Fastest |&nbsp; &nbsp; Cycles | Garbage | Test |&nbsp; &nbsp; &nbsp; Gain |--- Scale 5 ---------------------------------------------------- Time 0.475 ---| Unsafe&nbsp; &nbsp;|&nbsp; 54.271 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;1.850 K | 0.000 B | Pass |&nbsp; &nbsp;16.95 % || Original |&nbsp; 65.349 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;1.820 K | 0.000 B | Base |&nbsp; &nbsp; 0.00 % || Memcmp&nbsp; &nbsp;| 143.527 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;1.977 K | 0.000 B | Pass | -119.63 % |--- Scale 50 --------------------------------------------------- Time 0.483 ---| Unsafe&nbsp; &nbsp;|&nbsp; 78.542 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;1.936 K | 0.000 B | Pass |&nbsp; &nbsp;36.69 % || Original | 124.064 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;2.038 K | 0.000 B | Base |&nbsp; &nbsp; 0.00 % || Memcmp&nbsp; &nbsp;| 181.076 ns |&nbsp; &nbsp;0.000 ns |&nbsp; &nbsp;2.066 K | 0.000 B | Pass |&nbsp; -45.95 % |--- Scale 500 -------------------------------------------------- Time 0.620 ---| Memcmp&nbsp; &nbsp;| 445.434 ns | 300.000 ns |&nbsp; &nbsp;3.044 K | 0.000 B | Pass |&nbsp; &nbsp;44.14 % || Unsafe&nbsp; &nbsp;| 501.585 ns | 300.000 ns |&nbsp; &nbsp;3.341 K | 0.000 B | Pass |&nbsp; &nbsp;37.10 % || Original | 797.435 ns | 300.000 ns |&nbsp; &nbsp;4.291 K | 0.000 B | Base |&nbsp; &nbsp; 0.00 % |--- Scale 5,000 ------------------------------------------------ Time 2.172 ---| Memcmp&nbsp; &nbsp;|&nbsp; &nbsp;3.519 µs |&nbsp; &nbsp;2.701 µs |&nbsp; 13.625 K | 0.000 B | Pass |&nbsp; &nbsp;46.95 % || Unsafe&nbsp; &nbsp;|&nbsp; &nbsp;5.110 µs |&nbsp; &nbsp;4.502 µs |&nbsp; 19.084 K | 0.000 B | Pass |&nbsp; &nbsp;22.96 % || Original |&nbsp; &nbsp;6.633 µs |&nbsp; &nbsp;5.703 µs |&nbsp; 24.364 K | 0.000 B | Base |&nbsp; &nbsp; 0.00 % |--- Scale 50,000 ---------------------------------------------- Time 25.561 ---| Memcmp&nbsp; &nbsp;|&nbsp; 52.378 µs |&nbsp; 35.422 µs | 180.681 K | 0.000 B | Pass |&nbsp; &nbsp;34.55 % || Unsafe&nbsp; &nbsp;|&nbsp; 74.634 µs |&nbsp; 49.832 µs | 257.216 K | 0.000 B | Pass |&nbsp; &nbsp; 6.74 % || Original |&nbsp; 80.031 µs |&nbsp; 62.740 µs | 274.704 K | 0.000 B | Base |&nbsp; &nbsp; 0.00 % |--- Scale 500,000 --------------------------------------------- Time 38.306 ---| Memcmp&nbsp; &nbsp;| 505.916 µs | 447.289 µs |&nbsp; &nbsp;1.726 M | 0.000 B | Pass |&nbsp; &nbsp;37.20 % || Unsafe&nbsp; &nbsp;| 662.644 µs | 590.781 µs |&nbsp; &nbsp;2.262 M | 0.000 B | Pass |&nbsp; &nbsp;17.75 % || Original | 805.634 µs | 675.736 µs |&nbsp; &nbsp;2.748 M | 0.000 B | Base |&nbsp; &nbsp; 0.00 % |-------------------------------------------------------------------------------
随时随地看视频慕课网APP
我要回答