2013-08-29 4 views
4

자바 프로그램을 테스트하는 동안 루프에서 처음 실행하면 나중에 실행되는 데 더 많은 시간이 걸리는 것으로 나타났습니다.루프 시간을 줄이는 방법은 무엇입니까?

각 루프의 작업은 동일한 이미지의 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 

위 결과에서 알 수 있듯이 루프의 첫 번째 실행은 나머지 2 시간 걸립니다.

여기 무슨 일입니까? 그리고 처음 실행하는 시간을 어떻게 줄일 수 있습니까?

+7

이있을 수 있습니다 인해 JVM이 때문에입니다 프로그램 – sanbhat

+6

을 시작하기 전에 걸리는 오버 헤드에 [워밍업] (HTTP : // 유래 .com/questions/1481853/technique-or-utility-to-minimize-java-warm-up-time) – rocketboy

+0

클래스가로드되어야하기 때문입니다. java-vm 커맨드 라인의'verbose' 옵션을 시도해보십시오. – Claude

답변

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. 
    } 
} 
+0

당신의 대답이 옳은 것 같습니다. 의견 제시자가 위에서 말했듯이, 워밍업과 JVM 오버 헤드로부터 추가 시간이 걸리는 것처럼 보입니다. –

관련 문제