<?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>Blog by Andreas Glaser &#187; Performance</title>
	<atom:link href="http://www.andreas-glaser.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.andreas-glaser.com</link>
	<description>Development, Technology and such...</description>
	<lastBuildDate>Wed, 18 May 2011 23:16:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>PHP &#8211; count() vs. sizeOf()</title>
		<link>http://www.andreas-glaser.com/2008/08/19/php-count-vs-sizeof/</link>
		<comments>http://www.andreas-glaser.com/2008/08/19/php-count-vs-sizeof/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 08:51:57 +0000</pubDate>
		<dc:creator>Andreas Glaser</dc:creator>
				<category><![CDATA[Computer]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[PHP5]]></category>
		<category><![CDATA[Script]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://www.andreas-glaser.com/?p=65</guid>
		<description><![CDATA[I was wondering whether there is a performance difference between count() and sizeof(). Test code: ?View Code PHP1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 [...]]]></description>
			<content:encoded><![CDATA[<p>I was wondering whether there is a performance difference between count() and sizeof().</p>
<p><strong>Test code:</strong></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p65code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p652"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
</pre></td><td class="code" id="p65code2"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>?PHP
&nbsp;
  <span style="color: #666666; font-style: italic;">// CREATE TEST ARRAY</span>
  <span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'b'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'c'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'d'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'e'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'f'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'g'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'h'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'i'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'j'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// START TIMER ONE</span>
  <span style="color: #000088;">$TimerOne</span> <span style="color: #339933;">=</span> benchmarkTimerStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// CALL COUNT FUNCTION 1000000 TIMES</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// STOP TIMER ONE</span>
  <span style="color: #000088;">$ResultOne</span> <span style="color: #339933;">=</span> benchmarkTimerStop<span style="color: #009900;">&#40;</span><span style="color: #000088;">$TimerOne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// START TIMER TWO</span>
  <span style="color: #000088;">$TimerTwo</span> <span style="color: #339933;">=</span> benchmarkTimerStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// CALL SIZEOF FUNCTION 1000000 TIMES</span>
  <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <a href="http://www.php.net/sizeof"><span style="color: #990000;">sizeOf</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// STOP TIMER TWO</span>
  <span style="color: #000088;">$ResultTwo</span> <span style="color: #339933;">=</span> benchmarkTimerStop<span style="color: #009900;">&#40;</span><span style="color: #000088;">$TimerTwo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// PRINT RESULTS</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Count: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$ResultOne</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' Seconds&amp;lt;br /&amp;gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'SizeOf: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$ResultTwo</span><span style="color: #339933;">.</span><span style="color: #0000ff;">' Seconds'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// TIMER FUNCTIONS</span>
  <span style="color: #000000; font-weight: bold;">function</span> benchmarkTimerStart<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$timeExplode</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$timeExplode</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$timeExplode</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$time</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> benchmarkTimerStop<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timer</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$timeExplode</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$time</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$timeExplode</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$timeExplode</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$finish</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$time</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timer</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$endTime</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/sprintf"><span style="color: #990000;">sprintf</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%4.3f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$finish</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$endTime</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
?<span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>My test proved that there is <strong>no difference at all</strong>! sizeof() is just an alias for count().<br />
So I suggest you to use count() &#8211; its the original name of this function.</p>
<p><em><strong>So long,<br />
Andreas Glaser (aka JaZz)</strong></em></p>
<fb:like href='http://www.andreas-glaser.com/2008/08/19/php-count-vs-sizeof/' send='' layout='standard' show_faces='true' width='450' height='65' action='like' colorscheme='light' font='lucida+grande'></fb:like>]]></content:encoded>
			<wfw:commentRss>http://www.andreas-glaser.com/2008/08/19/php-count-vs-sizeof/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

