I'm looking for the fastest way to determine if a long value is a perfect square (ie its square root is another integer). I've done it the easy way, by using the built-in Math.sqrt() function, but I'm wondering if there is a way to do it faster by restricting yourself to integer-only domain. Maintaining a lookup table is impratical (since there are about 231.5 integers whose square
我正在寻找最快的方式来确定一个long值是一个完美的正方形(即其平方根是另一个整数)。 我已经通过使用内置的Math.sqrt()函数简单地完成了它,但是我想知道是否有办法通过将自己限制为仅包含整数的域来更快地实现它。 维护查找表是不实用的(因为大约有231.5个整数的平方小于263)。 这是我现在正在做的非常简单直接的方式: public final static boolean isPerfectSquare(long n) { if (n < 0) return false;
This question already has an answer here: How to see JIT-compiled code in JVM? 7 answers You will need to start the JVM with the options -XX:+PrintAssembly and -XX:UnlockDiagnosticVMOptions , but PrintAssembly requires the JVM to have the hsdis binary (HotSpot disassembler). The hsdis binary is not distributed with the JVM due to license incompatibility, so you will need to compile hsdis yo
这个问题在这里已经有了答案: 如何在JVM中查看JIT编译的代码? 7个答案 您需要使用选项-XX:+PrintAssembly和-XX:UnlockDiagnosticVMOptions来启动JVM,但PrintAssembly要求JVM具有hsdis二进制文件(HotSpot反汇编程序)。 由于许可证不兼容,hsdis二进制文件不会与JVM一起分发,因此您需要自己编译hsdis或从非官方网站上找到预构建的hsdis二进制文件。 为了理解输出,像JITWatch这样的工具很有用。 按照其指示将调试信
How do you write (and run) a correct micro-benchmark in Java? I'm looking here for code samples and comments illustrating various things to think about. Example: Should the benchmark measure time/iteration or iterations/time, and why? Related: Is stopwatch benchmarking acceptable? Tips about writing micro benchmarks from the creators of Java HotSpot: Rule 0: Read a reputable paper o
你如何在Java中编写(并运行)一个正确的微基准测试? 我正在寻找代码示例和评论来说明各种需要考虑的事情。 例如:基准应该测量时间/迭代还是迭代/时间,为什么? 相关:秒表基准测试是否可以接受? 关于从Java HotSpot的创建者编写微型基准测试的技巧: 规则0:在JVM上读取一份有信誉的论文并进行微观基准测试。 Brian Goetz,2005年是一个不错的选择。不要对微基准期望太高, 它们仅测量有限范围的JVM性能特征。
Why can't I switch on a String ? Is this functionality going to be put into a later Java version? Can someone explain why I can't do this, as in, the technical way Java's switch statement works? Switch statements with String cases have been implemented in Java SE 7, at least 16 years after they were first requested. A clear reason for the delay was not provided, but it likely h
为什么我不能打开一个String ? 这个功能是否会被放到更高版本的Java中? 有人可以解释为什么我不能这样做,因为Java的switch语句的技术方式有效吗? 在Java SE 7中已经实现了使用String情况的开关语句,至少在第一次请求后16年。 没有提供明确的延迟原因,但可能与性能有关。 在JDK 7中实现 这个特性现在已经在javac中用“脱糖”过程实现了; 在声明的case在编译时扩展为一个模式之后的更复杂的代码时,使用String常量
When I am running I am getting the following exception repeatedly each time I try to run the program. Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine. I tried to increase my virtual memory (page size) and RAM size, but to no avail. Can anyone tell me how can I eliminate this error? 使用-XX:MaxHeapSize=51
当我运行时,每次尝试运行程序时都会重复出现以下异常。 VM初始化期间发生错误 无法为对象堆预留足够的空间 无法创建Java虚拟机。 我试图增加我的虚拟内存(页面大小)和内存大小,但无济于事。 任何人都可以告诉我如何消除这个错误? 使用-XX:MaxHeapSize=512m (或任何大数字)(或简称为-Xmx512m )运行JVM, 这也可能是由于在32位HotSpot虚拟机上设置了太大的内容导致的,例如: -Xms1536m -Xmx1536m 这可能会/
I have been lately reading a lot on memory allocation schemes in java, and there have been many doubts as I have been reading from various sources. I have collected my concepts, and I would request to go through all of the points and comment on them. I came to know that memory allocation is JVM specific, so I must say beforehand, that my question is Sun specific. Classes (loaded by the classl
我最近一直在用java读取很多内存分配方案,并且在我从各种渠道阅读时遇到了许多疑问。 我已经收集了我的概念,并且我会请求通读所有观点并对它们发表评论。 我开始知道内存分配是特定于JVM的,所以我必须事先说,我的问题是Sun特有的。 类(由类加载器加载)进入堆中的特殊区域:永久生成 所有与像类名相关的类,与类关联的对象数组,与JVM使用的内部对象(如java / lang / Object)和优化信息相关的所有信息都会进入永久
This question already has an answer here: Is frame in JVM heap allocated or stack allocated? 1 answer The JVM uses the native stack of the process. This minimise overhead, and allows the stack to be virtual (it can have a maximum size much larger than what is actually used) As a result most stacks are rarely used much ( << 10% of maximum ) and graphing them might be more confusing t
这个问题在这里已经有了答案: JVM堆分配或栈分配框架? 1个答案 JVM使用进程的本地堆栈。 这可以最大限度地减少开销,并允许堆栈是虚拟的(它可以具有比实际使用的更大的最大尺寸) 因此,大多数堆栈很少使用太多(最大值的10%),并且绘制它们可能比实用更令人困惑。 编号堆栈是专用于存放与方法调用相关的信息的不同区域的存储区,例如参数值以及程序流在方法返回后应返回的位置。
In Hidden Features of Java the top answer mentions Double Brace Initialization, with a very enticing syntax: Set<String> flavors = new HashSet<String>() {{ add("vanilla"); add("strawberry"); add("chocolate"); add("butter pecan"); }}; This idiom creates an anonymous inner class with just an instance initializer in it, which "can use any [...] methods in the conta
在Java的隐藏特性中,最佳答案提到了Double Brace Initialization,其语法非常诱人: Set<String> flavors = new HashSet<String>() {{ add("vanilla"); add("strawberry"); add("chocolate"); add("butter pecan"); }}; 这个习语创建了一个匿名的内部类,只有一个实例初始化器,它可以使用“包含范围中的任何方法”。 主要问题:这听起来效率不高吗? 它的使用应限于一次性初始化吗? (当然,
This question already has an answer here: Inversion of Control vs Dependency Injection 16 answers IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application. DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object
这个问题在这里已经有了答案: 控制反转与依赖注入16答案 IoC是一个通用术语含义,而不是让应用程序调用框架中的方法,框架调用应用程序提供的实现。 DI是IoC的一种形式,通过构造函数/设置程序/服务查找将实现传递给对象,对象将“依赖”以便正确执行。 参考:控制反转与依赖注入 这些是在编程中实现松散耦合的模式 DI(依赖注入) : 依赖注入是一种模式,用于创建其他对象依赖的对象的实例,而无需知道在编译时将
(Please excuse me if i got the title incorrect, I believe it is binding but if anything let me know and I will edit the title/question) Learning about different ways to bind data in programming (No specific language) we went over 5 types. (I gave the type, with the definition given in class, under it will be my questions) 1) Static : subscript ranges are statically bound and storage allocati
(请原谅,如果我的标题不正确,我相信它是有约束力的,但如果有任何事情让我知道,我会编辑标题/问题) 学习在编程中绑定数据的不同方式(没有特定的语言),我们超过了5种类型。 (我给出的类型,在课堂上给出的定义下,将是我的问题) 1) 静态 :下标范围静态绑定,存储分配静态(运行前) 首先,我从来不明白它的含义,“存储分配是静态的”,这意味着它在运行时间之前发生。 现在我的想法是,内存是在程序中分配的,