简体   繁体   English

.Net,最有效的比较

[英].Net, most efficient comparison

I have 2 arrays of N objects, xs and ys.我有 2 个包含 N 个对象的数组,xs 和 ys。 The objects are boxed ints.对象是装箱的整数。 There are no null elements in either arrays.两个数组中都没有空元素。 1 >= N < 50 1 >= N < 50

I can compare them with the following code:我可以将它们与以下代码进行比较:

for (var i = 0; i < N; i++)
{
    if (!xs[i].Equals(ys[i]))
    {
        return false;
    }
}

return true;

My question is: using .Net JIT or CLR tricks or some transformation, can I optimize this algorithm further?我的问题是:使用 .Net JIT 或 CLR 技巧或一些转换,我可以进一步优化这个算法吗?

If these aren't boxed you have a few more options, also this would basically only start to pay with larger arrays.如果这些没有装箱,那么您还有更多选择,这基本上只会开始为更大的阵列付费。

Also as mentioned in the comments, you could use parallel.同样如评论中所述,您可以使用并行。 But plinq and TPL probably won't give you any positive benefit with such small arrays, and will most definitely give a lot more overhead.但是 plinq 和 TPL 可能不会为您提供如此小的数组带来的任何积极好处,而且肯定会带来更多的开销。

public static unsafe bool UnsafeCompare(int[] ary1, int[] ary2)
{
   fixed (int* pAry1 = ary1, pAry2 = ary2)
   {
      var pLen = pAry1 + ary1.Length;
      for (int* p1 = pAry1, p2 = pAry2; p1 < pLen; p1++, p2++)
         if (*p1 != *p2)
            return false;
   }
   return true;
}

or或者

[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)
{
   fixed (int* pAry1 = ary1, pAry2 = ary2)
   {
      return memcmp((IntPtr)pAry1, (IntPtr)pAry2, (IntPtr)(ary2.Length * sizeof(int))) == 0;
   }
}

Anyway, if you really are after performance, I'd be getting out your benchmarker and seeing what works for you.无论如何,如果你真的追求表现,我会拿出你的基准测试器,看看什么对你有用。

Just an interesting note, this is the source for memcmp (or close enough):只是一个有趣的说明,这是 memcmp 的来源(或足够接近):

memcmp (const PTR str1, const PTR str2, size_t count)
{
  register const unsigned char *s1 = (const unsigned char*)str1;
  register const unsigned char *s2 = (const unsigned char*)str2;

  while (count-- > 0)
    {
      if (*s1++ != *s2++)
      return s1[-1] < s2[-1] ? -1 : 1;
    }
  return 0;
}

Benchmarks基准

Take this with a grain of salt, however these tests were run a massive 100,000 times to try to tease out any subtle differences.对此持保留态度,但是这些测试运行了 100,000 次以试图找出任何细微的差异。

   ----------------------------------------------------------------------------
Mode             : Release (64Bit)
Test Framework   : .NET Framework 4.7.1 (CLR 4.0.30319.42000)
----------------------------------------------------------------------------
Operating System : Microsoft Windows 10 Pro
Version          : 10.0.17134
----------------------------------------------------------------------------
CPU Name         : Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz
Description      : Intel64 Family 6 Model 42 Stepping 7
Cores (Threads)  : 4 (8)      : Architecture  : x64
Clock Speed      : 3401 MHz   : Bus Speed     : 100 MHz
L2Cache          : 1 MB       : L3Cache       : 8 MB
----------------------------------------------------------------------------

Results结果

--- Standard input ------------------------------------------------------------
| Value    |    Average |    Fastest |    Cycles | Garbage | Test |      Gain |
--- Scale 5 ---------------------------------------------------- Time 0.475 ---
| Unsafe   |  54.271 ns |   0.000 ns |   1.850 K | 0.000 B | Pass |   16.95 % |
| Original |  65.349 ns |   0.000 ns |   1.820 K | 0.000 B | Base |    0.00 % |
| Memcmp   | 143.527 ns |   0.000 ns |   1.977 K | 0.000 B | Pass | -119.63 % |
--- Scale 50 --------------------------------------------------- Time 0.483 ---
| Unsafe   |  78.542 ns |   0.000 ns |   1.936 K | 0.000 B | Pass |   36.69 % |
| Original | 124.064 ns |   0.000 ns |   2.038 K | 0.000 B | Base |    0.00 % |
| Memcmp   | 181.076 ns |   0.000 ns |   2.066 K | 0.000 B | Pass |  -45.95 % |
--- Scale 500 -------------------------------------------------- Time 0.620 ---
| Memcmp   | 445.434 ns | 300.000 ns |   3.044 K | 0.000 B | Pass |   44.14 % |
| Unsafe   | 501.585 ns | 300.000 ns |   3.341 K | 0.000 B | Pass |   37.10 % |
| Original | 797.435 ns | 300.000 ns |   4.291 K | 0.000 B | Base |    0.00 % |
--- Scale 5,000 ------------------------------------------------ Time 2.172 ---
| Memcmp   |   3.519 µs |   2.701 µs |  13.625 K | 0.000 B | Pass |   46.95 % |
| Unsafe   |   5.110 µs |   4.502 µs |  19.084 K | 0.000 B | Pass |   22.96 % |
| Original |   6.633 µs |   5.703 µs |  24.364 K | 0.000 B | Base |    0.00 % |
--- Scale 50,000 ---------------------------------------------- Time 25.561 ---
| Memcmp   |  52.378 µs |  35.422 µs | 180.681 K | 0.000 B | Pass |   34.55 % |
| Unsafe   |  74.634 µs |  49.832 µs | 257.216 K | 0.000 B | Pass |    6.74 % |
| Original |  80.031 µs |  62.740 µs | 274.704 K | 0.000 B | Base |    0.00 % |
--- Scale 500,000 --------------------------------------------- Time 38.306 ---
| Memcmp   | 505.916 µs | 447.289 µs |   1.726 M | 0.000 B | Pass |   37.20 % |
| Unsafe   | 662.644 µs | 590.781 µs |   2.262 M | 0.000 B | Pass |   17.75 % |
| Original | 805.634 µs | 675.736 µs |   2.748 M | 0.000 B | Base |    0.00 % |
-------------------------------------------------------------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 进行字符串比较的最有效方法是什么? - What is the most efficient way to do the String comparison? 在.NET中处理大型csv的最有效方法 - Most efficient way to process a large csv in .NET .NET中UniqueQueue和UniqueReplacementQueue集合的最有效实现 - The Most Efficient Implementation of UniqueQueue and UniqueReplacementQueue Collections in .NET 从2个大表中获取数据比较统计信息的最有效方法是什么 - What is the most efficient way to get comparison statistics for data from 2 large tables 在.NET中通过线路移动对象最有效的方法是什么? - What method is most efficient at moving objects across the wire in .NET? 在ASP.NET MVC中创建数据库回调的最有效方法 - Most Efficient Way to Create a DB Callback in ASP.NET MVC 排序数组中x / n元素的最有效方法-.NET - Most efficient way to sort for x/n elements in an array - .NET 在 .NET Core 3.1 中写入响应正文的最有效方法 - Most efficient way to write to response body in .NET Core 3.1 .NET Microframework中找到三重最大的最有效方法? - most efficient way to find largest of three doubles in .NET Microframework? 将内存流与文件 C# .NET 进行比较的最有效方法 - Most efficient way to compare a memorystream to a file C# .NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM