什么是悲观? [英] What is pessimization?

查看:90
本文介绍了什么是悲观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此问题有评论可以使用C + +11的auto改善了性能?,该投票获得了很多票,并建议使其无意间变得不太悲观"作为答案.我以前从未听说过这个词.我想这与优化相反.

There is a comment on the question Can the use of C++11's auto improve performance? that scored many votes and suggests "makes it less likely to unintentionally pessimize" as an answer. I've never noticed this term before. I guess it is somehow the opposite of optimization.

任何人都可以给出更详细的定义吗? 在编程环境中是什么意思?悲观的代码会是什么样子?

Can anyone give a more detailed definition? What does it mean in the context of programming? How would pessimized code look like?

推荐答案

这主要是玩文字游戏,悲观主义者与乐观主义者相反.悲观化写的代码少于最优代码.

It's mostly a play on words, a pessimist is the opposite of an optimist. And pessimisation is writing less than optimal code.

编译器和程序员都可以通过使用错误的构造来简化代码,例如在不需要时复制内容. auto关键字至少将确保您获得最接近的类型",因此没有(不必要的)类型转换.

Both compilers and the programmer can pessimise code by having bad constructs that for example copy things when it isn't required. The auto keyword will at the very least ensure that you get the "closest type", so there is no (unnecessary) type conversion.

请注意,悲观化"是指没有使代码不是最佳"的好处:

Note that pessimisation is when there is no BENEFIT to the code being "not optimal":

这不是悲观的说法,如果我们花六个月的时间对此进行优化,它将以0.5%的速度运行".除非要求将速度提高0.5%,否则花六个月的时间可能会浪费时间.

It is not pessimisation "if we spent six months optimising this, it would run 0.5% faster". Unless it's a requirement to be 0.5% faster, spending six months on it is probably a waste of time.

此外,所需的功能(例如安全性)也不是悲观的:代码比我们可能要慢,因为我们确保了它的安全性".

Also, required functionality, such as security, is not pessimisation: "The code is slower than it possibly could be because we made it secure".

一个调试版本是"pessimal",因为它具有断言来捕获NULL指针取消引用并检查数组访问的索引等.只要编写了这些断言和检查,以使它们在启用时消失"释放模式. [并且如果您的代码正在运行核电站,则您可能永远都不想崩溃,请参见上面的安全性"]

A debug build is mot "pessimal" because it has asserts to catch NULL pointer dereferences and checking the index of array accesses, etc. As long as those asserts and checks are written such that they "disappear" when you enable the release mode. [and if your code is running a nuclear power-plant, you probably don't want crashes EVER, see "security" above]

我看到的一个老例子是这个C字符串循环:

An old example I've seen is this C-string loop:

char str [large_number] = "... several kilobytes of text (read from file) ... ";

for(char *p = str; p < str+strlen(str); p++)
{
   ... do stuff with p ... 
}

如果do stuff with p非常复杂,则编译器将不会意识到strlen是常数值,并且将在循环的每次迭代中执行strlen.如果这样做,循环将以更快的速度运行:

If do stuff with p is sufficiently complex, the compiler won't realize that strlen is a constant value, and will perform strlen every iteration of the loop. The loop will run MUCH faster if we did:

for(char *p = str, *e = str+strlen(str); p < e; p++)
{
   ... do stuff with p ... 
}

[恐怕不是auto的示例]

[Not an example of auto, I'm afraid]

这篇关于什么是悲观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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