new String()vs literal string performance [英] new String() vs literal string performance

查看:128
本文介绍了new String()vs literal string performance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StackOverflow上多次询问过这个问题,但没有一个基于性能

This question has been asked many times on StackOverflow but none of them were based on performance.

Effective Java 预订它


如果 String s = new String(stringette); 发生在循环或
频繁调用的方法中,数百万个String实例可以不必要地创建

If String s = new String("stringette"); occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly.

改进后的版本如下:
String s =stringette; 此版本使用一个String实例,而不是每次执行时创建一个新的

The improved version is simply the following: String s = "stringette"; This version uses a single String instance, rather than creating a new one each time it is executed.

所以,我试过两个发现性能显着改善

So, I tried both and found significant improvement in performance:

for (int j = 0; j < 1000; j++) {
    String s = new String("hello World");
}

需要 399 372 纳秒。

for (int j = 0; j < 1000; j++) {
    String s = "hello World";
}

需要 23 000 纳秒。

为什么会有这么多的性能提升?内部是否发生编译器优化

Why is there so much performance improvement? Is there any compiler optimization happening inside?

推荐答案

在第一种情况下,正在建立一个新对象在每次迭代中创建,在第二种情况下,它始终是从String常量池中检索的相同对象。

In the first case, a new object is being created in each iteration, in the second case, it's always the same object, being retrieved from the String constant pool.

在Java中,当你这样做时:

In Java, when you do:

String bla = new String("xpto");

你强制创建一个新的String对象,这需要一些时间和内存。

You force the creation of a new String object, this takes up some time and memory.

另一方面,当你这样做时:

On the other hand, when you do:

String muchMuchFaster = "xpto"; //String literal!

String只会在第一次创建(一个新对象),并且会被缓存在 String 常量池中,所以每次以它的文字形式引用它时,你得到的是完全相同的对象,这非常快。

The String will only be created the first time (a new object), and it'll be cached in the String constant pool, so every time you refer to it in it's literal form, you're getting the exact same object, which is amazingly fast.

现在您可能会问......如果代码中的两个不同点检索相同的文字并进行更改,会不会出现问题?!

Now you may ask... what if two different points in the code retrieve the same literal and change it, aren't there problems bound to happen?!

不,因为您可能非常清楚,Java中的字符串是不可变的!因此,任何可以改变String的操作都会返回一个新的String,并保留对同一文字的任何其他引用。

No, because Strings, in Java, as you may very well know, are immutable! So any operation that would mutate a String returns a new String, leaving any other references to the same literal happy on their way.

这是不可变数据结构的优点之一,但这完全是另一个问题,我会写几篇关于这个主题的文章。

This is one of the advantages of immutable data structures, but that's another issue altogether, and I would write a couple of pages on the subject.

编辑

只是澄清一下,常量池不是String类型独有的,你可以阅读更多关于它,或者如果你google for Java常量池。

Just a clarification, the constant pool isn't exclusive to String types, you can read more about it here, or if you google for Java constant pool.

http://docs.oracle.com/javase/specs/jvms/se7/jvms7.pdf

此外,你可以做的小测试来推动这一点:

Also, a little test you can do to drive the point home:

String a = new String("xpto");
String b = new String("xpto");
String c = "xpto";
String d = "xpto";

System.out.println(a == b);
System.out.println(a == c);
System.out.println(c == d);

有了这一切,你可以找出这些Sysouts的结果:

With all this, you can probably figure out the results of these Sysouts:

false
false
true

由于 c d 是同一个对象, == 比较成立。

Since c and d are the same object, the == comparison holds true.

这篇关于new String()vs literal string performance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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