Skip to content

Instantly share code, notes, and snippets.

@kkfnui
Created August 1, 2016 09:35
Show Gist options
  • Save kkfnui/8edfab884418d9265ddb921de335dfbb to your computer and use it in GitHub Desktop.
Save kkfnui/8edfab884418d9265ddb921de335dfbb to your computer and use it in GitHub Desktop.
ssdb 客户端性能测试
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
* Created by lvfei on 16/8/1.
*/
public class ParseTest {
public static void main(String[] args) {
byte[] buf = {'2', '0', '0', '0'};
long start = System.currentTimeMillis();
int count = 50000000;
for (int i = 0; i < count; i++) {
long value = parse(buf);
}
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
long value = stringParse(buf);
}
System.out.println(System.currentTimeMillis() - start);
}
private static long parse(byte[] buf) {
long value = 0;
for (int i = 0; i < buf.length; i++) {
byte b = buf[i];
value = value * 10 + b - '0';
}
return value;
}
private static long stringParse(byte[] buf) {
String str = new String(buf);
long l = Long.parseLong(str);
return l;
}
}
@kkfnui
Copy link
Author

kkfnui commented Aug 1, 2016

运行结果:
7
969

有一半的时间消耗在构造 String 对象中。 去除 Long.parseLong(str); 运行结果:

8
454

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment