if 语句中的多项检查
在java中有没有更快的方法来做到这一点?
if (keyCode != 66 && keyCode != 8 && keyCode != 21 && keyCode != 22) {
}
keyCode 是一个 int。
In java is there a faster way of doing this?
if (keyCode != 66 && keyCode != 8 && keyCode != 21 && keyCode != 22) {
}
keyCode is an int.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快点?对你来说太慢了吗?不要玩优化器。编写可读代码并将微优化留给优化器。 过早的优化是万恶之源
在 josh 的评论后编辑:
如果你真的有很多,把它们放在一个容器中(比如一个集合或一个数组)并在其中
find
keyCode
。如果你找到了它,那么你的条件就是错误的。否则就是真的。根据戴夫的评论:
Faster? Is it too slow for you? Don't play optimizer. Write readable code and leave microoptimizations to the optimizer. Premature optimization is the root of all evil
Edit after josh's comment:
If you have really many of them, put them in a container (such as a set or an array) and
find
keyCode
in it. If you found it, then your condition is false. Otherwise it's true.As per Dave's comment:
是的,
switch
语句将转换为直接跳转。然而,JIT 无论如何都有可能对您的代码执行此操作,因此您必须尝试一下switch
是否实际上更快。Yes, the
switch
statement will translate to direct jumps. It is however possible that the JIT does that to your code anyway, so you have to try it out ifswitch
is actually faster.