是的,sun.misc.BASE64Decoder速度比java.xml.bind.DatatypeConverter.parseBase64Binary()慢9倍,比org.apache.commons.codec.binary.Base64.decodeBase64()慢4倍,至少對於Java 6 OSX上的一個小字符串。
下面是我使用的測試程序。使用OSX上的Java 1.6.0_43:
john:password = am9objpwYXNzd29yZA==
javax.xml took 373: john:password
apache took 612: john:password
sun took 2215: john:password
順便說一句,這與公共編解碼器1.4。有了1.7,它似乎變慢了:
javax.xml took 377: john:password
apache took 1681: john:password
sun took 2197: john:password
未測試Java 7或其他操作系統。
import javax.xml.bind.DatatypeConverter;
import org.apache.commons.codec.binary.Base64;
import java.io.IOException;
public class TestBase64 {
private static volatile String save = null;
public static void main(String argv[]) {
String teststr = "john:password";
String b64 = DatatypeConverter.printBase64Binary(teststr.getBytes());
System.out.println(teststr + " = " + b64);
try {
final int COUNT = 1000000;
long start;
start = System.currentTimeMillis();
for (int i=0; i<COUNT; ++i) {
save = new String(DatatypeConverter.parseBase64Binary(b64));
}
System.out.println("javax.xml took "+(System.currentTimeMillis()-start)+": "+save);
start = System.currentTimeMillis();
for (int i=0; i<COUNT; ++i) {
save = new String(Base64.decodeBase64(b64));
}
System.out.println("apache took "+(System.currentTimeMillis()-start)+": "+save);
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
start = System.currentTimeMillis();
for (int i=0; i<COUNT; ++i) {
save = new String(dec.decodeBuffer(b64));
}
System.out.println("sun took "+(System.currentTimeMillis()-start)+": "+save);
} catch (Exception e) {
System.out.println(e);
}
}
}
是的,在這裏找到它:http://commons.apache.org/codec/ – jyz 2012-04-12 17:46:05
@downvoter請解釋,除非你想你downvote被視爲純粹的網站破壞。 – EJP 2012-11-09 04:02:21
我想這是downvoted贊成nikunj或Jon Skeeter的答案。 – 2013-01-25 09:54:51