Created
August 1, 2016 09:35
-
-
Save kkfnui/8edfab884418d9265ddb921de335dfbb to your computer and use it in GitHub Desktop.
ssdb 客户端性能测试
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
运行结果:
7
969
有一半的时间消耗在构造 String 对象中。 去除
Long.parseLong(str);
运行结果:8
454