12 Nov, 2009, Lobotomy wrote in the 1st comment:
Votes: 0
When walking a std::string in C++ are there any particular tradeoffs (speed, etc) in using a string iterator versus using the string's array operator in a loop similar to C, or are they essentially the same?

I.e, something like this:
std::string::iterator i;
for( i = temp.begin(); i != temp.end(); i++ )

As opposed to something like this:
for( int i = 0; i < temp.length(); i++ )
12 Nov, 2009, Davion wrote in the 2nd comment:
Votes: 0
Lobotomy said:
When walking a std::string in C++ are there any particular tradeoffs (speed, etc) in using a string iterator versus using the string's array operator in a loop similar to C, or are they essentially the same?

I.e, something like this:
std::string::iterator i;
for( i = temp.begin(); i != temp.end(); i++ )

As opposed to something like this:
for( int i = 0; i < temp.length(); i++ )


The built in functions for C++ would likely accept iterators as arguments. Like, std::reverse, std::replace, std::remove, etc.

Also, it's C++! Why are you walking an entire string? :P. There's a lot of built in functions for the string class to do a lot of the stuff for you. This is probably the meat of them. You can go digging around to find a few more (I think getline is missing in that). Also, most of these use iterators as well (with exception of a few like substr)
12 Nov, 2009, Lobotomy wrote in the 3rd comment:
Votes: 0
Davion said:
Also, it's C++! Why are you walking an entire string?

1) Force of habit.
2) At the moment, I'm writing my server's code for reading and processing the input from a socket, and one part in the process requires going through the input one character at a time (to handle things like IAC commands, etc).
12 Nov, 2009, Davion wrote in the 4th comment:
Votes: 0
Lobotomy said:
Davion said:
Also, it's C++! Why are you walking an entire string?

1) Force of habit.
2) At the moment, I'm writing my server's code for reading and processing the input from a socket, and one part in the process requires going through the input one character at a time (to handle things like IAC commands, etc).


Couldn't you use things like find to locate the IAC commands for you?
12 Nov, 2009, Lobotomy wrote in the 5th comment:
Votes: 0
Davion said:
Couldn't you use things like find to locate the IAC commands for you?

Yes, but that's not the only thing going on. There are other operations which need to be performed on the input string, and for the sake of speed and efficiency I prefer that it all be done in a single pass. Unless I'm misunderstanding something, if I use things like find() to do them instead then the string will end up being traversed multiple times.
12 Nov, 2009, Tyche wrote in the 6th comment:
Votes: 0
Yes you need to process telnet protocol one character at a time in order to insure commands are constructed correctly and that the stream is processed in seqence.
BTW, the dereference operation on an iterator produces faster code than the array subscript operator on g++ 4.3.2.
The best way to check this is to dissassemble the code in gdb. YMCAWV
12 Nov, 2009, David Haley wrote in the 7th comment:
Votes: 0
One might add, though, that in this case the performance difference will be negligible unless you are processing far more data per second than I've ever seen for a MUD server. When Tyche says "faster code", he probably means that there are a few less assembly instructions. It is incredibly unlikely that your performance problems – if indeed you have any in the first place – will come from this very particular part of the code.
Seriously, people are writing MUDs in Ruby, Python, Lua, etc.; Ruby is 10-20 times slower than C, and the other languages while faster aren't hugely fast. Are you really going to worry about an assembly instruction or two?

If there are tradeoffs to worry about, I might say that the iterator version is a little more verbose.
Also, you should declare i as a size_t or better yet a std::string::size_type, not an int.
0.0/7