안녕하세요.

기존 Array방식을 안쓰고 NIO통신으로 바꿔서 ByteBuffer를 사용할려고 하는데요.


ByteBuffer만을 두고 실험을 해봤습니다.

기존 Array방식보다 확실히 빨라진것은 확인했습니다. 


ByteBuffer에서 allocate와 allocateDirect를 비교해봤는데 

각종 웹페이지에서 설명으론 Direct가 좀더 빠르다고 했는데 

실질적으로 비교해보니 별로 많이 빨라지지 않았더군요.


  private static void allocate4(){
  byte[] OrderIncrease = new byte[1024];
  for(int i=0; i<1024; i++){
   OrderIncrease[i] = ((byte)i);
  }
  
  long firstTime = 0;
  if(SWITCH){
   ByteBuffer buffer = ByteBuffer.allocate(1024);
   firstTime = System.currentTimeMillis();
   for(int i=0; i<1000000; i++){
    buffer.clear();
    buffer.put(OrderIncrease);
   }
  }
  else{
   ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
   firstTime = System.currentTimeMillis();
   for(int i=0; i<1000000; i++){
    buffer.clear();
    buffer.put(OrderIncrease);
   }
  }
  long secondTime = System.currentTimeMillis();
  System.out.println("Process Time : "+ (secondTime-firstTime));
 }
 
 private static void allocate5(){
  byte[] OrderIncrease = new byte[1024];
  for(int i=0; i<1024; i++){
   OrderIncrease[i] = ((byte)i);
  }
  byte[] buffer = new byte[1024];
  long firstTime = System.currentTimeMillis();
  for(int i=0; i<1000000; i++){
   for(int j=0; j<1024; j++){
    buffer[j] = OrderIncrease[j];
   }   
  }
  long secondTime = System.currentTimeMillis();
  System.out.println("Process Time : "+ (secondTime-firstTime));
 }


혹시 가비지 때문인가 해서 자바를 껏다 켰다 방식으로 해봤는데.

allocate4(SWITCH==true)의 경우 평균 170

allocate4(SWITCH==true)의 경우 평균 320

allocate5()의 경우 3000 정도로 출력되었습니다.


뭔가 실험방법이 잘못된걸까요?