Skip to content

Instantly share code, notes, and snippets.

@AdamStelmaszczyk
Last active August 16, 2021 05:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamStelmaszczyk/555acba98e6982a02f71 to your computer and use it in GitHub Desktop.
Save AdamStelmaszczyk/555acba98e6982a02f71 to your computer and use it in GitHub Desktop.
Fastest way to check if comma separated string contains at least 1 valid integer?
package org.sample;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@OutputTimeUnit(TimeUnit.SECONDS)
@BenchmarkMode({Mode.Throughput})
@Warmup(iterations = 10)
@Fork(value = 1)
@State(Scope.Benchmark)
public class MyBenchmark {
private static final int LOOPS = 100000;
public static boolean jim(String string) {
String[] splits = string.split(",");
for (String split : splits) {
try {
Integer.parseInt(split);
return true;
} catch (NumberFormatException ignored) {
}
}
return false;
}
@Benchmark
public void testJim() {
String string = "1,34,433,12";
for (int i = 0; i < LOOPS; i++) {
jim(string);
}
}
public static boolean jean(String string) {
boolean onlyDigits = true;
boolean atLeastOneDigit = false;
final int len = string.length();
for (int i = 0; i < len; i++) {
char c = string.charAt(i);
if (c == ',') {
if (atLeastOneDigit && onlyDigits) {
return true;
}
onlyDigits = true;
atLeastOneDigit = false;
continue;
}
boolean isDigit = c >= '0' && c <= '9';
onlyDigits &= isDigit;
atLeastOneDigit |= isDigit;
}
return atLeastOneDigit && onlyDigits;
}
@Benchmark
public void testJean() {
String string = "1,34,433,12";
for (int i = 0; i < LOOPS; i++) {
jean(string);
}
}
public static Pattern pattern = Pattern.compile("(\\A|,)\\d+(\\z|,)");
public static boolean andreas(final String string) {
return pattern.matcher(string).find();
}
@Benchmark
public void testAndreas() {
String string = "1,34,433,12";
for (int i = 0; i < LOOPS; i++) {
andreas(string);
}
}
public static void unitTests() {
String[] valid = {
"1,34,433,12",
"12,safdg,sfdg",
"asfd,asdf,12",
"asfd,10,jrtj",
"12356",
",12356,"
};
String[] invalid = {
"asdf",
"1a,2b,13c",
",asdf,",
",,,",
"",
};
for (String s : valid) {
assert jim(s);
assert jean(s);
assert andreas(s);
}
for (String s : invalid) {
assert !jim(s);
assert !jean(s);
assert !andreas(s);
}
}
public static void main(String[] args) throws RunnerException {
unitTests();
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.forks(1)
.build();
new Runner(opt).run();
}
}
@SumedhaSahdev
Copy link

Hi, I want to three strings in an input = {"corn,mix veg,cheese"} using a consumer interface and then print them using supplier interfcae. Also, I need to check that if even one of the strings is other than these three values i.e. corn, mix veg or cheese (in any order) , then it should display "Incorrect Input". Can you please help me how to do it using these functional interfaces ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment