Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize like logic #239

Merged
merged 3 commits into from
Mar 31, 2023
Merged

optimize like logic #239

merged 3 commits into from
Mar 31, 2023

Conversation

DQinYuan
Copy link
Collaborator

@DQinYuan DQinYuan commented Mar 5, 2023

修复 like 逻辑的 bug 和优化性能。

#179

@DQinYuan DQinYuan requested a review from pymBupt March 5, 2023 09:22
@DQinYuan
Copy link
Collaborator Author

DQinYuan commented Mar 5, 2023

挑选了一个常见场景进行 benchmark,预计性能能有 3 倍提升,benchmark 代码如下:

    @Test
    public void matchOldBenchMark() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            matchOld("abdbc", "a%bc");
        }
        // 7
        System.out.println(System.currentTimeMillis() - startTime);
    }

    @Test
    public void matchNewBenchMark() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            OperatorLike.matchPattern("abdbc", "a%bc");
        }
        // 2
        System.out.println(System.currentTimeMillis() - startTime);
    }

    private static boolean matchOld(String target, String pattern) {
        if (pattern.contains("%")) {
            String[] list = split(pattern, "%");
            int index = 0;
            for (String s : list) {
                if (index >= target.length()) {
                    return false;
                }
                index = target.indexOf(s, index);
                if (index < 0) {
                    return false;
                }
                index = index + 1;
            }
            return true;
        } else {
            return target.equals(pattern);
        }
    }

    private static String[] split(String str, String s) {
        int start = 0;
        int end;
        String tmpStr;
        ArrayList<String> list = new ArrayList<>();
        do {
            end = str.indexOf(s, start);
            if (end < 0) {
                tmpStr = str.substring(start);
            } else {
                tmpStr = str.substring(start, end);
            }
            if (tmpStr.length() > 0) {
                list.add(tmpStr);
            }
            start = end + 1;
            if (start >= str.length()) {
                break;
            }
        } while (end >= 0);
        return list.toArray(new String[0]);
    }

老方法需要 7ms,而新方法只要 2ms

Copy link
Collaborator

@pymBupt pymBupt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

approved

@DQinYuan DQinYuan merged commit b260e7d into master Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants