473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Which of these for loops will iterate faster?

4 New Member
Which one of these for loops will iterate through each character in a std::string faster?
Expand|Select|Wrap|Line Numbers
  1. for (unsigned i = 0u; i < str.length(); ++i)
Expand|Select|Wrap|Line Numbers
  1. for (unsigned i = 0u, end = str.length(); i < end; ++i)
Nov 27 '11 #1
6 1725
whodgson
542 Contributor
#1 by 0.00006 msecs.
Nov 27 '11 #2
johny10151981
1,059 Top Contributor
Well I want to know why not #2?

My logic says second loop will work faster
Nov 27 '11 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The second loop is faster since it does not call str.length() on each cycle of the loop.

OTOH calls like string::length() are optimized inline or maybe are coded inline. So now you can't tell.

You will to look at the assembly. And then that pertains only to that one compiler.
Nov 27 '11 #4
johny10151981
1,059 Top Contributor
Even If it is designed in such a way so that we dont have to calculate the length again and again but I will have to call a function, which will lead a slower process then the loop #2
Nov 27 '11 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can't tell from the code if there is an actual function call. string::length may be an inline function. If so, there is no call.
Nov 28 '11 #6
donbock
2,426 Recognized Expert Top Contributor
As stated earlier, it is difficult to say with certainty which version of a code snippet will run faster simply by examining the source code. Code that runs faster on one compiler might run slower on another. The only way to be sure is to measure the execution time (which is what whodgson did in the first reply). However, you have to remember that those measurements only apply to that specific compiler and target computer.

For the record, a simple-minded compiler would probably generate slower code for option #2 due to (a) more code inside the loop; (b) the code inside the loop includes a function call to the str::length method; and (c) the str::length method might traverse the string to compute the length. weaknessforcats has already pointed out that the compiler might replace the function call with inline code, cancelling out (b). The str object might contain an explicit length field, cancelling out (c). Notice that the snippet invokes the method for a built-in type -- this means that the compiler could conceivably have enough information to know it is safe to pull the method-call out of the loop, cancelling out (a).

By the way, aggressive optimization can be a great help for embedded software where processor-speed is often a limiting factor; but it can also pose great difficulties if it rearranges instructions that access memory-mapped registers. The typical way to protect accesses to memory-mapped registers is to use the volatile keyword, but that may not always work: Volatiles are miscompiled, and what to do about it.
Nov 28 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul | last post by:
I am creating a Program for college, in which the Program will read a Folder and create a HTML page from the pictures that are storrd in that folder. . What would be the best way to do...
0
by: Paul | last post by:
I am creating a Program for college, in which the Program will read a Folder and create a HTML page from the pictures that are storrd in that folder. .. What would be the best way to do it in VB...
2
by: Abubakar | last post by:
Hi, NOTE: in the following code Rectangle is a struct inside the System.Drawing namespace. I'v a following code: Rectangle GetFillRectangle(ref Rectangle rect) { Rectangle newrect;...
2
by: prileep | last post by:
I have two tables Users and UserLogin. Here i will use two methods of table design. and which query will return me the result more fast. The table size will be large that it may contain records in...
7
by: A.E lover | last post by:
Dear all, In C, I am wondering what codes will run faster: assuming, I have two arrays: dounle a, double code 1:
6
by: process | last post by:
qsort can handle bigger lists it seems, making less recursive calls before finishing(quicksort blows the stack when sorting range(100,-1000,-1). qsort does more work though right? is there a way...
1
by: mahendra dubey | last post by:
Hi There I am new to c#. I am using SqlDataReader to read database records(using c#).I have found lots of way to do it,but for performance i have found that ordinal reading is fast. Now...
2
by: unconquerable | last post by:
if of the following will execute faster if ( flag == 0) { } or if ( 0 == flag) {
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.