事件是SFML。切换语句VS if语句 [英] Events is SFML. Switch statement VS if statement

查看:166
本文介绍了事件是SFML。切换语句VS if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些这样的代码:

while(window.pollEvent(event) {
    //checking events...
}



我的问题是,我应该使用switch语句: p>

My question is, should I use switch statement:

switch(event.type) {
    case sf::Event::Closed:
        window.close();
        break;
    case sf::Event::KeyPressed:
        //...
        break;
}

或if-else语句:

or if-else statement:

if(event.type == sf::Event::Closed)
    window.close();
else if(event.type == sf::Event::KeyPressed)
    //...

这在SFML中更快,对其他人更容易阅读?

Which is faster in SFML and more readable for others?

推荐答案

这不是SFML 的问题,同样的问题可能会出现与其他类似的代码,有限状态自动机,字节码解释器;另请参阅线程代码)。

It is not a matter of SFML, the same question could arise with other kind of similar code (e.g. most event loops, finite state automatons, bytecode interpreters; read also about threaded code).

关于性能,原则上,开关通常会稍微更快,但你应该基准(和一些编译器可以优化 的序列,如果进入等效于 switch ,反之亦然)。在你的情况下,它不是真的有意义的(因为大多数时候你的应用程序会等待<$ c $> window.pollEvent(event)中的一个事件。 ...)。

Regarding performance, in principle, a switch would often be slightly faster, but you should benchmark (and some compilers might optimize a sequence of if into the equivalent of switch or vice versa). In your case, it should not really matter (because most of the time your application would wait for an event in window.pollEvent(event) ....).

假设没有重型优化发生,我想象一些罕见的开关可能会稍微减慢因为热代码太大等等,但是这种情况是不寻常的。

Assuming no heavy optimization happens, I would imagine that some rare switches might be slightly slower, because e.g. of L1 I-cache being full because the hot code would be too large, etc... But that scenario is unusual.

如果你很好奇,请阅读此 多路分支代码生成的超级优化器分析 (关于开关优化。

If you are curious, read this A Superoptimizer Analysis of Multiway Branch Code Generation paper (by R.Sayle) about switch optimization.

关于可读性,开关也更易读。

Regarding readability, the switch is a also more readable.

可读性参数似乎与我最相关; 将微优化保留到编译器,它们运行得很好。当然,不要忘记使用 g ++ -Wall -O2 -mcpu = native 编译并且可能替换 -O2 -O3 (甚至可以编译并链接 g ++ -flto -O3 -mcpu = native 如果你关心性能很多)

The readability argument seems the most relevant to me; leave micro-optimizations to the compiler, they are doing quite well. Of course, don't forget to compile with g++ -Wall -O2 -mcpu=native and perhaps replace -O2 by -O3 (and perhaps even compile and link with g++ -flto -O3 -mcpu=native if you care a lot about performance)

(实际上,可读性是在这种情况下对你最重要的)

如果您想了解编译器如何优化以及为什么,请考虑添加到 -O2 -fverbose-asm -S (然后查看生成的 .s 文件)甚至编译 -fdump-tree-all (您将获得数百个编译器转储文件,这些文件对应于 -O2 或 -O3) )。

If you want to understand how and "why" the compiler optimizes, consider adding to your -O2 flag -fverbose-asm -S (then look into the generated .s file) or even compiling with -fdump-tree-all (you'll get hundreds of compiler dump files, corresponding to various optimizations passes in GCC...) with some optimization switches (e.g. -O2 or -O3).

这篇关于事件是SFML。切换语句VS if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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