mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-12 08:38:30 +08:00
add CharBufferJmh
This commit is contained in:
parent
c7881c74fd
commit
00c01eedac
@ -41,6 +41,11 @@ import java.nio.charset.Charset;
|
||||
*/
|
||||
public class NioUtil {
|
||||
|
||||
/**
|
||||
* 默认小缓存大小 1024
|
||||
*/
|
||||
public static final int DEFAULT_SMALL_BUFFER_SIZE = 2 << 9;
|
||||
|
||||
/**
|
||||
* 默认缓存大小 8192
|
||||
*/
|
||||
|
@ -41,7 +41,7 @@ public class FastByteBuffer extends FastBuffer {
|
||||
* 构造
|
||||
*/
|
||||
public FastByteBuffer() {
|
||||
this(IoUtil.DEFAULT_BUFFER_SIZE);
|
||||
this(IoUtil.DEFAULT_SMALL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,8 @@ import org.dromara.hutool.core.lang.Assert;
|
||||
|
||||
/**
|
||||
* 代码移植自jetbrick<br>
|
||||
* 快速字符缓冲,将数据存放在缓冲集中,取代以往的单一数组
|
||||
* 快速字符缓冲,将数据存放在缓冲集中,取代以往的单一数组<br>
|
||||
* 注意:此缓存在大量重复append时,性能比{@link StringBuilder}要好,但是{@link #toArray()}性能很差
|
||||
*
|
||||
* @author jetbrick, looly
|
||||
*/
|
||||
@ -40,7 +41,7 @@ public class FastCharBuffer extends FastBuffer implements CharSequence, Appendab
|
||||
* 构造
|
||||
*/
|
||||
public FastCharBuffer() {
|
||||
this(IoUtil.DEFAULT_BUFFER_SIZE);
|
||||
this(IoUtil.DEFAULT_SMALL_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -14,16 +14,15 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.core.io;
|
||||
package org.dromara.hutool.core.io.buffer;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.dromara.hutool.core.io.buffer.BufferUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* BufferUtil单元测试
|
||||
*
|
@ -0,0 +1,40 @@
|
||||
package org.dromara.hutool.core.io.buffer;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.SECONDS) //预热1次调用
|
||||
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||
@Threads(1) //单线程
|
||||
@Fork(1) //
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||
@State(Scope.Benchmark) // 共享域
|
||||
public class CharBufferJmh {
|
||||
|
||||
private final int appendCount = 10000;
|
||||
private String str;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
str = "abc123你好";
|
||||
}
|
||||
|
||||
@SuppressWarnings("MismatchedQueryAndUpdateOfStringBuilder")
|
||||
@Benchmark
|
||||
public void stringBuilderJmh() {
|
||||
final StringBuilder stringBuilder = new StringBuilder(1024);
|
||||
for (int i = 0; i < appendCount; i++) {
|
||||
stringBuilder.append(str);
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fastCharBufferJmh() {
|
||||
final FastCharBuffer fastCharBuffer = new FastCharBuffer(1024);
|
||||
for (int i = 0; i < appendCount; i++) {
|
||||
fastCharBuffer.append(str);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user