简体   繁体   English

在 sqrt 之前添加距离条件是否会提高此代码的性能?

[英]Is adding a condition on distance before sqrt a performance gain for this code?

Let me pose a hypothetical question.让我提出一个假设性问题。 Given these two blocks of C# code, is the second version faster?鉴于这两块 C# 代码,第二个版本更快吗? I've researched and tested/timed this quite a bit and I have my own theory (which I will keep to myself for now to not influence the answers), but I want to ask the question here to keep my own biases in check.我已经对此进行了相当多的研究和测试/计时,并且我有自己的理论(我现在将自己保留以不影响答案),但我想在这里提出这个问题以控制我自己的偏见。 The reason why I'm studying this is because it was asked in an interview question recently, and I'm not satisfied with how the result was discussed.我之所以研究这个是因为最近在一个面试问题中被问到,我对结果如何讨论不满意。 Thank you in advance.先感谢您。

First version:第一个版本:

public float DistanceBetweenPoints(float x1, float y1, float x2, float y2)
{
    float result;
    float xDifference = Math.Abs(x1 - x2);
    float yDifference = Math.Abs(y1 - y2);
    result = (float) Math.Sqrt(xDifference * xDifference + yDifference * yDifference);
    return result;
}

Second version:第二个版本:

public float DistanceBetweenPoints(float x1, float y1, float x2, float y2)
{
    float result;
    float xDifference = Math.Abs(x1 - x2);
    float yDifference = Math.Abs(y1 - y2);

    if(xDifference > 0 || yDifference > 0)
    {
        result = (float) Math.Sqrt(xDifference * xDifference + yDifference * yDifference);
    }
    else
    {
        result = 0;
    }

    return result;
}

I'm specifically wondering if adding in the 0 distance check is an optimization.我特别想知道添加 0 距离检查是否是一种优化。 At this point, I'm going to give more context around this question.在这一点上,我将围绕这个问题提供更多背景信息。 The first version was what I had submitted for a solution, and the second version was what the interviewer had insisted that I do to make the code more performant.第一个版本是我提交的解决方案,第二个版本是面试官坚持要我做的,以提高代码的性能。 Their claim was that the second version is up to 20 times faster than the original, but I highly disagree.他们声称第二个版本比原始版本快 20 倍,但我非常不同意。 If this was possible, then you can bet that Microsoft would have included that optimization within the Sqrt call itself.如果这是可能的,那么您可以打赌微软会在 Sqrt 调用本身中包含该优化。 Not only that, but from my understanding, the call is usually implemented via hardware, and when I measured the timing of it, I was seeing about 4-5 ticks per call.不仅如此,据我了解,调用通常是通过硬件实现的,当我测量它的时间时,我看到每个调用大约有 4-5 个滴答声。 It shows the same results with the "optimization" of the 0 distance check.它显示了与 0 距离检查的“优化”相同的结果。 That's very impressive, and I don't know if there is anything I can do to make it faster.这非常令人印象深刻,我不知道是否可以做些什么来让它更快。

As usual, instead of trying to fix "broken" framework code, you should be trying to fix your own broken algorithms.像往常一样,与其尝试修复“损坏的”框架代码,不如尝试修复自己损坏的算法。 Because if there was a way to make Math.Sqrt faster, it would already be done.因为如果有办法让Math.Sqrt更快,它已经完成了。

Specifically for distances, chances are you don't need to actually calculate the distance, the squared distance suffices (or again, fix your algorithm to make it suffice).特别是对于距离,您可能不需要实际计算距离,平方距离就足够了(或者再次修复您的算法以使其足够)。 That means you can drop the Math.Sqrt call completely!这意味着您可以完全放弃Math.Sqrt调用!

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

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