<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guy Rutenberg &#187; C/C++</title>
	<atom:link href="http://www.guyrutenberg.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.guyrutenberg.com</link>
	<description>Keeping track of what I do</description>
	<lastBuildDate>Wed, 16 Jun 2010 19:53:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple AI Engine for the Oware Game</title>
		<link>http://www.guyrutenberg.com/2009/08/19/simple-ai-engine-for-the-oware-game/</link>
		<comments>http://www.guyrutenberg.com/2009/08/19/simple-ai-engine-for-the-oware-game/#comments</comments>
		<pubDate>Wed, 19 Aug 2009 10:54:28 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[oware]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=568</guid>
		<description><![CDATA[Sometime ago I worked with a friend on building an Oware game. I was supposed to build the AI engine, and he was supposed to build the user interface to it. Unfortunately, while AI engine interface I designed and a simple alpha-beta pruning engine was implemented, the project was never completed.

Instead of letting the project [...]]]></description>
			<content:encoded><![CDATA[<p>Sometime ago I worked with a friend on building an <a href="http://en.wikipedia.org/wiki/Oware">Oware</a> <a href="http://www.wikimanqala.org/wiki/Oware">game</a>. I was supposed to build the AI engine, and he was supposed to build the user interface to it. Unfortunately, while AI engine interface I designed and a simple alpha-beta pruning engine was implemented, the project was never completed.</p>
<div id="attachment_569" class="wp-caption alignnone" style="width: 672px"><img src="http://www.guyrutenberg.com/wp-content/uploads/2009/08/oware-screenshot.png" alt="Screenshot of game session" title="oware-screenshot" width="662" height="412" class="size-full wp-image-569" /><p class="wp-caption-text">Screenshot of game session</p></div>
<p><span id="more-568"></span><br />
Instead of letting the project just sit and wait until it will be redeemed and completed I&#8217;ve decided to release my code. It consists of a simple terminal user-interface and an AI opponent. Compiling is straight forward and doesn&#8217;t require any special libraries. You can obtain the code from here: <a href="/wp-content/uploads/2009/09/oware-0.1.tar.bz2">oware-0.1.tar.bz2</a> [15.1 KB].</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">tar xvf ./oware-0.1.tar.bz2
cd oware
g++ main.cpp ai/basic_ai_engine.cpp -o oware</pre></div></div>

<p>Running the program is pretty simple, just type <code>./oware</code>. You will be prompted to select the level of the AI opponent. The higher the number the slower and better the opponent will play. I suggest starting pretty low, and climbing up untill you find it challenging. The houses (holes) are numbered from 0 to 11 starting with the bottom left hole (yours) and going counter-clockwise. </p>
<p>The game is licensed under the GPLv2 or any latter version, so fill free to hack and improve the game if you have time.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/08/19/simple-ai-engine-for-the-oware-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Histogram Widget for wxWidgets</title>
		<link>http://www.guyrutenberg.com/2009/08/13/simple-histogram-widget-for-wxwidgets/</link>
		<comments>http://www.guyrutenberg.com/2009/08/13/simple-histogram-widget-for-wxwidgets/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 20:25:28 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[wxWidgets]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=444</guid>
		<description><![CDATA[When working on Open Yahtzee 1.10 (or what ever I&#8217;ll call the version after 1.9), I&#8217;ve written a simple histogram widget to be part of the new statistics dialog. I should emphasize the simple part, this widget was mean to display a simple histogram without requiring any special bloated ploting libraries. It doesn&#8217;t support all [...]]]></description>
			<content:encoded><![CDATA[<p>When working on <a href="http://www.openyahtzee.org/">Open Yahtzee</a> 1.10 (or what ever I&#8217;ll call the version after 1.9), I&#8217;ve written a simple histogram widget to be part of the new statistics dialog. I should emphasize the <em>simple</em> part, this widget was mean to display a simple histogram without requiring any special bloated ploting libraries. It doesn&#8217;t support all the fancy stuff, just plain histogram. </p>
<p>I&#8217;ve figured that a simple pie plot would better serve Open Yahtzee&#8217;s needs, so unfortunately this code will not be released as part of the program. While the code is not perfect, it&#8217;s functional and serves a good example of a custom widget. So I&#8217;ve felt pity letting it fall into oblivion in Open Yahtzee&#8217;s SVN repository, and I&#8217;ve thought it might come handy to someone else (or at least for me) if it will be easily accessible.</p>
<p><img src="http://www.guyrutenberg.com/wp-content/uploads/2009/08/simple_histogram.png" alt="simple_histogram" title="simple_histogram" width="320" height="254" class="alignnone size-full wp-image-450" /><br />
<span id="more-444"></span><br />
The widget is made up of two files: <code>simple_histogram.h</code> and <code>simple_histogram.cpp</code>. The code uses <code>stl::vector</code> to pass the plotting data and depenpends on <code>BOOST_FOREACH</code> (but it can easily be removed).</p>
<p><code>simple_histogram.h</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/***************************************************************************
 *   Copyright (C) 2009 by Guy Rutenberg   *
 *   http://www.guyrutenberg.com/contact-me   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/</span>
&nbsp;
<span style="color: #339900;">#include &lt;wx/wx.h&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
&nbsp;
<span style="color: #339900;">#ifndef SIMPLE_HISTOGRAM_INC</span>
<span style="color: #339900;">#define SIMPLE_HISTOGRAM_INC</span>
&nbsp;
<span style="color: #0000ff;">class</span> SimpleHistogram <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> wxPanel <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	SimpleHistogram <span style="color: #008000;">&#40;</span>wxWindow<span style="color: #000040;">*</span> parent, wxWindowID id,
		 <span style="color: #0000ff;">const</span> wxPoint<span style="color: #000040;">&amp;</span> pos <span style="color: #000080;">=</span> wxDefaultPosition, <span style="color: #0000ff;">const</span> wxSize<span style="color: #000040;">&amp;</span> size <span style="color: #000080;">=</span> wxDefaultSize,
		 <span style="color: #0000ff;">long</span> style <span style="color: #000080;">=</span> wxNO_BORDER, <span style="color: #0000ff;">const</span> wxString<span style="color: #000040;">&amp;</span> name <span style="color: #000080;">=</span> wxPanelNameStr<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> SetData<span style="color: #008000;">&#40;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> d<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> OnPaint<span style="color: #008000;">&#40;</span>wxPaintEvent<span style="color: #000040;">&amp;</span> event<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">void</span> OnResize<span style="color: #008000;">&#40;</span>wxSizeEvent<span style="color: #000040;">&amp;</span> event<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> data<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> data_total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #339900;">#endif</span></pre></div></div>

<p><code>simple_histogram.cpp</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #ff0000; font-style: italic;">/***************************************************************************
 *   Copyright (C) 2009 by Guy Rutenberg   *
 *   http://www.guyrutenberg.com/contact-me   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/</span>
&nbsp;
<span style="color: #339900;">#include &quot;simple_histogram.h&quot;</span>
<span style="color: #339900;">#include &lt;boost/foreach.hpp&gt;</span>
<span style="color: #339900;">#include &lt;memory&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">SimpleHistogram</span><span style="color: #008000;">&#40;</span>wxWindow<span style="color: #000040;">*</span> parent, wxWindowID id,
				<span style="color: #0000ff;">const</span> wxPoint<span style="color: #000040;">&amp;</span> pos, <span style="color: #0000ff;">const</span> wxSize<span style="color: #000040;">&amp;</span> size,
				<span style="color: #0000ff;">long</span> style, <span style="color: #0000ff;">const</span> wxString<span style="color: #000040;">&amp;</span> name<span style="color: #008000;">&#41;</span>
				<span style="color: #008080;">:</span> wxPanel<span style="color: #008000;">&#40;</span>parent, id, pos, size, style, name<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Connect<span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, wxEVT_PAINT, wxPaintEventHandler<span style="color: #008000;">&#40;</span>SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">OnPaint</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Connect<span style="color: #008000;">&#40;</span>this<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>GetId<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, wxEVT_SIZE, wxSizeEventHandler<span style="color: #008000;">&#40;</span>SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">OnResize</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">OnPaint</span><span style="color: #008000;">&#40;</span>wxPaintEvent<span style="color: #000040;">&amp;</span> event<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	wxPaintDC pdc<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	auto_ptr<span style="color: #000080;">&lt;</span>wxGraphicsContext<span style="color: #000080;">&gt;</span> dc<span style="color: #008000;">&#40;</span>wxGraphicsContext<span style="color: #008080;">::</span><span style="color: #007788;">Create</span><span style="color: #008000;">&#40;</span>pdc<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	dc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>SetBrush<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>wxBLUE_BRUSH<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> width, height<span style="color: #008080;">;</span>
	GetClientSize<span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>width, <span style="color: #000040;">&amp;</span>height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">double</span> column_width <span style="color: #000080;">=</span> <span style="color: #0000ff;">static_cast</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span>width<span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> data.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">double</span> item_x, item_y, item_ratio, item_height<span style="color: #008080;">;</span>
	BOOST_FOREACH<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> d, data<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		item_x <span style="color: #000080;">=</span> i <span style="color: #000040;">*</span> column_width<span style="color: #008080;">;</span>
		item_ratio <span style="color: #000080;">=</span> data_total <span style="color: #008080;">?</span> d<span style="color: #000040;">/</span>data_total <span style="color: #008080;">:</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
		item_height <span style="color: #000080;">=</span> item_ratio <span style="color: #000040;">*</span> height<span style="color: #008080;">;</span>
		item_y <span style="color: #000080;">=</span> height<span style="color: #000040;">-</span>item_height<span style="color: #008080;">;</span>
		dc<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>DrawRectangle<span style="color: #008000;">&#40;</span>item_x, item_y, column_width, item_height<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		i<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">OnResize</span><span style="color: #008000;">&#40;</span>wxSizeEvent<span style="color: #000040;">&amp;</span> event<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Refresh<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	event.<span style="color: #007788;">Skip</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> SimpleHistogram<span style="color: #008080;">::</span><span style="color: #007788;">SetData</span><span style="color: #008000;">&#40;</span>vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #000080;">&gt;</span> d<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	data.<span style="color: #007788;">assign</span><span style="color: #008000;">&#40;</span>d.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, d.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	data_total <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
	BOOST_FOREACH<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> tmp, data<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		data_total <span style="color: #000040;">+</span><span style="color: #000080;">=</span> tmp<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><strong>UPDATE 2009-08-17:</strong> Removed commented out function &#8211; <code>DoGetBestSize()</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/08/13/simple-histogram-widget-for-wxwidgets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Convert int to string (As Well As Almost Anything)</title>
		<link>http://www.guyrutenberg.com/2009/05/04/convert-int-to-string-as-well-as-almost-anything/</link>
		<comments>http://www.guyrutenberg.com/2009/05/04/convert-int-to-string-as-well-as-almost-anything/#comments</comments>
		<pubDate>Mon, 04 May 2009 06:15:57 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=328</guid>
		<description><![CDATA[This is a little code snippet from Open Yahtzee&#8217;s code base that converts all the built-in types and many custom classes (ones that override the ]]></description>
			<content:encoded><![CDATA[<p>This is a little code snippet from <a href="http://www.openyahtzee.org">Open Yahtzee</a>&#8217;s code base that converts all the built-in types and many custom classes (ones that override the << operator) to string.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">inline</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> stringify<span style="color: #008000;">&#40;</span>T x<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	std<span style="color: #008080;">::</span><span style="color: #007788;">ostringstream</span> o<span style="color: #008080;">;</span>
	o <span style="color: #000080;">&lt;&lt;</span> x<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> o.<span style="color: #007788;">str</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I first wrote it to convert <code>int</code>s to <code>string</code>, but later I templatized it so it would work with other types as well. It&#8217;s a clean elegant snippet that I thought other might find useful too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2009/05/04/convert-int-to-string-as-well-as-almost-anything/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expanding Macros into String Constants in C</title>
		<link>http://www.guyrutenberg.com/2008/12/20/expanding-macros-into-string-constants-in-c/</link>
		<comments>http://www.guyrutenberg.com/2008/12/20/expanding-macros-into-string-constants-in-c/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 18:36:14 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=190</guid>
		<description><![CDATA[Today I came across an annoying problem, how do I expand a C macro into a string?
One of C&#8217;s preprocessor operators is the # which surrounds the token that follows it in the replacement text with double quotes (&#8220;). So, at first the solution sounds pretty simple, just define

#define STR(tok) #tok

and things will work. However, [...]]]></description>
			<content:encoded><![CDATA[<p>Today I came across an annoying problem, how do I expand a C macro into a string?</p>
<p>One of C&#8217;s preprocessor operators is the <code>#</code> which surrounds the token that follows it in the replacement text with double quotes (&#8220;). So, at first the solution sounds pretty simple, just define</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define STR(tok) #tok</span></pre></div></div>

<p>and things will work. However, there is one caveat: it will not work if passed another macro. For example,</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define BUF_LEN 100</span>
<span style="color: #339933;">#define STR(tok) #tok</span>
&nbsp;
STR<span style="color: #009900;">&#40;</span>BUF_LEN<span style="color: #009900;">&#41;</span></pre></div></div>

<p>will produce after going through the preprocessor</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;BUF_LEN&quot;</pre></div></div>

<p>instead of <code>"100"</code>, which is undesired. This behavior is due to the C standard noting that no macro expansions should happen to token preceded by <code>#</code>.</p>
<p>However, after reconsidering the source of the problem, I&#8217;ve found the following workaround: define another macro which will expand the argument and only then call the macro which does the quoting.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define STR_EXPAND(tok) #tok</span>
<span style="color: #339933;">#define STR(tok) STR_EXPAND(tok)</span>
&nbsp;
<span style="color: #339933;">#define BUF_LEN 100</span>
&nbsp;
STR<span style="color: #009900;">&#40;</span>BUF_LEN<span style="color: #009900;">&#41;</span></pre></div></div>

<p>will produce</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;100&quot;</pre></div></div>

<p>as desired.</p>
<p>Explanation: The <code>STR</code> macro calls the <code>STR_EXPAND</code> macro with its argument. Unlike in the first example, this time the parameter is checked for macro expansions and evaluated by the preprocessor <strong>before</strong> being passed to <code>STR_EXPAND</code> which quotes it, thus giving the desired behavior.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/12/20/expanding-macros-into-string-constants-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>spass 1.1 &#8211; Secure Password Generator</title>
		<link>http://www.guyrutenberg.com/2008/05/04/spass-11-secure-password-generator/</link>
		<comments>http://www.guyrutenberg.com/2008/05/04/spass-11-secure-password-generator/#comments</comments>
		<pubDate>Sun, 04 May 2008 20:14:59 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[spass]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=55</guid>
		<description><![CDATA[This is a new version of my /dev/random based secure password generator &#8211; spass. The new version doesn&#8217;t have new features, it&#8217;s mainly a bug-fix release. The package now uses autotools, which means it has the standard configure script and makefile. I also fixed some typos in the help message. Overall the new version doesn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>This is a new version of my <code>/dev/random</code> based <a href="http://www.guyrutenberg.com/2007/10/20/spass-a-secure-password-generator-utility/">secure password generator &#8211; <code>spass</code></a>. The new version doesn&#8217;t have new features, it&#8217;s mainly a bug-fix release. The package now uses autotools, which means it has the standard <code>configure</code> script and makefile. I also fixed some typos in the help message. Overall the new version doesn&#8217;t offer anything new compared to the old one, except for easier installation.<br />
<span id="more-55"></span><br />
You can download the new release from <a href="/wp-content/uploads/2008/05/spass-1.1.tar.bz2">spass-1.1.tar.bz2</a>. Installation procedure is a tad different. All you need to do now (after you untar the tarball) is <code>./configure &#038;&#038; make</code> switch to root and execute <code>make install</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/05/04/spass-11-secure-password-generator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>mctext 0.2 &#8211; A Markov Chain Text Generator</title>
		<link>http://www.guyrutenberg.com/2008/04/30/mctext-02-a-markov-chain-text-generator/</link>
		<comments>http://www.guyrutenberg.com/2008/04/30/mctext-02-a-markov-chain-text-generator/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 17:41:57 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[mctext]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/?p=42</guid>
		<description><![CDATA[This is the second release of my Markov Chain text generator &#8211; mctext. This text generator takes existing sample text, and generates a new text using Markov Chains.
The main new thing in the version in that it allows the users to specify via the command line how many words should be considered when generating the [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second release of my Markov Chain text generator &#8211; <a href="/2008/01/29/mctext-using-markov-chains-to-generate-text/"><code>mctext</code></a>. This text generator takes existing sample text, and generates a new text using Markov Chains.</p>
<p>The main new thing in the version in that it allows the users to specify via the command line how many words should be considered when generating the next one. The bigger the step number the closer the generated text is to the original one. The value used in mctext-0.1 was 2, and this is also the default in this one. The number of steps can be set using the <code>--steps</code> command line switch.<br />
<span id="more-42"></span><br />
In this version also couple of bugs were fixed (mostly segmentation faults). Another change the regular user will not notice, as it happened under the hood. I&#8217;ve redesign the program, and gave better software architecture that hopefully will allow one to extend its abilities and generalize its output generation.</p>
<p>I planned to add more features in this release but due to lack of time, I&#8217;ve decided to release as-is. The design rewrite is part of a future plan to allow <code>mctext</code> to operate on music pieces (probably MIDI). This should add a new dimension to the program and allow it to utilize the new generalized structure to generate new music pieces based on sample ones. This future project can have much nicer results that just plain text generation. I really hope I&#8217;ll find the time to complete it.</p>
<p>The new package can be downloaded found here &#8211; <a href="/wp-content/uploads/2008/04/mctext-0.2.tar.bz2">mctext-0.2.tar.bz2</a>. Compilation and installation remained the same as in the previous version. The only dependency is the Boost C++ library.</p>
<p>This is a free software, so please fill free to modify or hack it any way you like. It would be great if you can send a comment when you do so. Also, if you got an interesting idea how this program can be used or modified, please comment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/04/30/mctext-02-a-markov-chain-text-generator/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Setting Up OmniComplete (Autocompletion) for wxWidgets in Vim</title>
		<link>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/</link>
		<comments>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 19:27:11 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[wxWidgets]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/</guid>
		<description><![CDATA[I use Vim as my main IDE for C/C++ related development (as well as for almost all other development). If you use (or thinking about using) vim as as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by the OmniComplete, which is available since Vim 7.0. Just having [...]]]></description>
			<content:encoded><![CDATA[<p>I use Vim as my main IDE for C/C++ related development (as well as for almost all other development). If you use (or thinking about using) vim as as an IDE, you better get some good autocompletion functionality. This kind of autocompletion is provided by the OmniComplete, which is available since Vim 7.0. Just having  the OmniComplete is a nice thing, but it&#8217;s much more helpful if configured properly to work with the libraries you use, such as wxWidgets. In this post I will show you how to get working the OmniComplete for wxWidgets, however, the procedure I will show can be easily adapted to almost all libraries.<br />
<span id="more-41"></span></p>
<p>The first step is to install <code>ctags</code> (if you haven&#8217;t got it already installed), this provides the tags file, on which the autocompletion is based. <code>ctags</code> is available directly from the package manager of all popular distro&#8217;s.</p>
<p>The next step is to install the <a href="http://www.vim.org/scripts/script.php?script_id=1520">OmniCppComplete</a> plugin for vim. The plugin uses the <code>ctags</code>&#8216; generated tags file for the autocompletion. In order for the autocompletion to properly work with classes, you need to create the <code>~/.ctags</code> file and add some default options. Each option should be listed in a newline. Your <code>~/.ctags</code> file should look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">--c++-kinds=+p
--fields=+iaS
--extra=+q</pre></div></div>

<p>These options will now be used as default when running <code>ctags</code>.</p>
<p>The next step is to create a tag file for the wxWidgets library. This can be done using the following command:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"> ctags -f ~/.vim/wxwidgetstags -R /usr/include/wx-2.8</pre></div></div>

<p>Don&#8217;t forget to replace <code>/usr/include/wx-2.8</code> with the path to the header files of wxWidgets. This will create the tags file under your <code>~/.vim</code> directory.</p>
<p>The next step is to tell vim to use this tags file. This is done by adding the following line to your <code>~/.vimrc</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">:set tags+=~/.vim/wxwidgetstags</pre></div></div>

<p>And now your done. OmniComplete will now work for wxWidgets, listing members and functions for any of the wxWidgets classes.<br />
Remember you can repeat the last two steps for any other libraries you use in order for the OmniComplete to work with them too.</p>
<p>N.B. don&#8217;t forget to run <code>ctags -R .</code> in your projects root dir if you want OmniComplete to work for classes you defined.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2008/02/23/setting-up-omnicomplete-autocompletion-for-wxwidgets-in-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C&#8217;s &#8220;Goes To&#8221; Operator</title>
		<link>http://www.guyrutenberg.com/2007/11/19/c-goes-to-operator/</link>
		<comments>http://www.guyrutenberg.com/2007/11/19/c-goes-to-operator/#comments</comments>
		<pubDate>Mon, 19 Nov 2007 18:24:14 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/11/19/cs-goes-to-operator/</guid>
		<description><![CDATA[Well it isn&#8217;t really an operator but this is a nice C code construct I ran into. It doesn&#8217;t seem to have any benefit except as a nice way to create a reverse loop:

int count = 100;
while &#40;count--&#62;0&#41; &#123;
        //some work
&#125;

As I said I don&#8217;t think I&#8217;ll find [...]]]></description>
			<content:encoded><![CDATA[<p>Well it isn&#8217;t really an operator but this is a nice C code construct I ran into. It doesn&#8217;t seem to have any benefit except as a nice way to create a reverse loop:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> count <span style="color: #339933;">=</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>count<span style="color: #339933;">--&gt;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//some work</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As I said I don&#8217;t think I&#8217;ll find any performance benefit for using this code snippet. On the other hand it is always fun to see the puzzled face other programmers have the first time they see the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/11/19/c-goes-to-operator/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Optimizing for Loops: Reverse Loops</title>
		<link>http://www.guyrutenberg.com/2007/11/07/optimizing-for-loops-reverse-loops/</link>
		<comments>http://www.guyrutenberg.com/2007/11/07/optimizing-for-loops-reverse-loops/#comments</comments>
		<pubDate>Wed, 07 Nov 2007 14:26:24 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/11/07/optimizing-for-loops-reverse-loops/</guid>
		<description><![CDATA[for loops are basic language constructs in many languages. One of the first thing to look at when optimizing code is the loops, as they do considerable amounts of work (like going through a very large amount of data), in very little code.
If you go use for loop, but you don&#8217;t really care about the [...]]]></description>
			<content:encoded><![CDATA[<p><code>for</code> loops are basic language constructs in many languages. One of the first thing to look at when optimizing code is the loops, as they do considerable amounts of work (like going through a very large amount of data), in very little code.</p>
<p>If you go use <code>for</code> loop, but you don&#8217;t really care about the order in which the loop is executed, to be more precise, if you can afford reversing to loop, you can save quite some time. By reversing the loop I mean instead of giving the index values from 0 to 10 for example, you go from 10 downward to zero. This doesn&#8217;t seem like a big change, but when being carefully implemented this can easily upgrade the performance of your <code>for</code> loops.<br />
<span id="more-27"></span><br />
Take a look at the following <code>for</code> loop implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;</span><span style="color: #0000dd;">10</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//some work</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This loop goes from 0 to 10 and does some work. If the kind of work done allows one to go from 9 to 0, a <code>for</code> loop with the same functionality can be implemented like this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">9</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008080;">;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
	<span style="color: #666666;">//some work</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Notice the difference between the two implementations. The first one, needs to compare the index to the stopping value, and then increases the index for the next iteration. On the other hand, the second implementation just checks that the index isn&#8217;t zero and increases it in the same statement.</p>
<p>To check if the theory behind this optimization is right, I&#8217;ve put a short piece of code to check it.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// fortest.cpp</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;time.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
timespec diff<span style="color: #008000;">&#40;</span>timespec start, timespec end<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	timespec time1, time2<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> i, temp <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	clock_gettime<span style="color: #008000;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID, <span style="color: #000040;">&amp;</span>time1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;=</span> <span style="color: #0000dd;">2420000000</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
		temp<span style="color: #000040;">+</span><span style="color: #000080;">=</span>temp<span style="color: #008080;">;</span>
	clock_gettime<span style="color: #008000;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID, <span style="color: #000040;">&amp;</span>time2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>diff<span style="color: #008000;">&#40;</span>time1,time2<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">tv_sec</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;:&quot;</span><span style="color: #000080;">&lt;&lt;</span>diff<span style="color: #008000;">&#40;</span>time1,time2<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">tv_nsec</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	temp <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
	clock_gettime<span style="color: #008000;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID, <span style="color: #000040;">&amp;</span>time1<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">=</span> <span style="color: #0000dd;">2420000000</span><span style="color: #008080;">;</span> i<span style="color: #000040;">--</span><span style="color: #008080;">;</span> <span style="color: #008000;">&#41;</span>
		temp<span style="color: #000040;">+</span><span style="color: #000080;">=</span>temp<span style="color: #008080;">;</span>
	clock_gettime<span style="color: #008000;">&#40;</span>CLOCK_PROCESS_CPUTIME_ID, <span style="color: #000040;">&amp;</span>time2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span>diff<span style="color: #008000;">&#40;</span>time1,time2<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">tv_sec</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;:&quot;</span><span style="color: #000080;">&lt;&lt;</span>diff<span style="color: #008000;">&#40;</span>time1,time2<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">tv_nsec</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
timespec diff<span style="color: #008000;">&#40;</span>timespec start, timespec end<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	timespec temp<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>end.<span style="color: #007788;">tv_nsec</span><span style="color: #000040;">-</span>start.<span style="color: #007788;">tv_nsec</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&lt;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		temp.<span style="color: #007788;">tv_sec</span> <span style="color: #000080;">=</span> end.<span style="color: #007788;">tv_sec</span><span style="color: #000040;">-</span>start.<span style="color: #007788;">tv_sec</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
		temp.<span style="color: #007788;">tv_nsec</span> <span style="color: #000080;">=</span> <span style="color: #0000dd;">1000000000</span><span style="color: #000040;">+</span>end.<span style="color: #007788;">tv_nsec</span><span style="color: #000040;">-</span>start.<span style="color: #007788;">tv_nsec</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
		temp.<span style="color: #007788;">tv_sec</span> <span style="color: #000080;">=</span> end.<span style="color: #007788;">tv_sec</span><span style="color: #000040;">-</span>start.<span style="color: #007788;">tv_sec</span><span style="color: #008080;">;</span>
		temp.<span style="color: #007788;">tv_nsec</span> <span style="color: #000080;">=</span> end.<span style="color: #007788;">tv_nsec</span><span style="color: #000040;">-</span>start.<span style="color: #007788;">tv_nsec</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">return</span> temp<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>To compile it use <code>g++ -lrt fortest.cpp -o fortest</code> (don&#8217;t turn on, yet, any kind of compiler optimization). The program prints two line, one for every kind of <code>for</code> loop. Each line states the time it took for the for loop to complete in a seconds:nanoseconds format.</p>
<p>A typical run of the program on my machine resulted in:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">10:338986608
9:728866372</pre></div></div>

<p>The difference is about 0.5 seconds, which isn&#8217;t very small. On the other hand, it&#8217;s pretty small if taking into account that we did very little work in every iteration. But nonetheless it a speed gain you can easily achieve by just reversing the <code>for</code> loop code.</p>
<p>By the way, if you do use optimization, the speed gain is smaller, but compared to the runtime of the loop it can improve runtime by up to 70% (all my test showed an improvement of at least 50%).</p>
<p>For example one the same code compiled with the &#8220;-O2&#8243; optimization flag, I got the following output:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">0:873
0:255</pre></div></div>

<p>Which is a big improvement when considering the the total runtime of the traditional loop.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/11/07/optimizing-for-loops-reverse-loops/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>The Revised String Iteration Benchmark</title>
		<link>http://www.guyrutenberg.com/2007/09/26/the-revised-string-iteration-benchmark/</link>
		<comments>http://www.guyrutenberg.com/2007/09/26/the-revised-string-iteration-benchmark/#comments</comments>
		<pubDate>Wed, 26 Sep 2007 16:44:42 +0000</pubDate>
		<dc:creator>Guy</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[optimization]]></category>

		<guid isPermaLink="false">http://www.guyrutenberg.com/2007/09/26/the-revised-string-iteration-benchmark/</guid>
		<description><![CDATA[In this post I&#8217;m going to discuss again the string benchmark I did before to find out what is the fastest way to iterate over an std::string. If you haven&#8217;t read the previous post on this subject go a head and read it as it covers the basic idea behind this benchmark. As I did [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;m going to discuss again the <a href="/2007/08/30/what-is-the-fastest-method-to-iterate-over-a-string/">string benchmark</a> I did before to find out what is the fastest way to iterate over an <code>std::string</code>. If you haven&#8217;t read the previous post on this subject go a head and read it as it covers the basic idea behind this benchmark. As I did the last time I did the benchmark, I check 5 ways of iteration:<br />
<span id="more-20"></span></p>
<ul>
<li>Iterating using the native indexes.</li>
<li>Iterating using the <code>string::at</code> method.</li>
<li>Iterating using indexes over the C string representation of the string.</li>
<li>Iterating using pointers over the C string representation of the string.</li>
<li>Iterating using the STL iterators.</li>
</ul>
<p>The basic operation that tests the performance of each kind of iteration hasn&#8217;t been changed, and as in the last benchmark I used a test string of 50,000,000 characters. Instead I&#8217;ve altered the timing mechanism. In the first test, each one of the iteration methods had it&#8217;s one executable and the performance was timed as the execution time timed by the <code>time</code> command for each executable, this methods has a problematic overhead in the form of the time it takes to load to memory each one of the 50MB binaries. In this test I&#8217;ve  fixed this problem and now the tests timed using <a href="/2007/09/22/profiling-code-using-clock_gettime/"><code>clock_gettime</code></a>. This allowed me to preform all tests from within the same executable and without the overhead of the actual loading time. As a bonus the timer in <code>clock_gettime</code> has much higher resolution, so the results should be much more accurate.</p>
<p>To preform the test download the benchmark&#8217;s source code &#8211; <a href='http://www.guyrutenberg.com/wp-content/uploads/2007/09/stringbenchmark2.tar.gz' title='stringbenchmark2.tar.gz'>stringbenchmark2.tar.gz</a>. After you download it go to the directory where you saved the archive and execute<br />
<code><br />
tar -zxvf stringbenchmark2.tar.gz<br />
cd stringbenchmark2<br />
make<br />
./benchmark<br />
</code><br />
The last command is the actual benchmark. It will ran each of the tests and print its timing in the format of seconds:nanoseconds. For example a typical output of the benchmark on my system looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">String iteration benchmark
String length: 50000000
iteration using indexes
0:588484755
&nbsp;
iteration using indexes over C string
0:617691596
&nbsp;
iteration using string::at()
0:615612772
&nbsp;
iteration using pointers over C string
0:515342342
&nbsp;
iteration using iterators
2:166078723</pre></div></div>

<p>As you can see the results (in the relative speeds between the iteration methods) remained the same as in the last benchmark. Iterating using pointers over the C string representation is the fastest method by a small margin and using iterators is the slowest by far. This time the results are much more accurate and the difference between closely performing methods such as <code>string::at</code> and using index over the C string repesentation is easily noticeable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.guyrutenberg.com/2007/09/26/the-revised-string-iteration-benchmark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

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