How to reduce the time spent on cycles?

While I was testing my java program, I found out that the first run in a loop takes longer than it does later.

Each of the tasks of the cycle is to make 5 sketches of the same image and save them in a zip file. I am using zip4j and thumbnailator. All 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 is 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 can be seen from the above result, the first run from the cycle takes twice as much time.

What's going on here? And how can I shorten the time for the first launch?

+4
source share
1 answer

I don’t think that this is related to the loop, rather it is related to the createZipFile () function, which seems to do the initialization / loading, which runs the first time it is called. Consider the following modified example, which produces the same runtime in a 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. } } 
+1
source

Source: https://habr.com/ru/post/1499536/


All Articles