feat:编写测试类信息
parent
49487a999d
commit
32433af35e
|
@ -0,0 +1,64 @@
|
|||
package com.lijie;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* 直接内存 与 堆内存的比较
|
||||
*/
|
||||
public class ByteBufferCompare {
|
||||
|
||||
public static void main(String[] args) {
|
||||
allocateCompare(); //分配比较
|
||||
operateCompare(); //读写比较
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接内存 和 堆内存的 分配空间比较
|
||||
*/
|
||||
public static void allocateCompare() {
|
||||
int time = 10000000; //操作次数
|
||||
long st = System.currentTimeMillis();
|
||||
for (int i = 0; i < time; i++) {
|
||||
|
||||
ByteBuffer buffer = ByteBuffer.allocate(2); //非直接内存分配申请
|
||||
}
|
||||
long et = System.currentTimeMillis();
|
||||
System.out.println("在进行" + time + "次分配操作时,堆内存:分配耗时:" + (et - st) + "ms");
|
||||
long st_heap = System.currentTimeMillis();
|
||||
for (int i = 0; i < time; i++) {
|
||||
ByteBuffer buffer = ByteBuffer.allocateDirect(2); //直接内存分配申请
|
||||
}
|
||||
long et_direct = System.currentTimeMillis();
|
||||
System.out.println("在进行" + time + "次分配操作时,直接内存:分配耗时:" + (et_direct - st_heap) + "ms");
|
||||
}
|
||||
|
||||
/**
|
||||
* 直接内存 和 堆内存的 读写性能比较
|
||||
*/
|
||||
public static void operateCompare() {
|
||||
//如果报错修改这里,把数字改小一点
|
||||
int time = 1000000000;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(2 * time);
|
||||
long st = System.currentTimeMillis();
|
||||
for (int i = 0; i < time; i++) {
|
||||
buffer.putChar('a');
|
||||
}
|
||||
buffer.flip();
|
||||
for (int i = 0; i < time; i++) {
|
||||
buffer.getChar();
|
||||
}
|
||||
long et = System.currentTimeMillis();
|
||||
System.out.println("在进行" + time + "次读写操作时,堆内存:读写耗时:" + (et - st) + "ms");
|
||||
ByteBuffer buffer_d = ByteBuffer.allocateDirect(2 * time);
|
||||
long st_direct = System.currentTimeMillis();
|
||||
for (int i = 0; i < time; i++) {
|
||||
buffer_d.putChar('a');
|
||||
}
|
||||
buffer_d.flip();
|
||||
for (int i = 0; i < time; i++) {
|
||||
buffer_d.getChar();
|
||||
}
|
||||
long et_direct = System.currentTimeMillis();
|
||||
System.out.println("在进行" + time + "次读写操作时,直接内存:读写耗时:" + (et_direct - st_direct) + "ms");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue