- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试一些关于字符串池的性能基准。然而,结果并非预期。
我做了3个静态方法
我的期望是(1. 最快 -> 3. 最慢)
但基准测试显示“Te”+“st”比“Test”快一点。
new String(): 141677000 ns
"Test" : 1148000 ns
"Te"+"st" : 1059000 ns
new String(): 141253000 ns
"Test" : 1177000 ns
"Te"+"st" : 1089000 ns
new String(): 142307000 ns
"Test" : 1878000 ns
"Te"+"st" : 1082000 ns
new String(): 142127000 ns
"Test" : 1155000 ns
"Te"+"st" : 1078000 ns
...
代码如下:
import java.util.concurrent.TimeUnit;
public class StringPoolPerformance {
public static long perform0() {
long start = System.nanoTime();
for (int i=0; i<1000000; i++) {
String str = new String("Test");
}
return System.nanoTime()-start;
}
public static long perform1() {
long start = System.nanoTime();
for (int i=0; i<1000000; i++) {
String str = "Test";
}
return System.nanoTime()-start;
}
public static long perform2() {
long start = System.nanoTime();
for (int i=0; i<1000000; i++) {
String str = "Te"+"st";
}
return System.nanoTime()-start;
}
public static void main(String[] args) {
long time0=0, time1=0, time2=0;
for (int i=0; i<100; i++) {
// result
time0 += perform0();
time1 += perform1();
time2 += perform2();
}
System.out.println("new String(): " + time0 + " ns");
System.out.println("\"Test\" : " + time1 + " ns");
System.out.println("\"Te\"+\"st\" : " + time2 + " ns");
}
}
有人可以解释为什么“Te”+“st”比“Test”执行得更快吗? JVM 是不是在这里做了一些优化?谢谢。
最佳答案
"Te"+ "st"
是编译器时常量表达式,因此在运行时的行为与简单的 "Test"没有什么不同
。任何性能影响都将在尝试编译时发生,而不是在尝试运行时发生。
使用 javap -c StringPoolPerformance
反汇编已编译的基准测试类很容易证明这一点:
public static long perform1();
Code:
...
7: ldc #3; //int 1000000
9: if_icmpge 21
12: ldc #5; //String Test
14: astore_3
15: iinc 2, 1
...
public static long perform2();
Code:
...
7: ldc #3; //int 1000000
9: if_icmpge 21
12: ldc #5; //String Test
14: astore_3
15: iinc 2, 1
...
方法的字节码完全相同!这是由 Java Language Specification, 15.18.1 指定的:
The String object is newly created (§12.5) unless the expression is a compile-time constant expression (§15.28).
您遇到的基准差异可能是由于典型的可变性或因为您的基准并不完美。看到这个问题:How do I write a correct micro-benchmark in Java?
您违反的一些值得注意的规则:
perform1()
总是在创建一百万个对象的测试后立即运行时尤其相关)。关于java - 字符串池 : "Te" +"st" faster than "Test"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12081170/
Strassen 的算法在多项式上比 n 次方正则矩阵乘法快。 “多项式更快”是什么意思? 最佳答案 您的问题与“复杂性”的理论概念有关。例如,据说正则矩阵乘法的复杂度为 O(n^3)。这意味着随着维
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
假设我有两个 PHP 语句: echo "foo"."bar" echo "foo", "bar" 注意连接字符串的不同方式 - 使用 . 或 ,. 我意识到这两种方法之间的实际区别,使用 , 为关键
我有一个 RGB 图像,并尝试将 RGB 上的每个像素设置为黑色,其中相应的 alpha 像素也为黑色。所以基本上我试图将 Alpha“烘焙”到我的 RGB 中。我已经尝试使用 PIL 像素访问对象、
可能这个问题以前已经以许多不同的方式回答过很多次了。然而,我一直在寻找更具体的东西。通用词典,我相信比哈希表更快,因为它们不需要经历装箱/拆箱的过程。 但是,哈希表不是经过排序的,这意味着搜索可以更快
以这种方式加入给定的选择会更快吗 select ... join table1 on condition1 join table2 on condition2 然后这个 select ... join
我观看了演讲并阅读了博客文章,但我仍然不确定...这是为什么 template struct conditional { template using f = T; }; template<> str
我想知道用离散数据集编码的二维图形。对我来说,似乎由于计算机以离散集合运行,所以对图形做同样的事情也许更有意义。 例如,与其让一条线由两个 vector 表示,让计算机整理出像素配置,然后必须用代数方
我正在开发一个将大量数据写入磁盘的 C++ 程序。以下函数对数据进行 gzip 压缩并将其写入文件。压缩数据约为100GB。压缩并写出数据的函数如下: void constructSNVFastqD
我最近遇到了这段SQL: SELECT members.id FROM members, members_to_groups WHERE members.id = members_to_groups.
所以我一直在研究 pthreads,特别是尝试计算两个矩阵的乘积。我的代码非常困惑,因为它本应是我自己的一个快速有趣的小项目,但我使用的线程理论非常类似于: #include #include #
我看到了被选中的answer对此post . 我很惊讶 (x & 255) == (x % 256) 如果 x 是无符号整数,我想知道总是替换 % 是否有意义& in x % n for n = 2^
Anaconda是什么? Anaconda 是一个开源的 Python 发行版本,主要面向数据科学、机器学习和数据分析等领域。它不仅包含了 Python 解释器本身,更重要的是集成了大量的用于科学计
我正在使用Ubuntu 14.04操作系统的g2.8xlarge EC-2实例上启用cuDNN的情况下运行py-faster-rcnn。一切都已编译,似乎工作正常。我通过以下方式登录到远程实例: ss
要求用户输入的简单程序可以使用 datainputstream 或扫描仪。哪个会更快? Datainputstream 是二进制的,所以它可能更快?有意见吗?我只是很好奇,因为我正在构建一个程序,我希
我正在 INRIA Person 数据集上训练 Faster-RCNN(VGG-16 架构)。我接受了 180,000 个训练步骤的训练。但是当我评估网络时,它会用相同的图像给出不同的结果。以下是图片
我是 python 新手,我为 Hackerrank 问题提交了此代码 Arrays and Simple Queries ,但对于大量测试用例,程序“由于超时而终止”。我怎样才能提高效率? 我已将主
如果将程序分成头文件和源文件,如果提供给智能编译管理器,可能会受益于更快的编译,这就是我正在研究的内容。 理论工作的意愿:为每个源文件创建一个线程并一次将每个源文件编译成目标文件。然后将这些目标文件链
方法一: SELECT * FROM `wordpressusers` WHERE user_login='user10000001' PhpMyadmin 的结果: 方法2: SELECT user
据我所知set.add使用FastFloat的equals方法 对我来说重要的只是点后的前两位数(!!!),所以在 equals 方法中我使用 Math.abs() >= 0.001 来使 equal
我是一名优秀的程序员,十分优秀!