当我测试我的java程序时,我发现循环中的第一次运行需要更多时间才能运行。
每个循环的任务是制作5张相同图像的缩略图并将它们存储在一个zip文件中。我正在使用zip4j和thumbnailator。所有的运行都有相同的代码。
public static void main(String[] args) throws IOException {
try {
for(int i=0;i<10;i++){
long start = System.currentTimeMillis();
ZipFile zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
parameters.setIncludeRootFolder(false);
ArrayList<File> files = new ArrayList<File>();
for(int j=1;j<5;j++){
files.add(new File("C:\\savedFile2\\1.jpg"));
}
zipFile.createZipFile(files, parameters);
File zippedFile = zipFile.getFile();
byte[] buffer = new byte[(int)zippedFile.length()];
FileInputStream fis = new FileInputStream(zippedFile);
fis.read(buffer);
fis.close();
zippedFile.delete();
System.out.println("Time taken for "+(i+1)+"th run: "+(System.currentTimeMillis() - start));
}
} catch (ZipException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
这是我的代码。
Time taken for 1th run: 58
Time taken for 2th run: 24
Time taken for 3th run: 24
Time taken for 4th run: 24
Time taken for 5th run: 25
Time taken for 6th run: 24
Time taken for 7th run: 25
Time taken for 8th run: 25
Time taken for 9th run: 25
Time taken for 10th run: 29
从上面的结果可以看出,循环中的第一次运行所花费的时间是其余部分的两倍。
这里发生了什么?我怎样才能减少第一次运行的时间?
答案 0 :(得分:1)
我不认为这与循环有关,而是与createZipFile()函数有关,它似乎做了一些在第一次调用时运行的初始化/加载。 请考虑以下修改示例,该示例在循环中生成相同的运行时间:
public static void main(String[] args) throws IOException {
try {
long _start = System.currentTimeMillis();
ZipFile _zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters _parameters = new ZipParameters();
_parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
_parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
_parameters.setIncludeRootFolder(false);
ArrayList<File> _files = new ArrayList<File>();
for(int j=1;j<5;j++){
_files.add(new File("1.jpg"));
}
System.out.println("Initializing files: "+(System.currentTimeMillis() - _start));
_zipFile.createZipFile(_files, _parameters);
System.out.println("Initial run: "+(System.currentTimeMillis() - _start));
for(int i=0;i<10;i++){
long start = System.currentTimeMillis();
ZipFile zipFile = new ZipFile(System.nanoTime()+".zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_STORE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_FASTEST);
parameters.setIncludeRootFolder(false);
ArrayList<File> files = new ArrayList<File>();
for(int j=1;j<5;j++){
files.add(new File("1.jpg"));
}
zipFile.createZipFile(files, parameters);
File zippedFile = zipFile.getFile();
byte[] buffer = new byte[(int)zippedFile.length()];
FileInputStream fis = new FileInputStream(zippedFile);
fis.read(buffer);
fis.close();
zippedFile.delete();
System.out.println("Time taken for "+(i+1)+"tenter code hereh run: "+(System.currentTimeMillis() - start));
}
} catch (ZipException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}