如果语句在内部还是外部循环? [英] If statements inside or outside loops?

查看:113
本文介绍了如果语句在内部还是外部循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做会更好吗?

foreach my $item ( @array ) {
   if ( $bool ) {
     .. code ..
   }
   else {
     .. code ..
   }
}

if ( $bool ) {
   foreach my $item ( @array ) {
   }
}
else {
   foreach my $item ( @array ) {
   }
}

推荐答案

我会离开过早的优化放在一边.

过早的优化是万恶之源"-唐纳德·克努斯(Donald Knuth)

"Premature optimization is the root of all evil" - Donald Knuth

您应该首先追求可维护性.考虑到代码的逻辑结构(例如,将相关语句分组在一起),以更合理的方式对它们进行分组.

You should go for maintainability first and foremost. Group them in the way that makes more sense taking into account the logical structure of the code (such as grouping related statements together).

如果以后确定性能是问题,请尝试使用探查器之类的工具进行测量,以查看瓶颈在哪里.可能是,那里不存在.从代码完成2:

If you later determine that performance is an issue, try measuring with something like a profiler to see where the bottlenecks are. Chances are, it's not there. From Code Complete 2:

Barry Boehm报告说20%的人 一个程序的例程消耗80 执行时间的百分比.在他的 经典论文的实证研究 Fortran计划",唐纳德·克努斯(Donald Knuth)发现 不到一个的百分之四 程序通常占比 运行时间的50%.

Barry Boehm reports that 20 percent of a program's routines consume 80 percent of its execution time. In his classic paper "An Empirical Study of Fortran Programs," Donald Knuth found that less than four percent of a program usually accounts for more than 50 percent of its run time.

我们不应该在必要之前就猜测应该在哪里进行优化,因为我们大多数人都很难猜测代码中最慢的部分在哪里.在进行过程中进行优化的程序员还花费了大约96%的时间来优化不需要进行优化的代码.要考虑的另一件事是,代码调优(如本例所示)考虑了性能的可读性和可维护性之间的权衡:

We shouldn't try to guess where to optimize before it is necessary since most of us are really bad at guessing where that slow portion of our code is. Programmers who optimize as they go also spend about 96% of their time optimizing code that doesn't need to be optimized. Another thing to take into account is that code tuning (as in this example) considers a tradeoff between readability and maintainability for performance:

关注优化过程中 最初的发展有损于 实现其他计划目标. 开发人员沉迷于 算法分析和奥秘辩论 最终贡献不大 给用户的价值.诸如此类的问题 正确性,信息隐藏以及 可读性成为次要目标, 即使性能更容易 比其他人晚改善 值得关注.事后表演 工作通常影响不到五个 程序代码的百分比.你会 宁愿回去做表演工作 百分之五的代码或 可读性在100%上起作用?

Focusing on optimization during initial development detracts from achieving other program objectives. Developers immerse themselves in algorithm analysis and arcane debates that in the end don't contribute much value to the user. Concerns such as correctness, information hiding, and readability become secondary goals, even though performance is easier to improve later than these other concerns are. Post hoc performance work typically affects less than five percent of a program's code. Would you rather go back and do performance work on five percent of the code or readability work on 100 percent?

我不是说不要优化,而是要在最后的时候使用优化代码,前提是您拥有丰富的知识和工具来指出自己正确的方向.

I'm not saying don't optimize, but optimize code only in the end, when you have the luxury of the big picture and tools to point you in the right direction.

额外:不过,要回答性能本身的问题,

EXTRA: To answer the question of performance itself, though:

此[取消切换"代码]可以节省大约20%的时间:

This ["unswitching" the code] is good for about a 20 percent time savings:

Language        Straight Time    Code-Tuned Time    Time Savings
C++             2.81             2.27               19%     
Java            3.97             3.12               21%
Visual Basic    2.78             2.77               <1%
Python          8.14             5.87               28%

与这种情况不同的危险是两个回路必须并行保持. [...]您必须记住在两个地方都更改了代码,这很烦人 您和其他需要使用该代码的人都感到头疼.

A hazard distinct to this case is that the two loops have to be maintained in parallel. [...] you have to remember changing the code in both places, which is an annoyance for you and a maintenance headache for anyone else who has to work with the code.

此示例还说明了代码调优的关键挑战:任何特定代码的影响 代码调整是不可预测的.代码调优在以下方面产生了重大改进 四种语言中的三种,但Visual Basic中没有.要执行此特定 在此特定版本的Visual Basic中进行优化将产生较少的可维护性 代码,而不会在性能上产生任何抵消性收益.一般的教训是,您必须 测量每个特定优化的效果以确保其效果-否 例外情况.

This example also illustrates a key challenge in code tuning: the effect of any specific code tuning is not predictable. The code tuning produced significant improvements in three of the four languages but not in Visual Basic. To perform this specific optimization in this specific version of Visual Basic would produce less maintainable code without any offsetting gain in performance. The general lesson is that you must measure the effect of each specific optimization to be sure of its effect - no exceptions.

在此处检查这其他问题.

这篇关于如果语句在内部还是外部循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆