在循环中使用 string.length() 是否有效?
例如,假设 string s
是这样的:
for(int x = 0; x < s.length(); x++)
比这个更好?:
int length = s.length();
for(int x = 0; x < length; x++)
For example, assuming a string s
is this:
for(int x = 0; x < s.length(); x++)
better than this?:
int length = s.length();
for(int x = 0; x < length; x++)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一般来说,如果结果在迭代期间没有改变,您应该避免在循环的条件部分中调用函数。
因此,规范形式是:
这里注意 3 件事:
!=
表示,而不是<
(我还更改了类型,因为负长度是无意义的,并且字符串接口是根据
std::string::size_type
定义的,通常是std ::size_t
大多数实现)。虽然......我承认它对性能的影响不如对可读性的影响:
x
和length
范围都尽可能严格简而言之:使用适合工作的最佳工具就在眼前:)
In general, you should avoid function calls in the condition part of a loop, if the result does not change during the iteration.
The canonical form is therefore:
Note 3 things here:
!=
rather than<
(I also changed the type because is a negative length is non-sense and the string interface is defined in term of
std::string::size_type
, which is normallystd::size_t
on most implementations).Though... I admit that it's not as much for performance than for readability:
x
andlength
scope is as tight as necessaryIn short: use the best tool for the job at hand :)
这取决于编译器的内联和优化能力。一般来说,第二个变体很可能会更快(更好:它会更快或与第一个片段一样快,但几乎不会慢)。
然而,在大多数情况下,这并不重要,因此人们倾向于选择第一个变体,因为它的简短性。
It depends on the inlining and optimization abilities of the compiler. Generally, the second variant will most likely be faster (better: it will be either faster or as fast as the first snippet, but almost never slower).
However, in most cases it doesn't matter, so people tend to prefer the first variant for its shortness.
这取决于您的 C++ 实现/库,唯一确定的方法是对其进行基准测试。然而,可以肯定的是,第二个版本永远不会比第一个版本慢,因此,如果您不在循环内修改字符串,那么这是一个明智的优化。
It depends on your C++ implementation / library, the only way to be sure is to benchmark it. However, it's effectively certain that the second version will never be slower than the first, so if you don't modify the string within the loop it's a sensible optimisation to make.
您希望效率如何?
如果不在循环内修改字符串,编译器很容易发现大小没有改变。不要让它变得比你需要的更复杂!
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!
s.length() 是内联并返回成员变量吗?那么不,否则取消引用并将内容放入堆栈的成本,您知道每次迭代将产生的函数调用的所有开销。
Is s.length() inline and returns a member variable? then no, otherwise cost of dereferencing and putting stuff in stack, you know all the overheads of function call you will incur for each iteration.
尽管我不一定鼓励您这样做,但似乎不断调用
.length()
比将其存储在int
中更快,令人惊讶的是(至少在我的计算机,请记住,我使用的是配备 i5 第四代的 MSI 游戏笔记本电脑,但它应该不会真正影响哪种方式更快)。常量调用的测试代码:
根据 Code::Blocks,平均运行时间为 385 毫秒
这是将长度存储在变量中的代码:
平均运行时间约为 420 毫秒。
我知道这个问题已经有一个公认的答案,但还没有任何经过实际测试的答案,所以我决定投入 2 美分。我和你有同样的问题,但我在这里没有找到任何有用的答案,所以我做了自己的实验。
Although I am not necessarily encouraging you to do so, it appears it is faster to constantly call
.length()
than to store it in anint
, surprisingly (atleast on my computer, keeping in mind that I'm using an MSI gaming laptop with i5 4th gen, but it shouldn't really affect which way is faster).Test code for constant call:
On average, this ran for 385ms according to Code::Blocks
And here's the code that stores the length in a variable:
And this averaged around 420ms.
I know this question already has an accepted answer, but there haven't been any practically tested answers, so I decided to throw my 2 cents in. I had the same question as you, but I didn't find any helpful answers here, so I ran my own experiment.