java - 为什么 String.equalsIgnoreCase 这么慢

标签 java string performance ignore-case

我在面试中遇到了一个问题,要写一个方法来检查相似词,而不考虑字符大小写。

我通过使用每对字符的 ASCII 值的差异来回答它。但是在家里,当我在 String.class 中进行它的实际实现时,我感到很不安 - 为什么它是这样实现的!

我试图在内置方法和我的自定义方法之间进行比较,这种方式-

public class EqualsIgnoreCase {

    public static void main(String[] args) {
        String str1 = "Srimant @$ Sahu 959s";
        String str2 = "sriMaNt @$ sAhu 959s";

        System.out.println("Avg millisecs with inbuilt () - " + averageOfTenForInbuilt(str1, str2));
        System.out.println("\nAvg millisecs with custom () - " + averageOfTenForCustom(str1, str2));
    }

    public static int averageOfTenForInbuilt(String str1, String str2) {
        int avg = 0;
        for (int itr = 0; itr < 10; itr++) {
            long start1 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                str1.equalsIgnoreCase(str2);
            }
            avg += System.currentTimeMillis() - start1;
        }
        return avg / 10;
    }

    public static int averageOfTenForCustom(String str1, String str2) {
        int avg = 0;
        for (int itr = 0; itr < 10; itr++) {
            long start2 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                isEqualsIgnoreCase(str1, str2);
            }
            avg += System.currentTimeMillis() - start2;
        }
        return avg / 10;
    }

    public static boolean isEqualsIgnoreCase(String str1, String str2) {
        int length = str1.length();
        if (str2.length() != length) {
            return false;
        }

        for (int i = 0; i < length; i++) {
            char ch1 = str1.charAt(i);
            char ch2 = str2.charAt(i);

            int val = Math.abs(ch1 - ch2);
            if (val != 0) {
                if (isInAlphabetsRange(ch1, ch2)) {
                    if (val != 32) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isInAlphabetsRange(char ch1, char ch2) {
        return (((ch1 <= 122 && ch1 >= 97) || (ch1 <= 90 && ch1 >= 65)) && ((ch2 <= 122 && ch2 >= 97) || (ch2 <= 90 && ch2 >= 65)));
    }

}

输出-

内置 () 的平均毫秒数 - 14

自定义 () 的平均毫秒数 - 5

我发现内置方法正在提高效率,因为有很多检查和方法调用。 这种实现背后有什么具体原因吗?还是我的逻辑中遗漏了什么?

任何建议,将不胜感激!

最佳答案

您的例程只处理 ASCII 字符。系统一处理所有的unicode字符。

考虑以下示例:

public class Test {

    public static void main(String[] args) {
        System.out.println((int) 'ě'); // => 283
        System.out.println((int) 'Ě'); // => 282 
    }

}

关于java - 为什么 String.equalsIgnoreCase 这么慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23884914/

相关文章:

java - 在 Java 中获取操纵杆输入

java - 如何使用 Java 11 运行 Wildfly 14?

python - Pandas 数据框 : Split mixed float-string column into separate float and string columns

sql - 性能差异大 : Using sysdate vs using pre-formatted date

java - 为什么我的 .jar 文件运行速度比 eclipse 中的程序慢?

不同CG/GLSL/HLSL功能的表现

java - Wildfly 类NotFoundException

java - XSLT 将计数器与字段名称连接起来以使其动态化

java - 人物提取挑战

string - Rust 的 `String` 和 `str` 有什么区别?