gpt4 book ai didi

java - 像 Guava Splitter 一样快速地搜索大型 CSV

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:03 25 4
gpt4 key购买 nike

自从 Java 8 发布后,我发现我的项目中不需要超过 2 MB 的 Google Guava,因为我可以用纯 Java 替换其中的大部分。然而,我真的很喜欢不错的 Splitter API,它同时非常快。最重要的是 - 确实懒惰地 split 了。它似乎可以用 Pattern.splitAsStream 替换。所以我准备了快速测试 - 在长字符串的中间找到一个值(即拆分整个字符串没有意义)。

package splitstream;


import com.google.common.base.Splitter;
import org.junit.Assert;
import org.junit.Test;

import java.util.StringTokenizer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class SplitStreamPerfTest {

private static final int TIMES = 1000;
private static final String FIND = "10000";

@Test
public void go() throws Exception {
final String longString = IntStream.rangeClosed(1,20000).boxed()
.map(Object::toString)
.collect(Collectors.joining(" ,"));

IntStream.rangeClosed(1,3).forEach((i) -> {
measureTime("Test " + i + " with regex", () -> doWithRegex(longString));
measureTime("Test " + i + " with string tokenizer", () -> doWithStringTokenizer(longString));
measureTime("Test " + i + " with guava", () -> doWithGuava(longString));
});

}

private void measureTime(String name, Runnable r) {
long s = System.currentTimeMillis();
r.run();
long elapsed = System.currentTimeMillis() - s;
System.out.println("Check " + name +" took " + elapsed + " ms");
}

private void doWithStringTokenizer(String longString) {

String f = null;
for (int i = 0; i < TIMES; i++) {
StringTokenizer st = new StringTokenizer(longString,",",false);
while (st.hasMoreTokens()) {
String t = st.nextToken().trim();
if (FIND.equals(t)) {
f = t;
break;
}
}
}
Assert.assertEquals(FIND, f);
}


private void doWithRegex(String longString) {
final Pattern pattern = Pattern.compile(",");
String f = null;
for (int i = 0; i < TIMES; i++) {
f = pattern.splitAsStream(longString)
.map(String::trim)
.filter(FIND::equals)
.findFirst().orElse("");
}
Assert.assertEquals(FIND, f);
}


private void doWithGuava(String longString) {
final Splitter splitter = Splitter.on(',').trimResults();
String f = null;
for (int i = 0; i < TIMES; i++) {
Iterable<String> iterable = splitter.split(longString);
for (String s : iterable) {
if (FIND.equals(s)) {
f = s;
break;
}
}
}
Assert.assertEquals(FIND, f);
}
}

结果是(热身后)

Check Test 3 with regex took 1359 ms
Check Test 3 with string tokenizer took 750 ms
Check Test 3 with guava took 594 ms

如何让Java实现和Guava一样快?也许我做错了?

或者您可能知道有哪个工具/库的速度与 Guava Splitter 一样快,而且不需要为此提取大量未使用的类?

最佳答案

首先,guava 远不止 SplitterPredicateFunction - 你可能没有使用它所提供的一切;我们使用它是铁杆,只是听到它让我颤抖。无论如何,您的测试被破坏了——可能以多种方式。我已经使用 JMH 来测试这两种方法只是为了好玩:

    @BenchmarkMode(org.openjdk.jmh.annotations.Mode.AverageTime) 
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Warmup(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
@State(Scope.Thread) public class GuavaTest {

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder().include(GuavaTest.class.getSimpleName())
.jvmArgs("-ea", "-Xms10g", "-Xmx10g")
.shouldFailOnError(true)
.build();
new Runner(opt).run();
}

@Param(value = { "300", "1000" })
public String tokenToSearchFor;

@State(Scope.Benchmark)
public static class ThreadState {
String longString = IntStream.range(1, 20000).boxed().map(Object::toString).collect(Collectors.joining(" ,"));

StringTokenizer st = null;

Pattern pattern = null;

Splitter splitter = null;

@Setup(Level.Invocation)
public void setUp() {
st = new StringTokenizer(longString, ",", false);
pattern = Pattern.compile(",");
splitter = Splitter.on(',').trimResults();
}
}

@Benchmark
@Fork(1)
public boolean doWithStringTokenizer(ThreadState ts) {
while (ts.st.hasMoreTokens()) {
String t = ts.st.nextToken().trim();
if (t.equals(tokenToSearchFor)) {
return true;
}
}
return false;
}

@Benchmark
@Fork(1)
public boolean doWithRegex(ThreadState ts) {
return ts.pattern.splitAsStream(ts.longString)
.map(String::trim)
.anyMatch(tokenToSearchFor::equals);
}

@Benchmark
@Fork(1)
public boolean doWithGuava(ThreadState ts) {
Iterable<String> iterable = ts.splitter.split(ts.longString);
for (String s : iterable) {
if (s.equals(tokenToSearchFor)) {
return true;
}
}
return false;
}

}

结果:

Benchmark                        (tokenToSearchFor)  Mode  Cnt       Score        Error  Units
GuavaTest.doWithGuava 300 avgt 5 19284.192 ± 23536.321 ns/op
GuavaTest.doWithGuava 1000 avgt 5 67182.531 ± 93242.266 ns/op
GuavaTest.doWithRegex 300 avgt 5 65780.954 ± 169044.641 ns/op
GuavaTest.doWithRegex 1000 avgt 5 182530.069 ± 409571.222 ns/op
GuavaTest.doWithStringTokenizer 300 avgt 5 34111.030 ± 61014.332 ns/op
GuavaTest.doWithStringTokenizer 1000 avgt 5 118963.048 ± 165510.183 ns/op

这使得 Guava 确实是最快的。

如果您将parallel 添加到splitAsStream 中,它将变得有趣,必读here

关于java - 像 Guava Splitter 一样快速地搜索大型 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45001491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com