When traversing Java strings, compare the efficiency of the toCharArray() method and the charAt() method

Questions raised:

Recently, when I was brushing the questions in Likou, I found that when traversing strings, the big guys usually use the toCharArray () method, but rarely use the harAt () method, so I am curious which of the two methods will be more efficient What about a little? I ran some test cases, but the result is that toCharArray () is efficient from time to time, and charAt () is efficient from time to time. I think it may be that the string length of my test case is not enough, but I add the length of the string to my When thought long enough, I found that charAt() was significantly more efficient than toCharArray().

public class Test {
    public static void main(String[] args) {
        String s = "这里面的字符串内容就省略了,知道它足够长就可以";
 
        long st1 = System.currentTimeMillis();
        char[] ch = s.toCharArray();
        for (char c : ch) {
            System.out.print(c);
        }
        long et1 = System.currentTimeMillis();
        long rt1 = et1 - st1;
        System.out.println();
        System.out.println(rt1);
 
        System.out.println("-------------------------------------------------");
 
        long st2 = System.currentTimeMillis();
        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.charAt(i));
        }
        long et2 = System.currentTimeMillis();
        long rt2 = et2 - st2;
        System.out.println();
        System.out.println(rt2);
    }
}

Note: I have tested many times, and the result is that the toCharAray () method takes about 30ms, and the charAt () method takes about 15ms.

I'm curious, since the charAt () method is efficient, why do so many people use the toCharArray () method? Is there any downside to the charAt() method?

Exploration process:

So I asked Mr. Man from the dark horse, and Mr. Man gave me the answer: "I don't think there are any disadvantages. The efficiency of the java api level is not too important in my opinion." In fact, this answer can't completely answer my doubts. Since there are no disadvantages and the efficiency is still high, why is the charAt () method rarely used in this platform that values ​​​​efficiency?

Then I searched for this question on the Internet, but the answers were different, and I failed to convince me very well, but I found that many people think that the toCharArray () method is efficient, but this is contrary to my test results.

Then I went to CSDN to post a question and answer, but unfortunately there was only one answer, that is: obviously the toCharArray () method is efficient because it directly accesses the array, but toCharArray () itself requires time and additional memory. How to say this answer is not very convincing to me.

It happened that when I was reviewing String related knowledge, I saw the video of Dark Horse saying: using the toCharArray () method will be more efficient, because some other methods will be called when using the charAt () method to traverse, and these methods will take time to push and pop. It is true that this statement makes sense, but why is it inconsistent with my test results?

Should be the correct solution:

Finally, I thought of chatGPT, and it turns out that chatGPT is really powerful, at least its answer convinced me, and its answer is as follows:

"In Java, both string  charAt() methods and  toCharArray() methods can be used to iterate over strings, but their efficiency depends on the specific use case.

If you only need to traverse a single character in the string,  charAt() the method is more convenient to use, because it only needs to access one character in the string, so in this case, charAt() the method is more efficient.

However, if you need to iterate over the entire string and perform some manipulation, such as converting the string to a character array for some processing, it  toCharArray() may be more efficient to use the method. This is because  toCharArray() the method can convert the entire string into a character array at one time, allowing us to perform a series of operations on the array, such as modification, sorting, and so on. However, using  charAt() the method requires repeated access to the string, which is less efficient.

In general, if you only need to traverse a single character,  charAt() the method of use is more efficient, and if you need to operate on the entire string,  toCharArray() the method of use may be more efficient. But in practice, for small strings, the difference between the two methods is not obvious, so which method to choose depends on the specific situation and personal preference. "

I have to say that its answer is very pertinent, and it also conforms to my test case. As for the method of using Lituo  toCharArray() , it is true that they do not simply traverse the string, and there are other operations. So in my mind this answer should be correct.

Summarize:

Regarding the comparison of the efficiency of the toCharArray () method and the charAt () method when traversing Java strings, the views I support are:

It is more efficient to use if you only need to iterate over a single character  , and  it may be more efficient charAt() to use if you need to operate on the entire string  .toCharArray()

But for small strings, the difference between the two methods is not obvious, maybe as Mr. Man said: the efficiency issue at the java api level is not too important.

 

 

Guess you like

Origin blog.csdn.net/m0_56680022/article/details/129784208