简体   繁体   English

Java中的字符串比较性能

[英]String comparison performance in java

I have two Strings of about 15 character in java and I want to know how many flops or cycles does it take to compare these two. 我在Java中有两个大约15个字符的字符串,我想知道比较这两个字符串需要多少个触发器或周期。 how can I acquire such information. 我如何获取此类信息。

example : 例如:

"Hello World".compareTo("Another Hello World")

I don't know how to answer that in terms of flops or cycles, but in terms of what's actually being done when you call compareTo , the actual processing depends on the number of identical characters the two Strings share in their beginning, since compareTo will only test as many characters as required to find the first non-equal character. 我不知道如何用触发器或循环来回答这个问题,但是根据您调用compareTo时实际完成的工作,实际的处理取决于两个字符串在开始时共享的相同字符数,因为compareTo将仅根据需要测试尽可能多的字符才能找到第一个不相等的字符。

In your example, only the first character of the two Strings will be examined (since 'H' != 'A'). 在您的示例中,将仅检查两个字符串中的第一个字符(因为'H'!='A')。 In the worst case, if the two Strings are equal, all characters of both Strings will be compared. 在最坏的情况下,如果两个字符串相等,则将比较两个字符串的所有字符。

I want to know how many flops or cycles does it take 我想知道需要多少次翻牌或循环

I assume you are interested in CPU cycles/timings. 我假设您对CPU周期/时序感兴趣。

To measure CPU time per thread under windows you can use GetThreadTimes WinAPI function, you can wrap its call using JNI. 要测量Windows下每个线程的CPU时间,可以使用GetThreadTimes WinAPI函数,可以使用JNI封装其调用。

To get cycles you will want to use QueryThreadCycleTime function. 要获取周期,您将需要使用QueryThreadCycleTime函数。

Both will return times/cycles per thread, so even if some other JVM thread will take CPU during measurement, it will not be included in results. 两者都将返回每个线程的时间/周期,因此即使某些其他JVM线程在测量期间占用了CPU,也不会包含在结果中。

EDIT: 编辑:

It looks like per thread timing is available since 1.5: 从1.5开始,每个线程的计时似乎可用:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html

java.lang.String class source is available. java.lang.String类源可用。 For example in JRE 1.6.0_38 it is implemented in following way: 例如,在JRE 1.6.0_38中,它是通过以下方式实现的:

public int compareTo(String anotherString) {
    int len1 = count;
    int len2 = anotherString.count;
    int n = Math.min(len1, len2);
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = offset;
    int j = anotherString.offset;

    if (i == j) {
        int k = i;
        int lim = n + i;
        while (k < lim) {
        char c1 = v1[k];
        char c2 = v2[k];
        if (c1 != c2) {
            return c1 - c2;
        }
        k++;
        }
    } else {
        while (n-- != 0) {
        char c1 = v1[i++];
        char c2 = v2[j++];
        if (c1 != c2) {
            return c1 - c2;
        }
        }
    }
    return len1 - len2;
}

It is O(n) where n is the number of matching chars in both strings. 它是O(n),其中n是两个字符串中匹配的字符数。 In the worst case where one string is prefix of the other n will be the length of the shorter string. 在最坏的情况下,一个字符串是另一个n前缀将是较短字符串的长度。 For example "test".compareTo("testa"). 例如“ test” .compareTo(“ testa”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM