java 比较两个字符串的前三个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2261697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare first three characters of two strings
提问by nalo
Strings s1 and s2 will always be of length 1 or higher.
字符串 s1 和 s2 的长度总是大于等于 1。
How can I speed this up?
我怎样才能加快速度?
int l1 = s1.length();
if (l1 > 3) { l1 = 3; }
if (s2.startsWith(s1.substring(0,l1)))
{
// do something..
}
Regex maybe?
也许正则表达式?
回答by DigitalRoss
Rewrite to avoid object creation
重写以避免对象创建
Your instincts were correct. The creation of new objects (substring()) is not very fast and it means that each one created must incur g/c overhead as well.
你的直觉是正确的。新对象 (substring()) 的创建速度不是很快,这意味着每个创建的对象也必须产生 g/c 开销。
This might be a lot faster:
这可能会快很多:
static boolean fastCmp(String s1, String s2) {
return s1.regionMatches(0, s2, 0, 3);
}
回答by luiscubal
This seems pretty reasonable. Is this really too slow for you? You sure it's not premature optimization?
这似乎很合理。这对你来说真的太慢了吗?你确定这不是过早优化?
回答by Bozho
if (s2.startsWith(s1.substring(0, Math.min(3, s1.length())) {..};
Btw, there is nothing slow in it. startsWithhas complexity O(n)
顺便说一句,没有什么慢的。startsWith有复杂性O(n)
Another option is to compare the char values, which might be more efficient:
另一种选择是比较字符值,这可能更有效:
boolean match = true;
for (int i = 0; i < Math.min(Math.min(s1.length(), 3), s2.length()); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
match = false;
break;
}
}
回答by James Curran
My java isn't that good, so I'll give you an answer in C#:
我的java不是那么好,所以我会用C#给你一个答案:
int len = Math.Min(s1.Length, Math.Min(s2.Length, 3));
for(int i=0; i< len; ++i)
{
if (s1[i] != s2[i])
return false;
}
return true;
Note that unlike yours and Bozho's, this does not create a new string, which would be the slowest part of your algorithm.
请注意,与您和 Bozho 的不同,这不会创建新字符串,这将是您算法中最慢的部分。
回答by Vino
Perhaps you could do this
也许你可以这样做
if (s1.length() > 3 && s2.length() > 3 && s1.indexOf (s2.substring (0, 3)) == 0)
{
// do something..
}
回答by Hershi
There is context missing here: What are you trying to scan for? What type of application? How often is it expected to run?
这里缺少上下文:您要扫描什么?什么类型的应用程序?预计多久运行一次?
These things are important because different scenarios call for different solutions:
这些事情很重要,因为不同的场景需要不同的解决方案:
- If this is a one-time scan then this is probably unneeded optimization. Even for a 20MB text file, it wouldn't take more than a couple of minutes in the worst case.
- If you have a set of inputs and for each of them you're scanning all the words in a 20MB file, it might be better to sort/index the 20MB file to make it easy to look up matches and skip the 99% of unnecessary comparisons. Also, if inputs tend to repeat themselves it might make sense to employ caching.
- 如果这是一次性扫描,那么这可能是不需要的优化。即使对于 20MB 的文本文件,在最坏的情况下也不会超过几分钟。
- 如果您有一组输入,并且为每个输入扫描 20MB 文件中的所有单词,最好对 20MB 文件进行排序/索引,以便轻松查找匹配项并跳过 99% 的不必要的比较。此外,如果输入倾向于重复自己,那么使用缓存可能是有意义的。
Other solutions might also be relevant, depending on the actual problem.
其他解决方案也可能相关,具体取决于实际问题。
But if you boil it down only to comparing the first 3 characters of two strings, I believe the code snippets given here are as good as you're going to get - they're all O(1)*, so there's no drastic optimization you can do.
但是如果你只将它归结为比较两个字符串的前 3 个字符,我相信这里给出的代码片段和你得到的一样好 - 它们都是 O(1)*,所以没有剧烈的优化你可以做。
*The only place where this might not hold true is if getting the length of the string is O(n) rather than O(1) (which is the case for the strlen function in C++), which is not the case for Java and C# string objects.
*这可能不成立的唯一地方是,如果获取字符串的长度是 O(n) 而不是 O(1)(这是 C++ 中的 strlen 函数的情况),而 Java 和C# 字符串对象。

