代码对比

发布于 2024-11-06 03:19:28 字数 566 浏览 4 评论 0原文

以下两段代码中哪一段应该优先于其他代码?一般应在什么基础上作出决定?

在此函数的各种调用中,MAX_LIMIT 可能从 1000 到 5000 不等。

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         anObj.setMatrix(i,j,0);
    }
}
for (i=0;i<MAX_LIMIT;++i)
{
    anObj.setMatrix(i,i,1);
}

VS

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         if(i==j)
         {
             anObj.setMatrix(i,j,1);
         }
         else
         {
             anObj.setMatrix(i,j,0);
         }
    }
}

谢谢。

Which one of the following two pieces of code should be preferred over other? and on what basis the decision should be taken generally?

MAX_LIMIT might vary from 1000 to 5000 in various calls of this function.

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         anObj.setMatrix(i,j,0);
    }
}
for (i=0;i<MAX_LIMIT;++i)
{
    anObj.setMatrix(i,i,1);
}

vs

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         if(i==j)
         {
             anObj.setMatrix(i,j,1);
         }
         else
         {
             anObj.setMatrix(i,j,0);
         }
    }
}

Thanks.

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

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

发布评论

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

评论(4

Bonjour°[大白 2024-11-13 03:19:28

两者的性能应该渐近相等,因为两者的运行时间都是 O(n^2)。
您可能应该更喜欢最具可读性的一个。

The perfomance of the two should be asymptoticaly equal, as both run in O(n^2).
You should probably prefer the one with most readability.

萌逼全场 2024-11-13 03:19:28

第二个,它按照它所说的去做。但是,我强烈希望

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         anObj.setMatrix(i, j, i==j ? 1 : 0);
    }
}

The second, which does what it says. However, I'd strongly prefer

for (i=0;i<MAX_LIMIT;++i)
{
    for (j=0;j<MAX_LIMIT;++j)
    {
         anObj.setMatrix(i, j, i==j ? 1 : 0);
    }
}
怪我鬧 2024-11-13 03:19:28

在我看来,我会选择两者中的第二个。最好限制循环某些内容的次数。由于您正在考虑至少 O(n^2) 运行时间,因此我肯定会将自己限制为只有 2 个循环。

In my opinion I would go with the second of the two. It is best to limit the number of times you have to loop through something. Since you're looking at at least O(n^2) runtime I would definately limity yourself to only 2 loops.

逆流 2024-11-13 03:19:28

更好用于什么?我认为第二个版本最容易一眼理解,尽管其他人可能有其他意见。

如果您对性能感兴趣,那么对这两种方法进行计时应该很容易。 (它甚至可能是相关的。)

从性能角度来看,我怀疑第二种方法会更快,因为它一次会处理一个缓存行的数组。第一种方法将一次处理一个缓存行的数组,然后继续执行 N 个缓存故障。我的猜测是,这比在循环中添加额外的条件更重要。

因此,如果与其他操作相比,数组初始化需要大量时间,并且这一点很重要,请对每个操作进行计时并查看。如果没有,请选择更具可读性的版本。

Better for what? I think the second version is easiest to understand at a glance, although other people may have other opinions.

If you're interested in performance, it should be easy to time the two methods. (It may even be relevant.)

Taking a look at this for performance, I'd suspect the second method would be faster, because it will work through the array one cache line at a time. The first method will work through the array one cache line at a time, and then go on to execute N cache faults. My guess is that this is more significant than putting an extra conditional in the loop.

So, if array initialization takes a significant amount of time compared to other operations, and this matters, time each one and see. If not, then go with the more readable version.

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