<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: What is the Fastest Method to Iterate Over a String?</title>
	<atom:link href="http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Tue, 07 Feb 2012 23:33:30 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: RobC</title>
		<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/comment-page-1/#comment-44512</link>
		<dc:creator>RobC</dc:creator>
		<pubDate>Sun, 17 Oct 2010 01:30:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/#comment-44512</guid>
		<description>^ ^
Like RichardK said, not exactly apples to apples. Also, how often do you know the size of your string at compile time?</description>
		<content:encoded><![CDATA[<p>^ ^<br />
Like RichardK said, not exactly apples to apples. Also, how often do you know the size of your string at compile time?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Guy</title>
		<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/comment-page-1/#comment-23741</link>
		<dc:creator>Guy</dc:creator>
		<pubDate>Sat, 26 Sep 2009 19:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/#comment-23741</guid>
		<description>@RichardK, Thanks for your comments and optimizations.</description>
		<content:encoded><![CDATA[<p>@RichardK, Thanks for your comments and optimizations.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RichardK</title>
		<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/comment-page-1/#comment-23737</link>
		<dc:creator>RichardK</dc:creator>
		<pubDate>Sat, 26 Sep 2009 16:49:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/#comment-23737</guid>
		<description>Your iterator code is flawed, move the .begin() and .end() code outside the for loop, otherwise the functions (which are not inline) will be run EVERY loop iteration!

Also you need to change the iter &#039;tmp&#039; from tmp++ to ++tmp. The former is postfix, meaning the compiler will create a temporary object to store the contents of &#039;tmp&#039;, then increment it. Using prefix notation will elliminate the temporary.

Heres a better example:
std::string::iterator tmp(hugestring.begin());
std::string::iterator const tmp_end(hugestring.end());
for(; tmp != tmp_end; ++tmp)
{
      //-&gt;Code Here
}

Note how I use the constructor call for storing the begin() and end() values. If not done this way, the compiler will create a temporary for both those lines!</description>
		<content:encoded><![CDATA[<p>Your iterator code is flawed, move the .begin() and .end() code outside the for loop, otherwise the functions (which are not inline) will be run EVERY loop iteration!</p>
<p>Also you need to change the iter &#8216;tmp&#8217; from tmp++ to ++tmp. The former is postfix, meaning the compiler will create a temporary object to store the contents of &#8216;tmp&#8217;, then increment it. Using prefix notation will elliminate the temporary.</p>
<p>Heres a better example:<br />
std::string::iterator tmp(hugestring.begin());<br />
std::string::iterator const tmp_end(hugestring.end());<br />
for(; tmp != tmp_end; ++tmp)<br />
{<br />
      //-&gt;Code Here<br />
}</p>
<p>Note how I use the constructor call for storing the begin() and end() values. If not done this way, the compiler will create a temporary for both those lines!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David Sankel</title>
		<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/comment-page-1/#comment-16506</link>
		<dc:creator>David Sankel</dc:creator>
		<pubDate>Fri, 06 Mar 2009 18:32:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/#comment-16506</guid>
		<description>One method that was left out is using the .data member of std::string. This accesses the underlying data where the c_str() does not and cannot (std::string must be able to have embedded &#039;&#039;&#039;s).

That method ought to be the fastest (hopefully tied with iterators):

char * cur = s.data();
char * end = cur + s.size();
for( ; cur!=end; ++cur )
{
  f(*cur);
}</description>
		<content:encoded><![CDATA[<p>One method that was left out is using the .data member of std::string. This accesses the underlying data where the c_str() does not and cannot (std::string must be able to have embedded &#8221;&#8217;s).</p>
<p>That method ought to be the fastest (hopefully tied with iterators):</p>
<p>char * cur = s.data();<br />
char * end = cur + s.size();<br />
for( ; cur!=end; ++cur )<br />
{<br />
  f(*cur);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jancsi F.</title>
		<link>http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/comment-page-1/#comment-1755</link>
		<dc:creator>Jancsi F.</dc:creator>
		<pubDate>Fri, 29 Feb 2008 15:10:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/#comment-1755</guid>
		<description>The results are right, without optimization.
However if I turn on optimizations, with -O2 I will get the following result:

jancsi@dew:~/$ ./benchmark
iteration using indexes
real    0m0.325s
user    0m0.280s
sys     0m0.040s

iteration using indexes over C string
real    0m0.327s
user    0m0.260s
sys     0m0.060s

iteration using string::at()
real    0m0.425s
user    0m0.348s
sys     0m0.068s

iteration using pointers over C string
real    0m0.703s
user    0m0.612s
sys     0m0.072s

iteration using iterators
real    0m0.355s
user    0m0.304s
sys     0m0.048s

J.</description>
		<content:encoded><![CDATA[<p>The results are right, without optimization.<br />
However if I turn on optimizations, with -O2 I will get the following result:</p>
<p>jancsi@dew:~/$ ./benchmark<br />
iteration using indexes<br />
real    0m0.325s<br />
user    0m0.280s<br />
sys     0m0.040s</p>
<p>iteration using indexes over C string<br />
real    0m0.327s<br />
user    0m0.260s<br />
sys     0m0.060s</p>
<p>iteration using string::at()<br />
real    0m0.425s<br />
user    0m0.348s<br />
sys     0m0.068s</p>
<p>iteration using pointers over C string<br />
real    0m0.703s<br />
user    0m0.612s<br />
sys     0m0.072s</p>
<p>iteration using iterators<br />
real    0m0.355s<br />
user    0m0.304s<br />
sys     0m0.048s</p>
<p>J.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.251 seconds -->

