You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kylin.apache.org by " Kaige Liu (JIRA)" <ji...@apache.org> on 2018/01/25 08:31:00 UTC
[jira] [Updated] (KYLIN-3196) Replace StringUtils.containsOnly with
Regex
[ https://issues.apache.org/jira/browse/KYLIN-3196?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Kaige Liu updated KYLIN-3196:
-----------------------------
Description:
Notice that we use StringUtils.contains to validate project/cube/model names. It's not high efficiency and elegant.
I did a small test:
{code:java}
public class TempTest {
Pattern r = Pattern.compile("^[a-zA-Z0-9_]*$");
@Test
public void test() {
final char[] VALID_MODELNAME = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_".toCharArray();
String s1 = "abc@";
System.out.println("Call StringUtils.containsOnly 100 times");
long start = System.nanoTime();
for(int i =0; i<100; ++i) {
StringUtils.containsOnly(s1, VALID_MODELNAME);
}
long end = System.nanoTime();
System.out.println(end - start);
System.out.println("Call Regex match 100 times");
start = System.nanoTime();
for(int i =0; i<100; ++i) {
containsByRegex(s1);
}
end = System.nanoTime();
System.out.println(end - start);
}
private boolean containsByRegex(final String s) {
Matcher matcher = r.matcher(s);
return matcher.find();
}
}{code}
The result shows:
{code:java}
Call StringUtils.containsOnly 100 times
4740997
Call Regex match 100 times
753182
{code}
Conclusion:
Regex is better than StringUtils.containsOnly
was:
Notice that we use StringUtils.contains to validate project/cube/model names. It's not high efficiency and elegant.
I did a small test:
{code:java}
public class TempTest {
Pattern r = Pattern.compile("^[a-zA-Z0-9_]*$");
@Test
public void test() {
final char[] VALID_MODELNAME = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_".toCharArray();
String s1 = "abc@";
System.out.println("Call StringUtils.containsOnly 100 times");
long start = System.nanoTime();
for(int i =0; i<100; ++i) {
StringUtils.containsOnly(s1);
}
long end = System.nanoTime();
System.out.println(end - start);
System.out.println("Call Regex match 100 times");
start = System.nanoTime();
for(int i =0; i<100; ++i) {
containsByRegex(s1);
}
end = System.nanoTime();
System.out.println(end - start);
}
private boolean containsByRegex(final String s) {
Matcher matcher = r.matcher(s);
return matcher.find();
}
}{code}
The result shows:
{code:java}
Call StringUtils.containsOnly 100 times
4740997
Call Regex match 100 times
753182
{code}
Conclusion:
Regex is better than StringUtils.containsOnly
> Replace StringUtils.containsOnly with Regex
> -------------------------------------------
>
> Key: KYLIN-3196
> URL: https://issues.apache.org/jira/browse/KYLIN-3196
> Project: Kylin
> Issue Type: Bug
> Components: REST Service
> Reporter: Kaige Liu
> Assignee: Kaige Liu
> Priority: Minor
> Fix For: v2.3.0
>
>
> Notice that we use StringUtils.contains to validate project/cube/model names. It's not high efficiency and elegant.
>
> I did a small test:
> {code:java}
> public class TempTest {
> Pattern r = Pattern.compile("^[a-zA-Z0-9_]*$");
> @Test
> public void test() {
> final char[] VALID_MODELNAME = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_".toCharArray();
> String s1 = "abc@";
> System.out.println("Call StringUtils.containsOnly 100 times");
> long start = System.nanoTime();
> for(int i =0; i<100; ++i) {
> StringUtils.containsOnly(s1, VALID_MODELNAME);
> }
> long end = System.nanoTime();
> System.out.println(end - start);
> System.out.println("Call Regex match 100 times");
> start = System.nanoTime();
> for(int i =0; i<100; ++i) {
> containsByRegex(s1);
> }
> end = System.nanoTime();
> System.out.println(end - start);
> }
> private boolean containsByRegex(final String s) {
> Matcher matcher = r.matcher(s);
> return matcher.find();
> }
> }{code}
> The result shows:
> {code:java}
> Call StringUtils.containsOnly 100 times
> 4740997
> Call Regex match 100 times
> 753182
> {code}
>
> Conclusion:
> Regex is better than StringUtils.containsOnly
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)