<?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>Mac / iPhone App Development &#187; dynamic arrays</title>
	<atom:link href="http://benreeves.co.uk/tag/dynamic-arrays/feed/" rel="self" type="application/rss+xml" />
	<link>http://benreeves.co.uk</link>
	<description>Home of a Small Time Developer</description>
	<lastBuildDate>Wed, 26 May 2010 14:24:12 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Arrays with C</title>
		<link>http://benreeves.co.uk/dynamic-arrays-with-c/</link>
		<comments>http://benreeves.co.uk/dynamic-arrays-with-c/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 17:12:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[dynamic arrays]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=81</guid>
		<description><![CDATA[In the final push to add syntax highlighting to iVersion I have been working on optimising the parser.
In the inner parsing loop I have been using the standard NSMutableArray to keep track of various objects and tokens. Objective C message passing is of course an order of times slower than a C function call, even [...]]]></description>
			<content:encoded><![CDATA[<p>In the final push to add syntax highlighting to iVersion I have been working on optimising the parser.</p>
<p>In the inner parsing loop I have been using the standard NSMutableArray to keep track of various objects and tokens. Objective C message passing is of course an order of times slower than a C function call, even if purely for the fact that they can’t be inlined. A great article that explains more about the objective c runtime an be found on the <a href="http://cocoasamurai.blogspot.com/2010/01/understanding-objective-c-runtime.html" target="_blank">cocoa samurai blog</a></p>
<p>So I decided to drop down to a dynamic array using plain C. However even though I’m sure dynamic arrays have been implement thousands of times I couldn’t seem to find any simple implementations.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//** Usage</span>
<span style="color: #11740a; font-style: italic;">//Create an array with capacity 2</span>
CArray <span style="color: #002200;">*</span> array <span style="color: #002200;">=</span> CArrayInit<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add two buckets</span>
CArrayAdd<span style="color: #002200;">&#40;</span>array, <span style="color: #bf1d1a;">&quot;test&quot;</span><span style="color: #002200;">&#41;</span>;
CArrayAdd<span style="color: #002200;">&#40;</span>array, <span style="color: #bf1d1a;">&quot;another test&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add another and it will resize accordingly</span>
CArrayAdd<span style="color: #002200;">&#40;</span>array, <span style="color: #bf1d1a;">&quot;yet another&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Print All</span>
<span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> ii <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; ii <span style="color: #002200;">&amp;</span>lt; array<span style="color: #002200;">-&amp;</span>gt;count; <span style="color: #002200;">++</span>ii<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span> buffer <span style="color: #002200;">=</span> CArrayObjectAtIndex<span style="color: #002200;">&#40;</span>array, ii<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;Index: %d - %s<span style="color: #2400d9;">\n</span>&quot;</span>, ii, buffer<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><strong>Download:</strong></p>
<p><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/CArray.h">CArray.h</a></p>
<p><a href="http://www.benreeves.co.uk/wp-content/uploads/2010/01/CArray.c">CArray.c</a></p>
<p><strong>Notes:</strong><br />
The following test  function usually reports about 5x faster than NSMutableArray which I think is a credit to how efficient the Objective C library and messaging system really is considering all the things your missing out on (exceptions, null pointers etc)</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">void</span> CArrayTestRoutine<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
&nbsp;
    CArray <span style="color: #002200;">*</span> array <span style="color: #002200;">=</span> CArrayInit<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    NSTimeInterval start <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> timeIntervalSince1970<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> test;
&nbsp;
    <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> ii <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; ii <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">10000000</span>; <span style="color: #002200;">++</span>ii<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        CArrayAdd<span style="color: #002200;">&#40;</span>array, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span><span style="color: #002200;">&#41;</span>;
        test <span style="color: #002200;">=</span> CArrayObjectAtIndex<span style="color: #002200;">&#40;</span>array, CArrayCount<span style="color: #002200;">&#40;</span>array<span style="color: #002200;">&#41;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
     CArrayRemoveObjectAtIndex<span style="color: #002200;">&#40;</span>array, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
     NSTimeInterval end <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> timeIntervalSince1970<span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;CArray 100,000,000 Elements took %fs<span style="color: #2400d9;">\n</span>&quot;</span>, end<span style="color: #002200;">-</span>start<span style="color: #002200;">&#41;</span>;
&nbsp;
     <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span> nsarray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
     start <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> timeIntervalSince1970<span style="color: #002200;">&#93;</span>;
     <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> <span style="color: #a61390;">string</span>;
&nbsp;
     <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> ii <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; ii <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">10000000</span>; <span style="color: #002200;">++</span>ii<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>nsarray addObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;test&quot;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>nsarray objectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>nsarray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #002200;">&#91;</span>nsarray removeObjectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
     end <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span> timeIntervalSince1970<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>nsarray release<span style="color: #002200;">&#93;</span>;
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;NSMutableArray 100,000,000 Elements took %fs<span style="color: #2400d9;">\n</span>&quot;</span>, end<span style="color: #002200;">-</span>start<span style="color: #002200;">&#41;</span>;
&nbsp;
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s<span style="color: #2400d9;">\n</span>&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
     <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;%s<span style="color: #2400d9;">\n</span>&quot;</span>, <span style="color: #002200;">&#91;</span>test UTF8String<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
     CArrayDestroy<span style="color: #002200;">&#40;</span>array<span style="color: #002200;">&#41;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://benreeves.co.uk/dynamic-arrays-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
