简体   繁体   English

哪个是比较两位数最高效的方法?

[英]Which is the most performant way to compare two digits?

I need to compare a lot of integer values to equality. 我需要将很多整数值与相等进行比较。

Which method is the fastest? 哪种方法最快?

A) 一种)

int a1 = 12345;
int a2 = 54321;
if(a1 == a2)
{
   //do something.
}

B) B)

int a1 = 12345;
int a2 = 54321;
if( (a1 ^ a2) == 0 )
{
   //do something.
}

I suspect first is actually faster anyway. 我怀疑首先实际上是更快。 It's not like comparing two integers for equality is an uncommon operation, so I imagine it's pretty well supported :) However, far more importantly it's much, much more readable . 这并不像比较两个整数的平等是一个不常见的操作,所以我认为它得到了很好的支持:)然而,更重要的是它更多,更具可读性

It seems very unlikely to me that comparing integers for equality will be your bottleneck. 对我来说似乎不太可能比较整数是否是你的瓶颈。 If you really feel it is, then by all means benchmark what you've got in as realistic a situation as you can, then try various changes (perhaps XOR, perhaps subtraction etc) and remeasure. 如果你真的觉得它是,那么无论如何都要将你所拥有的东西作为现实情况进行基准测试,然后尝试各种变化(可能是异或,也许是减法等)并重新测量。 But do make sure you've actually got something you need to fix before spending time moving away from the most obvious, simple, readable code. 但是,确保在花时间摆脱最明显,简单,可读的代码之前,确实已经得到了一些你需要修复的东西。

The only way to answer a question like this is to measure it, in your specific application and environment. 回答这样的问题的唯一方法是在特定的应用程序和环境中测量它。

But do the first and don't worry about it. 但是先做,不要担心。 There's nothing you're likely to do in C# where this is going to be an issue. 在C#中你没有什么可能做的,这将成为一个问题。

If you're in some bizarre corner case of performance where you really do have better knowledge about how to compare integers than the MS tools team, then you should be writing in assembler. 如果你处于一个奇怪的角色表现,你真的比MS工具团队更了解如何比较整数,那么你应该用汇编语言写作。

Personally, I cannot imagine a scenario where the comparison of two integers in a tight loop will dominate your timing - the loop and branching overhead is going to be similar or greater than the comparison cost, and even then that assumes that this great block of data has magically appeared in CPU cache without any paging or memory I/O cost. 就个人而言,我无法想象一个场景,即紧密循环中两个整数的比较将主导你的时间 - 循环和分支开销将与比较成本相似或更大,即便如此,也就是假设这个伟大的数据块神奇地出现在CPU缓存中,没有任何分页或内存I / O成本。

Rococo substitutions for operations like compares (and set-to-zero and multiply-by-constant powers of two) haven't been appropriate with proper commercial tool chains (craptastic embedded C compilers might still need them) for the best part of 20 years. 比较(以及设置为零和乘以2的乘法)的操作的洛可可替换对于适当的商业工具链(craptastic嵌入式C编译器可能仍然需要它们)来说已经不合适了20年的最佳时间。 It's time to stop thinking about them. 现在是时候停止思考它们了。

I have wrote some code to verify this. 我写了一些代码来验证这一点。

static void Main( string[] args )
{
  int a1 = 123456;
  int a2 = 654321;
  int a3;

  var t = new System.Diagnostics.Stopwatch();
  t.Start();
  for ( int i = 0; i < int.MaxValue; i++ )
  {
    if ( a1 == a2 )
    {
      a3 = a1 + a2;
    }
  }
  t.Stop();

  Console.WriteLine( t.ElapsedMilliseconds );

  t.Reset();

  t.Start();
  for ( int i = 0; i < int.MaxValue; i++ )
  {
    if ( ( a1 ^ a2 ) == 0 )
    {
      a3 = a1 + a2;
    }
  }
  t.Stop();

  Console.WriteLine( t.ElapsedMilliseconds );

  Console.ReadKey();
}

The result is the next: 结果是下一个:
For the first 6275 milliseconds 对于前6275毫秒
For the second 6277 milliseconds 对于第二个6277毫秒

It looks that these methods works identical. 看起来这些方法的工作方式相同。

Thanks for your answers. 谢谢你的回答。

The first one. 第一个。 Because both of them do a comparing operation (==), but the second one uses additional XOR operation. 因为它们都执行比较操作(==),但第二个操作使用额外的XOR操作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM