简体   繁体   English

是否值得在方法中使用按位运算符?

[英]Is It Worth Using Bitwise Operators In Methods?

I am very new to Java (and programming in general, my previous experience is with ActionScript 2.0 and some simple JavaScript), and I am working my way slowly and methodically through Java: A Beginner's Guide by Herbert Schildt. 我是Java的新手(一般编程,我之前的经验是使用ActionScript 2.0和一些简单的JavaScript),我正在通过Java慢慢地,有条不紊地工作:Herbert Schildt的初学者指南。 It is an incredible book. 这是一本令人难以置信的书。

For one thing, I finally understand more-or-less what bitwise operators (which I first encountered in ActionScript 2.0) do, and that they are more efficient than other methods for certain sums. 首先,我终于理解了或多或少的位运算符(我在ActionScript 2.0中首次遇到的),并且它们比某些和的其他方法更有效。

My question is, is it more efficient to use a method that uses, say, a shift right, to perform all your divisions/2 (or divisions/even) for you in a large program with many calculations (in this case, a sprawling RPG), or is it more efficient to simply use standard mathematical operations because the compiler will optimise it all for you? 我的问题是,在一个包含许多计算的大型程序中使用一种使用权利,例如右移,为你执行所有分区/ 2(或分区/偶数)的方法是更有效的(在这种情况下,是一个庞大的计算) RPG),或者简单地使用标准数学运算是否更有效,因为编译器会为您优化它?

Or, am I asking the wrong question entirely? 或者,我是否完全提出了错误的问题?

You are asking the wrong question entirely. 你完全在问错误的问题。 The question you should be asking is "should I keep my code simple and readable, or use tricks that I believe will improve its performance, even though I haven't measured its performance." 你应该问的问题是“我应该保持我的代码简单易读,或者使用我相信会提高其性能的技巧,即使我没有测量它的性能。” The answer should be obvious. 答案应该是显而易见的。

Bitwise operators have their place, especially when you're dealing with binary file formats that need to pack a lot of data into small space. 按位运算符占有一席之地,特别是当您处理需要将大量数据打包到小空间的二进制文件格式时。 And it's absolutely critical to know how to mask out the high bits of a byte so that you don't accidentally sign-extend (print out these two variables to see what I mean): 并且知道如何屏蔽一个字节的高位以便不会意外地进行符号扩展(打印出这两个变量以查看我的意思)是绝对关键的:

byte b = (byte)0xDC;
int  i = b & 0xFF;

But don't go looking for places to use these operators, especially to replace such simple tasks as dividing by 2. 但是不要去寻找使用这些操作符的地方,尤其是替换除以2之类的简单任务。

Optimizations like this should only be done if you have a real NEED to do it. 只有在您真正需要这样做时才能进行这样的优化。 If you simply think the code will run faster, it may not be worth it. 如果你只是认为代码运行得更快,那可能就不值得了。

Often times the compiler may be smarter than you think, and do the optimizations for you, other times there may be caveats that only get you deeper in trouble. 通常情况下,编译器可能比您想象的更聪明,并为您进行优化,有时可能会有一些警告只能让您更深入地解决问题。 In top of that (and probably the biggest reason against doing this), is that if it makes your code hard to read/understand for future developers (or yourself), you may only be adding more anti-optimizations in the future trying to work around the original code. 最重要的是(也许是反对这样做的最大原因),如果它使您的代码难以阅读/理解未来的开发人员(或您自己),您可能只会在将来添加更多反优化工作围绕原始代码。

Usually the compiler does a lot of optimizations. 通常编译器会进行大量优化。 Also you need to investigate what your bottleneck really is before you go optimizing it. 此外,您需要在优化之前调查您的瓶颈。 Optimizing things that are not the bottleneck just leads to more of your threads hitting the real bottleneck faster. 优化不是瓶颈的事情只会导致更多的线程更快地达到真正的瓶颈。 Look up constraint theory. 查找约束理论。

For learning purpose you can skip optimizing your code that way. 出于学习目的,您可以跳过以这种方式优化代码。 It requires some experience otherwise you may face problems debugging your code (I mainly mean "what the heck is this thing doing?" syndrome) Compilers nowadays are smart enough to optimize the output code and deal with known patterns the right way. 它需要一些经验,否则你可能会遇到调试代码的问题(我主要是指“这件事情到底在做什么?”综合症)现在编译器非常聪明,可以优化输出代码并以正确的方式处理已知模式。 Unless you are going to really need to save each millisecond or cpu cycle, focus on keeping your code clean. 除非您真的需要保存每毫秒或cpu周期,否则请专注于保持代码清洁。

The latter. 后者。 Which is to say, write your code in the most clear way possible first, which will most likely entail standard mathematical operations. 也就是说,首先以最清晰的方式编写代码,这很可能需要标准的数学运算。 Let the compiler take care of the rest. 让编译器处理剩下的事情。

Java, because it runs on a VM, has other interesting built-in optimization features: for example, as your program runs, the VM can see which branches of code are being executed most frequently and make those branches more efficient. Java,因为它在VM上运行,具有其他有趣的内置优化功能:例如,当您的程序运行时,VM可以查看哪些代码分支最常执行并使这些分支更有效。

Once you write your program, then use a profiling tool to measure which specific methods are slow. 编写程序后,使用分析工具来测量哪些特定方法很慢。 Once you know that, then you will know what kind of code to optimize. 一旦你知道了,那么你就会知道要优化什么样的代码。 For example, you might find that reading a configuration from a file is the slowest thing you do, and you can look for ways to make that faster. 例如,您可能会发现从文件中读取配置是您做的最慢的事情,您可以寻找更快的方法。 My hunch is that you're not likely to find any performance benefit at all by using things like bitwise operators rather than normal arithmetic. 我的预感是,你不可能通过使用按位运算符而不是普通算术来找到任何性能优势。

Best of luck! 祝你好运!

For one thing, I finally understand more-or-less what bitwise operators (which I first encountered in ActionScript 2.0) do, and that they are more efficient than other methods for certain sums. 首先,我终于理解了或多或少的位运算符(我在ActionScript 2.0中首次遇到的),并且它们比某些和的其他方法更有效。

No this is wrong here. 这不是错的。 Bitwise operations has more applications that doing a division or multiplication that you ask about. 按位运算有更多的应用程序可以进行分区或乘法。
You can efficiently use bitmasks as flags, to pack values, to emulate access rights in compact manner, in crytography etc etc. 您可以有效地使用位掩码作为标志,打包值,以紧凑的方式模拟访问权限,在密码学等中。
See this thread. 看到这个帖子。 You should search for more applications than just read a chapter that says about how a shift is similar and (perhaps) faster than a division (not necessarily as the compiler would convert to shift anyway) 你应该搜索更多的应用程序,而不仅仅是阅读一章,说明一个班次是如何相似的,并且(可能)比一个部门更快(不一定是编译器转换为移位)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM