有没有更快的方法来比较这些小数?

huangapple go评论71阅读模式
英文:

Is there a faster way to compare these decimals?

问题

我有一系列的小数,我试图将最新的小数与最后6个小数的差异进行比较,我可能会增加这个数字。

我有以下的class

public class CompareRandom
{
    private const decimal DIFFERENCE = 1.8m;

    public decimal a;
    public decimal b;
    public decimal c;
    public decimal d;
    public decimal e;
    public decimal f;
    public decimal g;

    public bool Compare(decimal num)
    {
        this.g = this.f;
        this.f = this.e;
        this.e = this.d;
        this.d = this.c;
        this.c = this.b;
        this.b = this.a;
        this.a = num;

        if (b != decimal.Zero && b / DIFFERENCE > a)
        {
            return true;
        }

        if (c != decimal.Zero && c / DIFFERENCE > a)
        {
            return true;
        }

        if (d != decimal.Zero && d / DIFFERENCE > a)
        {
            return true;
        }

        if (e != decimal.Zero && e / DIFFERENCE > a)
        {
            return true;
        }

        if (f != decimal.Zero && f / DIFFERENCE > a)
        {
            return true;
        }

        if (g != decimal.Zero && g / DIFFERENCE > a)
        {
            return true;
        }

        return false;
    }
}

然后我将其初始化为volatile

volatile static CompareRandom CompareRandom = new CompareRandom();

然后我以同步的方式调用CompareRandom.Compare(value),作为每个1ms更新一次值的循环的一部分来比较这些值。

我最感兴趣的部分是是否有更快的方法来执行以下部分:

this.g = this.f;
this.f = this.e;
this.e = this.d;
this.d = this.c;
this.c = this.b;
this.b = this.a;
this.a = num;

成功的回答将展示Compare方法的更快执行。

你可以尝试优化它,以下是链接:

https://dotnetfiddle.net/tLw8qM

https://dotnetfiddle.net/jd0bSF

英文:

I have a stream of decimals and I am trying to compare the most recent decimal to the difference of the last 6 decimals, I may increase this number

I have the following class

public class CompareRandom
{
    private const decimal DIFFERENCE = 1.8m;

    public decimal a;
    public decimal b;
    public decimal c;
    public decimal d;
    public decimal e;
    public decimal f;
    public decimal g;

    public bool Compare(decimal num)
    {
        this.g = this.f;
        this.f = this.e;
        this.e = this.d;
        this.d = this.c;
        this.c = this.b;
        this.b = this.a;
        this.a = num;

        if (b != decimal.Zero && b / DIFFERENCE > a)
        {
            return true;
        }

        if (c != decimal.Zero && c / DIFFERENCE > a)
        {

            return true;
        }

        if (d != decimal.Zero && d / DIFFERENCE > a)
        {
            return true;
        }

        if (e != decimal.Zero && e / DIFFERENCE > a)
        {
            return true;
        }

        if (f != decimal.Zero && f / DIFFERENCE > a)
        {
            return true;
        }

        if (g != decimal.Zero && g / DIFFERENCE > a)
        {
            return true;
        }

        return false;
    }
}

Then I initialize it as volatile

volatile static CompareRandom CompareRandom = new CompareRandom();

Then I call CompareRandom.Compare(value) synchronously as part of a loop that updates every 1ms to compare the values.

The part I am the most interested in knowing if there is a faster way to do is this part

    this.g = this.f;
    this.f = this.e;
    this.e = this.d;
    this.d = this.c;
    this.c = this.b;
    this.b = this.a;
    this.a = num;

A successful answer will demonstrate a faster execution of the method Compare

See if you can make it faster:

https://dotnetfiddle.net/tLw8qM

https://dotnetfiddle.net/jd0bSF

答案1

得分: 2

不需要每次执行除法操作。相反, a 乘以 DIFFERENCE 来获取阈值:

// 名称已更改以更符合传统习惯
private const decimal Difference = 1.8m;

public bool Compare(decimal num)
{
    g = f;
    f = e;
    e = d;
    d = c;
    c = b;
    b = a;
    a = num;
    var threshold = num * Difference;
    return (b != decimal.Zero && b > threshold) ||
           (c != decimal.Zero && c > threshold) ||
           (d != decimal.Zero && d > threshold) ||
           (e != decimal.Zero && e > threshold) ||
           (f != decimal.Zero && f > threshold) ||
           (g != decimal.Zero && g > threshold);
};

另外:

  • Compare 方法返回 bool 而不是整数有点奇怪;考虑到它不是“常见”的 Compare 意义,可能值得重命名以提高清晰度。
  • 一个比较方法改变对象的状态,就像这个例子中赋值给 a 一样,这非常奇怪 - 这也是改名的另一个很好的理由。

使用集合而不是单独的变量会使所有这些更易维护,但我会感到惊讶如果它提高了速度

英文:

You don't need to perform the division every time. Instead, multiply a by DIFFERENCE to obtain a threshold:

// Name changed to be more conventional
private const decimal Difference = 1.8m;

public bool Compare(decimal num)
{
    g = f;
    f = e;
    e = d;
    d = c;
    c = b;
    b = a;
    a = num;
    var threshold = num * Difference;
    return (b != decimal.Zero && b > threshold) ||
           (c != decimal.Zero && c > threshold) ||
           (d != decimal.Zero && d > threshold) ||
           (e != decimal.Zero && e > threshold) ||
           (f != decimal.Zero && f > threshold) ||
           (g != decimal.Zero && g > threshold);
};

As asides:

  • It's odd for a Compare method to return bool rather than an integer; given that it's not the "common" meaning of Compare, it's probably worth renaming it for clarity
  • It's very odd for a comparison method to change the state of an object, as this is doing (assigning to a) - another good reason to change the name.

Using a collection instead of separate variables would make all of this more maintainable, but I'd be surprised if it improved the speed.

答案2

得分: -1

I also really like the idea of eliminating the division, but I don't like the way you're handling booleans: the original question says something like:

如果 condition1 为真,那么代码不再考虑计算其他条件(当一个条件为 true 时,所有条件的 OR 也为 true)。

The proposal from Jon looks as follows:

Jon 的提议如下:

return (condition1 || condition2 || condition3)

这做的事情完全相同,但是 OR 方程的计算是否由编译器决定:如果你使用旧的编译器,不执行优化,那么整个计算会被执行,即使 condition1true

英文:

I also really like the idea of eliminating the division, but I don't like the way you're handling booleans: the original question says something like:

if condition1 then return true;
if condition2 then return true;
if condition3 then return true;

If condition1 is true, then the code does not bother about the calculation of the other conditions (why would it, when one condition is true, then the OR of all conditions also is true).

The proposal from Jon looks as follows:

return (condition1 || condition2 || condition3)

This does exactly the same thing, but the fact if the calculation of the OR equation is determined by the compiler: if you're dealing with an older compiler, not performing optimsation, then the entire calculation is done, even if condition1 is true.

huangapple
  • 本文由 发表于 2023年2月10日 14:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定