mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add jmh
This commit is contained in:
parent
5ef8fab528
commit
649d41cd8a
@ -11,6 +11,7 @@
|
|||||||
* 【http 】 ImgUtil增加getMainColor方法(pr#338@Gitee)
|
* 【http 】 ImgUtil增加getMainColor方法(pr#338@Gitee)
|
||||||
* 【core 】 改进TreeUtil.buid算法性能(pr#1594@Github)
|
* 【core 】 改进TreeUtil.buid算法性能(pr#1594@Github)
|
||||||
* 【core 】 CsvConfig的setXXX返回this(issue#I3UIQF@Gitee)
|
* 【core 】 CsvConfig的setXXX返回this(issue#I3UIQF@Gitee)
|
||||||
|
* 【all 】 增加jmh基准测试
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复FileUtil.normalize去掉末尾空格问题(issue#1603@Github)
|
* 【core 】 修复FileUtil.normalize去掉末尾空格问题(issue#1603@Github)
|
||||||
|
@ -510,6 +510,24 @@ public class CollUtil {
|
|||||||
return IterUtil.countMap(null == collection ? null : collection.iterator());
|
return IterUtil.countMap(null == collection ? null : collection.iterator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以 conjunction 为分隔符将集合转换为字符串
|
||||||
|
*
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @param iterable {@link Iterable}
|
||||||
|
* @param conjunction 分隔符
|
||||||
|
* @param func 集合元素转换器,将元素转换为字符串
|
||||||
|
* @return 连接后的字符串
|
||||||
|
* @see IterUtil#join(Iterator, CharSequence, Function)
|
||||||
|
* @since 5.6.7
|
||||||
|
*/
|
||||||
|
public static <T> String join(Iterable<T> iterable, CharSequence conjunction, Function<T, ? extends CharSequence> func) {
|
||||||
|
if (null == iterable) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return IterUtil.join(iterable.iterator(), conjunction, func);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 以 conjunction 为分隔符将集合转换为字符串<br>
|
* 以 conjunction 为分隔符将集合转换为字符串<br>
|
||||||
* 如果集合元素为数组、{@link Iterable}或{@link Iterator},则递归组合其为字符串
|
* 如果集合元素为数组、{@link Iterable}或{@link Iterator},则递归组合其为字符串
|
||||||
@ -1617,9 +1635,9 @@ public class CollUtil {
|
|||||||
* @since 5.6.6
|
* @since 5.6.6
|
||||||
*/
|
*/
|
||||||
public static <T> int lastIndexOf(Collection<T> collection, Matcher<T> matcher) {
|
public static <T> int lastIndexOf(Collection<T> collection, Matcher<T> matcher) {
|
||||||
if(collection instanceof List){
|
if (collection instanceof List) {
|
||||||
// List的查找最后一个有优化算法
|
// List的查找最后一个有优化算法
|
||||||
return ListUtil.lastIndexOf((List<T>)collection, matcher);
|
return ListUtil.lastIndexOf((List<T>) collection, matcher);
|
||||||
}
|
}
|
||||||
int matchIndex = -1;
|
int matchIndex = -1;
|
||||||
if (isNotEmpty(collection)) {
|
if (isNotEmpty(collection)) {
|
||||||
@ -2612,7 +2630,7 @@ public class CollUtil {
|
|||||||
* @since 5.4.7
|
* @since 5.4.7
|
||||||
*/
|
*/
|
||||||
public static <T> void forEach(Iterable<T> iterable, Consumer<T> consumer) {
|
public static <T> void forEach(Iterable<T> iterable, Consumer<T> consumer) {
|
||||||
if(iterable == null){
|
if (iterable == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
forEach(iterable.iterator(), consumer);
|
forEach(iterable.iterator(), consumer);
|
||||||
@ -2626,7 +2644,7 @@ public class CollUtil {
|
|||||||
* @param consumer {@link Consumer} 遍历的每条数据处理器
|
* @param consumer {@link Consumer} 遍历的每条数据处理器
|
||||||
*/
|
*/
|
||||||
public static <T> void forEach(Iterator<T> iterator, Consumer<T> consumer) {
|
public static <T> void forEach(Iterator<T> iterator, Consumer<T> consumer) {
|
||||||
if(iterator == null){
|
if (iterator == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@ -2644,7 +2662,7 @@ public class CollUtil {
|
|||||||
* @param consumer {@link Consumer} 遍历的每条数据处理器
|
* @param consumer {@link Consumer} 遍历的每条数据处理器
|
||||||
*/
|
*/
|
||||||
public static <T> void forEach(Enumeration<T> enumeration, Consumer<T> consumer) {
|
public static <T> void forEach(Enumeration<T> enumeration, Consumer<T> consumer) {
|
||||||
if(enumeration == null){
|
if (enumeration == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@ -2664,7 +2682,7 @@ public class CollUtil {
|
|||||||
* @param kvConsumer {@link KVConsumer} 遍历的每条数据处理器
|
* @param kvConsumer {@link KVConsumer} 遍历的每条数据处理器
|
||||||
*/
|
*/
|
||||||
public static <K, V> void forEach(Map<K, V> map, KVConsumer<K, V> kvConsumer) {
|
public static <K, V> void forEach(Map<K, V> map, KVConsumer<K, V> kvConsumer) {
|
||||||
if(map == null){
|
if (map == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@ -2986,7 +3004,7 @@ public class CollUtil {
|
|||||||
* @author Looly
|
* @author Looly
|
||||||
*/
|
*/
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface KVConsumer<K, V> extends Serializable{
|
public interface KVConsumer<K, V> extends Serializable {
|
||||||
/**
|
/**
|
||||||
* 接受并处理一对参数
|
* 接受并处理一对参数
|
||||||
*
|
*
|
||||||
|
@ -337,6 +337,31 @@ public class IterUtil {
|
|||||||
* @since 4.0.10
|
* @since 4.0.10
|
||||||
*/
|
*/
|
||||||
public static <T> String join(Iterator<T> iterator, CharSequence conjunction, String prefix, String suffix) {
|
public static <T> String join(Iterator<T> iterator, CharSequence conjunction, String prefix, String suffix) {
|
||||||
|
return join(iterator, conjunction, (item)->{
|
||||||
|
if (ArrayUtil.isArray(item)) {
|
||||||
|
return ArrayUtil.join(ArrayUtil.wrap(item), conjunction, prefix, suffix);
|
||||||
|
} else if (item instanceof Iterable<?>) {
|
||||||
|
return join((Iterable<?>) item, conjunction, prefix, suffix);
|
||||||
|
} else if (item instanceof Iterator<?>) {
|
||||||
|
return join((Iterator<?>) item, conjunction, prefix, suffix);
|
||||||
|
} else {
|
||||||
|
return StrUtil.wrap(String.valueOf(item), prefix, suffix);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 以 conjunction 为分隔符将集合转换为字符串<br>
|
||||||
|
* 如果集合元素为数组、{@link Iterable}或{@link Iterator},则递归组合其为字符串
|
||||||
|
*
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @param iterator 集合
|
||||||
|
* @param conjunction 分隔符
|
||||||
|
* @param func 集合元素转换器,将元素转换为字符串
|
||||||
|
* @return 连接后的字符串
|
||||||
|
* @since 5.6.7
|
||||||
|
*/
|
||||||
|
public static <T> String join(Iterator<T> iterator, CharSequence conjunction, Function<T, ? extends CharSequence> func) {
|
||||||
if (null == iterator) {
|
if (null == iterator) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -352,15 +377,7 @@ public class IterUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
item = iterator.next();
|
item = iterator.next();
|
||||||
if (ArrayUtil.isArray(item)) {
|
sb.append(func.apply(item));
|
||||||
sb.append(ArrayUtil.join(ArrayUtil.wrap(item), conjunction, prefix, suffix));
|
|
||||||
} else if (item instanceof Iterable<?>) {
|
|
||||||
sb.append(join((Iterable<?>) item, conjunction, prefix, suffix));
|
|
||||||
} else if (item instanceof Iterator<?>) {
|
|
||||||
sb.append(join((Iterator<?>) item, conjunction, prefix, suffix));
|
|
||||||
} else {
|
|
||||||
sb.append(StrUtil.wrap(String.valueOf(item), prefix, suffix));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
@ -826,7 +843,7 @@ public class IterUtil {
|
|||||||
* @since 5.5.0
|
* @since 5.5.0
|
||||||
*/
|
*/
|
||||||
public static int size(final Iterable<?> iterable) {
|
public static int size(final Iterable<?> iterable) {
|
||||||
if(null == iterable){
|
if (null == iterable) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,77 @@
|
|||||||
|
package cn.hutool.core.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.openjdk.jmh.annotations.Benchmark;
|
||||||
|
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||||
|
import org.openjdk.jmh.annotations.Fork;
|
||||||
|
import org.openjdk.jmh.annotations.Measurement;
|
||||||
|
import org.openjdk.jmh.annotations.Mode;
|
||||||
|
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||||
|
import org.openjdk.jmh.annotations.Scope;
|
||||||
|
import org.openjdk.jmh.annotations.State;
|
||||||
|
import org.openjdk.jmh.annotations.Threads;
|
||||||
|
import org.openjdk.jmh.annotations.Warmup;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
@BenchmarkMode(Mode.AverageTime)//每次执行平均花费时间
|
||||||
|
@Warmup(iterations = 5) //预热5次调用
|
||||||
|
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) // 执行5此,每次1秒
|
||||||
|
@Threads(1) //单线程
|
||||||
|
@Fork(1) //
|
||||||
|
@OutputTimeUnit(TimeUnit.NANOSECONDS) // 单位:纳秒
|
||||||
|
@State(Scope.Benchmark) // 共享域
|
||||||
|
public class StrUtilJmh {
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void joinJmh() {
|
||||||
|
CollUtil.join(initSize(20), ",", (org)-> String.valueOf(org.getProvinceId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
public void joinJmh2() {
|
||||||
|
final List<Org> orgs = initSize(20);
|
||||||
|
final Iterator<Org> iterator = orgs.iterator();
|
||||||
|
|
||||||
|
final StringBuilder sb = new StringBuilder();
|
||||||
|
boolean isFirst = true;
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
if (isFirst) {
|
||||||
|
isFirst = false;
|
||||||
|
} else {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append(iterator.next().getProvinceId());
|
||||||
|
}
|
||||||
|
sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 来自:ibeetl
|
||||||
|
* 模拟测试数据,建议使用较大size以验证效果
|
||||||
|
* @param size 数量
|
||||||
|
* @return 对象List
|
||||||
|
*/
|
||||||
|
public static List<Org> initSize(int size){
|
||||||
|
Org org = new Org();
|
||||||
|
org.setProvinceId(21);
|
||||||
|
org.setName("北京");
|
||||||
|
ArrayList<Org> list = new ArrayList<>();
|
||||||
|
for(int i=0;i<size;i++){
|
||||||
|
list.add(org);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Org {
|
||||||
|
private Integer id;
|
||||||
|
private Integer provinceId;
|
||||||
|
private String name;
|
||||||
|
}
|
||||||
|
}
|
2
pom.xml
2
pom.xml
@ -61,7 +61,7 @@
|
|||||||
<version>${lombok.version}</version>
|
<version>${lombok.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- 性能测试 -->
|
<!-- 基准性能测试 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.openjdk.jmh</groupId>
|
<groupId>org.openjdk.jmh</groupId>
|
||||||
<artifactId>jmh-core</artifactId>
|
<artifactId>jmh-core</artifactId>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user