我的一个程序里有个方法比较耗时,
调试发现是new string比较耗时间,比如以下代码
StringBuilder sb = new StringBuilder(); long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
for (int i = 0; i < 100000; i++) {
String string = new String(bytes, 2, 2);
// String string = "test";
sb.append(string);
}
long end = System.currentTimeMillis(); System.out.println(end - start);用new string的话要60毫秒,
而直接用test的话只要16毫秒,问题是我必需从bytes中取得后面两位的数据, 前面两位没有用,
有什么办法优化吗?
调试发现是new string比较耗时间,比如以下代码
StringBuilder sb = new StringBuilder(); long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
for (int i = 0; i < 100000; i++) {
String string = new String(bytes, 2, 2);
// String string = "test";
sb.append(string);
}
long end = System.currentTimeMillis(); System.out.println(end - start);用new string的话要60毫秒,
而直接用test的话只要16毫秒,问题是我必需从bytes中取得后面两位的数据, 前面两位没有用,
有什么办法优化吗?
解决方案 »
- JDialog 中setVisible的问题
- 在windows 2003 server下整合apache2.2和tomcat5.5,有点难度,都来看看
- 请问如何用jstl将Map中的键、值一起成对显示出来?
- 问几个关于java基础的但是拿不准的问题,能回答的都有分~!
- 大家帮忙看看,通过报错帮我分析一下错误的原因。
- 一个低级问题:请问java的jdbc下载后一般放在哪里使用?
- 关于java打印,如何才能自定义纸张大小?谢谢!
- Toolkit.getDefaultToolkit().getScreenSize()获取的是全屏幕的大小。我想获取鼠标周围一小范围
- 请问我在一个applet中动态的写了很多的JLabel(每个JLabel都没有具体的名称),如何在点击某一个JLabel时出发鼠标事件,得到该JLabel的文本
- 关于java写gui,提问。(关于事件处理方面)
- 高手帮帮忙,一道笔试题
- jsoup解析链接时出现java.nio.charset.IllegalCharsetNameException:utf-8报错
long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
String string=null;
for (int i = 0; i < 100000; i++) {
string = new String(bytes, 2, 2);
sb.append(string);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
放在for外面,
还是说bytes每次不一样?
2楼的, 问题是byte数组不是固定的, 也就是string也是变化的
char[] bytes = {1,2,3,4};
for (int i = 0; i < 100000; i++) {
sb.append(bytes, 2, 2);
}
long end = System.currentTimeMillis(); System.out.println(end - start);只要5ms
int是可以直接当做char的
这个好啊, 可惜我取到的值已经是byte数组了
难道我自己复制下?
如果bytes长度不多,可以复制下;如果很长,可能就得不偿失了。
如果不能改,你每次取bytes里面的位数都确定么,每次都是最后两个么?
public static void main(String[] args) {
long start = System.currentTimeMillis();
byte[] bytes = { 1, 2, 3, 4 };
byte[] target = new byte[1024 * 1024];
int currentIndex = 0;
for (int i = 0; i < 10000000; i++) {
if(currentIndex + 2 > target.length){
byte[] newTarget = new byte[target.length * 3 / 2];
System.arraycopy(target, 0, newTarget, 0, currentIndex);
target = newTarget;
}
System.arraycopy(bytes, 2, target, currentIndex, 2);
currentIndex += 2;
}
String str = new String(target, 0, currentIndex);
System.out.println(str.length());
long end = System.currentTimeMillis();
System.out.println(end - start);
}次数改成10000000了,你那个方法要2s多,这个600多ms
StringBuilder sb = new StringBuilder();
long start = System.currentTimeMillis();
byte[] bytes = {1,2,3,4};
String string=null;
byte[] des = new byte[2];
for (int i = 0; i < 100000; i++) {
System.arraycopy(bytes, 2, des, 0, 2);
string =Arrays.toString(des);
sb.append(string);
}
long end = System.currentTimeMillis();
System.out.println(end - start);
package pkg;public class TestBytes { /**
* @param args
*/
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(); byte[] bytes = {1, 2,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,
97, 98, 97, 98, 97, 98, 97, 98, 97, 98,}; System.out.print(foo1(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo2(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo3(sb, bytes));
sb.delete(0, sb.length());
System.out.print(",");
System.out.print(foo4(sb, bytes));
sb.delete(0, sb.length());
System.out.println(); } /**
* @param sb
* @param bytes
*/
private static long foo4(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) {
for (int j = 0; j < bytes.length - 2; j++) {
sb.append(bytes[j + 2]);
} }
long end = System.currentTimeMillis(); return end - start;
} /**
* @param sb
* @param bytes
*/
private static long foo3(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) {
char[] chars = new char[40];
for (int j = 0; j < chars.length; j++) {
chars[j] = (char) bytes[j + 2];
}
sb.append(chars); }
long end = System.currentTimeMillis(); return end - start;
} /**
* @param sb
* @param bytes
*/
private static long foo2(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis(); String string = null;
for (int i = 0; i < 100000; i++) {
string = new String(bytes, 2, 40);
sb.append(string); }
long end = System.currentTimeMillis(); return end - start;
} /**
* @param sb
* @param bytes
*/
private static long foo1(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
String string = "abababababababababababababababababababab";
for (int i = 0; i < 100000; i++) {
sb.append(string); }
long end = System.currentTimeMillis(); return end - start;
}}
结果为
62,110,15,140
* @param sb
* @param bytes
*/
private static long foo3(StringBuilder sb, byte[] bytes) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
char[] chars = new char[40];
for (int j = 0; j < chars.length; j++) {
chars[j] = (char) bytes[j + 2];
}
sb.append(chars);
}
long end = System.currentTimeMillis();
return end - start;
}如果byte[]是由中文字符串获取的,将byte直接转换为char是错误的做法, private static String foo( byte[] bytes) { int time = 100000;
int size = 0;
byte[] buff = new byte[time<<2];
for (int i = 0; i < time; i++) {
int contentSize = bytes.length-2;
if(buff.length-size<contentSize){
int tmpLength = buff.length;
while(tmpLength-size<contentSize){
tmpLength=tmpLength<<1;
}
byte[] tmp = new byte[tmpLength];
System.arraycopy(buff,0,tmp,0,size);
buff=tmp;
}
System.arraycopy(bytes, 2, buff, size, contentSize);
size +=contentSize;
}
String result = new String(buff,0,size);
return result;
}