C# do/while 和 for 哪个更快?

发布于 2024-08-13 02:51:08 字数 2296 浏览 5 评论 0原文

我有一个 C# .NET 2.0 脚本,我想知道为什么以下代码比同类的 do while 循环更快。

private double getStop(double avgPrice, bool longTrading)
    {
        double stopS = 0.0;
        double stopL = 0.0;

        for (int i = 0; i < 13; i++)
        {
            if (i == 0 || i == 12)
            {
                stopS = 0.0;
                stopL = 0.0;
            }
            else
            {
                if ((lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))
                {
                    if (avgPrice < lines[i])
                    {
                        stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));
                        stopS = lines[i];
                    } else {
                        stopL = lines[i];
                        stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));
                    }
                }
            }
        }

        if (longTrading)
        {
            return stopL;   
        } else {
            return stopS;   
        }   
    }

另外,直接明确地声明每个 if 语句而不是在 for 循环中执行它们会更快吗?

既然回答得这么快,为什么它的运行速度会比上面的代码慢得多?

private double getStop(double avgPrice, bool longTrading)
    {
        double stopS = 0.0;
        double stopL = 0.0;

        for (int i = 0; i < 13; i++)
        {
            if (i == 0 || i == 12)
            {
                stopS = 0.0;
                stopL = 0.0;
                skip = true;
            }

            if (!skip && (lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))
                {
                    if (avgPrice < lines[i])
                    {
                        stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));
                        stopS = lines[i];
                    } else {
                        stopL = lines[i];
                        stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));
                    }
                }
            }
           skip = false;
        }

        if (longTrading)
        {
            return stopL;   
        } else {
            return stopS;   
        }   
    }

I have a C# .NET 2.0 script and I want to know why the following code would be faster than a do while loop of the same kind.

private double getStop(double avgPrice, bool longTrading)
    {
        double stopS = 0.0;
        double stopL = 0.0;

        for (int i = 0; i < 13; i++)
        {
            if (i == 0 || i == 12)
            {
                stopS = 0.0;
                stopL = 0.0;
            }
            else
            {
                if ((lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))
                {
                    if (avgPrice < lines[i])
                    {
                        stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));
                        stopS = lines[i];
                    } else {
                        stopL = lines[i];
                        stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));
                    }
                }
            }
        }

        if (longTrading)
        {
            return stopL;   
        } else {
            return stopS;   
        }   
    }

Also, would it be faster just to explicitly state each if statement instead of doing them inside of a for loop?

Being that this was answered so fast, why would this run far slower than the above code?

private double getStop(double avgPrice, bool longTrading)
    {
        double stopS = 0.0;
        double stopL = 0.0;

        for (int i = 0; i < 13; i++)
        {
            if (i == 0 || i == 12)
            {
                stopS = 0.0;
                stopL = 0.0;
                skip = true;
            }

            if (!skip && (lines[i] - ((lines[i] - lines[i - 1]) / 2)) < avgPrice && avgPrice < (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2)))
                {
                    if (avgPrice < lines[i])
                    {
                        stopL = (lines[i] - ((lines[i] - lines[i - 1]) / 2));
                        stopS = lines[i];
                    } else {
                        stopL = lines[i];
                        stopS = (lines[i + 1] - ((lines[i + 1] - lines[i]) / 2));
                    }
                }
            }
           skip = false;
        }

        if (longTrading)
        {
            return stopL;   
        } else {
            return stopS;   
        }   
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

分开我的手 2024-08-20 02:51:08

性能差异应该可以忽略不计,但 for 循环更清晰,所以我会这样做。

The performance difference should be negligible, but the for loop is clearer so I would go with that.

时光无声 2024-08-20 02:51:08

它们应该本质上是等价的。您的“for”循环被评估为:

int i = 0;
while (i < 13)
{
   //all other stuff
   i++;
}; 

They should be essentially equivalent. Your 'for' loop gets evaluated as:

int i = 0;
while (i < 13)
{
   //all other stuff
   i++;
}; 
一腔孤↑勇 2024-08-20 02:51:08

循环的变化并没有太大的不同,它取决于上下文和编程语言。

但我的观点是,for语句在约束匹配之前不会do,因此它应该比do更快/尽管。

loop variations aren't too much different, it depends on the context and the programming language.

But my opinion is, for statement don't do until the constaint(s) is/are matched, therefore it should be faster than do/while.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文