当我从IntelliJ IDE运行我的项目时,GUI很流畅,一切都运行得非常快。当我将它打包在JAR中时,速度非常慢。
编辑:这是一个证明问题的SSCCE。
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
public class Main {
public static void main(String[] args) throws Exception {
char[] secret = "Secret password".toCharArray();
byte[] salt = "Salty salt".getBytes();
int iterations = 10000;
int keyLengthBits = 128;
for (int i=0; i<20; i++) {
long timeStart = System.nanoTime();
SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
PBEKeySpec spec = new PBEKeySpec(secret, salt, iterations, keyLengthBits);
SecretKey key = skf.generateSecret(spec);
long timeEnd = System.nanoTime();
System.out.println((timeEnd-timeStart)/1000000 + "ms");
}
}
}
在我的计算机上,从IDE运行此程序打印25ms
并从JAR运行打印125ms
。