Reputation: 4796
While I was testing my java program, I found out that the first run in a loop takes more time that the later runs.
Each of loop's task is to make 5 thumbnails of same image and store them in a zip file. I am using zip4j and thumbnailator. All of the runs have the same code.
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.
}
}
Here's my code.
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
As you can see from the result above, the first run from the loop takes twice the time as the rest.
What is happening here? And how can I reduce the time for the first run?
Upvotes: 4
Views: 1087
Reputation: 59
I don't think this is related to looping, rather it is related to the createZipFile() function that seems to do some initializing/loading that runs at first time it is called. Consider the following modified example that is producing identical run times in the loop:
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.
}
}
Upvotes: 1