Is using string.length() in loop efficient?

前端 未结 6 1908
我在风中等你
我在风中等你 2021-02-20 09:11

For example, assuming a string s is this:

for(int x = 0; x < s.length(); x++)

better than this?:

int length         


        
6条回答
  •  太阳男子
    2021-02-20 10:16

    How efficient do you want to be?

    If you don't modify the string inside the loop, the compiler will easily see than the size doesn't change. Don't make it any more complicated than you have to!

提交回复
热议问题