Tag: 字符串连接

性能:Java的String.format

可能重复: 如果性能很重要,我应该使用Java的String.format()吗? 我想知道在Java应用程序中使用String.format而不是StringBuilder …所以,我只是编写一个简单的测试,如下所示: public static void main(String[] args) { int i = 0; Long start = System.currentTimeMillis(); while (i < 10000) { String s = String.format("test %d", i); i++; } System.out.println(System.currentTimeMillis() – start); i = 0; start = System.currentTimeMillis(); while (i < 10000) { String s = new StringBuilder().append("test ").append(i).toString(); i++; } System.out.println(System.currentTimeMillis() – […]

为什么字符串连接需要这么长时间?

我在一个循环中连接一个字符串,但它需要很长时间,为什么呢? for (String object : jsonData) { counter++; finalJsonDataStr += object; } 变量object是一块JSON,最多70个字符,循环大约50k次。 我理解有些人建议使用StringBuffer或StringBuilder但这个链接说,它没有性能改进: StringBuilder vs Java中toString()的字符串连接

大输入上的慢字符串连接

我写了一个n-ary树ADT工作正常。 但是,我需要将其序列化存储在一个变量调用类中。 例如。 DomTree a = Data.createTreeInstance(“very_large_file.xml”); String x = a.toString(); 我编写的方法完全符合我的需要,但在非常大的输入上它需要永远(在100MB xml文件上20分钟) – 我已经定时方法并从xml文件构建树很快,但是调用如上所示的toString()非常慢。 @Override public String toString(){ return printTree(this); } public String printTree(AbstractTree tree){ if (tree.isLeaf()){ return tree.getNodeName(); }else{ String tStr = tree.getNodeName() + “(“; int i = 0; Iterator<AbstractTree> child = tree.getChildren().iterator(); while (i < tree.getChildren().size() – 1){ tStr += printTree(child.next()) + […]

Java中的字符串+运算符

几分钟前我看到了这个问题 ,并决定查看java String类来检查+运算符是否有一些重载。 我找不到任何东西,但我知道我能做到这一点 String ab = “ab”; String cd = “cd”; String both = ab + cd; //both = “abcd” 实施的地方在哪里?

为什么+在Java中使用Strings?

Java不能执行运算符重载,但+对于String和Integer以及其他一些类都可以正常工作。 这怎么可能? 更新: 为什么这样做? Integer i = 4; Integer p = 5; System.out.println(i*p); // prints 20

字符串连接真的那么慢吗?

我目前正在研究String concat选项以及它们对整体性能的惩罚。 我的测试用例创造了令我心烦意乱的结果,我不确定我是否会忽略某些东西。 这是交易:在java中执行”something”+”somethingElse” (在编译时)每次完成时都会创建一个新的StringBuilder 。 对于我的测试用例,我正在从我的HDD加载一个包含1661行示例数据的文件 (经典的“Lorem Ipsum”)。 这个问题不是关于I / O性能 ,而是关于不同字符串concat方法的性能。 public class InefficientStringConcat { public static void main(String[] agrs) throws Exception{ // Get a file with example data: System.out.println(“Starting benchmark”); // Read an measure: for (int i = 0; i < 10; i++){ BufferedReader in = new BufferedReader( new InputStreamReader(new FileInputStream(new File("data.txt"))) […]

连接运算符(+)与concat()

对于字符串连接,我们可以使用concat()或concat运算符(+) 。 我尝试了以下性能测试,发现concat()更快,并且是一种内存有效的字符串连接方式。 字符串连接比较100,000次 : String str = null; //————Using Concatenation operator————- long time1 = System.currentTimeMillis(); long freeMemory1 = Runtime.getRuntime().freeMemory(); for(int i=0; i<100000; i++){ str = "Hi"; str = str+" Bye"; } long time2 = System.currentTimeMillis(); long freeMemory2 = Runtime.getRuntime().freeMemory(); long timetaken1 = time2-time1; long memoryTaken1 = freeMemory1 – freeMemory2; System.out.println("Concat operator :" + "Time […]

字符串等于和==字符串连接

我试图理解字符串连接与String比较的输出。 为了清楚起见,我有一个类使用==和equals来比较两个字符串。 我试图将==和equals()的输出连接到一个字符串。 equals()concats的输出,但==的输出不会连接。 使用java的装箱function,与字符串连接的布尔值将联系。 根据我的知识,equals和==都返回boolean。 那为什么会有这种差异呢? 任何人都可以解释一下吗? public class StringHandler { public void compareStrings() { String s1 = new String(“jai”); String s2 = “jai”; String s3 = “jai”; System.out.println(“Object and literal compare by double equal to :: ” + s1 == s2); System.out.println(“Object and literal compare by equals :: ” + s1.equals(s2)); System.out .println(“Literal […]

在Java中连接字符串是否总是导致在内存中创建新字符串?

我有一个不符合屏幕宽度的长字符串。 例如。 String longString = “This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed.”; 为了便于阅读,我想到了这样写 – String longString = “This string is very long.” + “It does not fit the width of the screen.” […]

Java字符串文字串联

public static void main(String[] args){ one(); two(); three(); } public static void one() { String s1 = “hill5”; String s2 = “hill” + 5; System.out.println(s1==s2); } public static void two() { String s1 = “hill5”; int i =5; String s2 = “hill” + i; System.out.println(s1==s2); } public static void three() { String s1 = “hill5”; […]