<?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: PHP: Arrays or Linked Lists?</title>
	<atom:link href="/2012/07/24/php-arrays-or-linked-lists/feed/" rel="self" type="application/rss+xml" />
	<link>/2012/07/24/php-arrays-or-linked-lists/</link>
	<description>on web development</description>
	<lastBuildDate>Fri, 26 Oct 2018 21:40:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>By: Sam</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/comment-page-1/#comment-409638</link>
		<dc:creator><![CDATA[Sam]]></dc:creator>
		<pubDate>Thu, 02 Jun 2016 04:53:10 +0000</pubDate>
		<guid isPermaLink="false">/?p=3259#comment-409638</guid>
		<description><![CDATA[@Brilliand I don&#039;t understand what you mean his items are a kind of hash tables. Care to explain?]]></description>
		<content:encoded><![CDATA[<p>@Brilliand I don&#8217;t understand what you mean his items are a kind of hash tables. Care to explain?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brilliand</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/comment-page-1/#comment-16413</link>
		<dc:creator><![CDATA[Brilliand]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 17:43:11 +0000</pubDate>
		<guid isPermaLink="false">/?p=3259#comment-16413</guid>
		<description><![CDATA[For reference, here&#039;s the code that I used for my benchmark:

Array benchmark:
&lt;pre lang=&quot;PHP&quot; &gt;
&lt;?php
$max = 100000;
$base_memory = memory_get_usage();
$data = array();
for($i = 1; $i &#060;= $max; $i++) {
	$data[] = $i;
	if($i % 100 == 0)	// Having this code in the loop appears to have no significant effect on memory usage
		echo $i.&quot;\t&quot;.(memory_get_usage() - $base_memory).&quot;\n&quot;;
}
&lt;/pre&gt;

Linked list benchmark:

&lt;pre lang=&quot;PHP&quot;&gt;
&lt;?php
class Item {
	public $data;
	public $prev;
	public function __construct($data, $prev) {
		$this-&#062;data = $data;
		$this-&#062;prev = $prev;
	}
}

$max = 100000;
$base_memory = memory_get_usage();
$head = new Item(1, null);
$tail = $head;
for($i = 2; $i &#060;= $max; $i++) {
	$tail = new Item($i, $tail);
	if($i % 100 == 0)
		echo $i.&quot;\t&quot;.(memory_get_usage() - $base_memory).&quot;\n&quot;;
}
&lt;/pre&gt;]]></description>
		<content:encoded><![CDATA[<p>For reference, here&#8217;s the code that I used for my benchmark:</p>
<p>Array benchmark:</p>
<pre lang="PHP" >
< ?php
$max = 100000;
$base_memory = memory_get_usage();
$data = array();
for($i = 1; $i &lt;= $max; $i++) {
	$data[] = $i;
	if($i % 100 == 0)	// Having this code in the loop appears to have no significant effect on memory usage
		echo $i."\t".(memory_get_usage() - $base_memory)."\n";
}
</pre>
<p>Linked list benchmark:</p>
<pre lang="PHP">
< ?php
class Item {
	public $data;
	public $prev;
	public function __construct($data, $prev) {
		$this-&gt;data = $data;
		$this-&gt;prev = $prev;
	}
}

$max = 100000;
$base_memory = memory_get_usage();
$head = new Item(1, null);
$tail = $head;
for($i = 2; $i &lt;= $max; $i++) {
	$tail = new Item($i, $tail);
	if($i % 100 == 0)
		echo $i."\t".(memory_get_usage() - $base_memory)."\n";
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brilliand</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/comment-page-1/#comment-16411</link>
		<dc:creator><![CDATA[Brilliand]]></dc:creator>
		<pubDate>Wed, 01 Aug 2012 17:34:06 +0000</pubDate>
		<guid isPermaLink="false">/?p=3259#comment-16411</guid>
		<description><![CDATA[I&#039;m not sure you&#039;ve really done arrays justice.  In C, arrays are more memory-efficient than linked lists by the size of the pointers (which linked lists need to store and arrays don&#039;t), at the cost of being harder to resize.  However, PHP &quot;arrays&quot; are not merely arrays - they&#039;re hashtables crossed with doubly-linked lists.

On the other hand, your linked list is contaminated with hashtables as well - every single one of your items is a hashtable.  Intuitively, that should lose the efficiency race - so why did it win?

It took me until this point to see it, but you&#039;ve actually filled your array with hashtable nodes, thus burdening your array with all of the inefficiencies of your linked list implementation on top of its own implementation.  Not doing arrays justice indeed!

I thought that smooth line was suspect, so I ran my own benchmarks:
http://www.freeimagehosting.net/7kvmr

The line for linked lists cuts off about midway because... my Apache Server crashed at that point.  At that point, the linked list is showing memory usage that&#039;s worse by a factor of three.

A true linked list (with no hashtable contamination) would be able to outperform PHP&#039;s array, but a true array would still beat it by a factor of about two.  Linked lists really come into their own when you want to insert or remove elements in the middle of the sequence - for arrays, or just about any other data type, this is a linear-time operation, but linked lists can do it in constant time.  PHP arrays, being a hybridized linked list, should theoretically be able to pull that off, but as far as I know there is no PHP function that exposes that functionality.  A similar situation arises when you want to insert or remove elements at the beginning of the sequence - while there are several data types that can do that in constant time (for example, deques), PHP arrays are not one of them, at least when used conventionally; things may be a bit different when the array is in &quot;associative mode&quot;.]]></description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure you&#8217;ve really done arrays justice.  In C, arrays are more memory-efficient than linked lists by the size of the pointers (which linked lists need to store and arrays don&#8217;t), at the cost of being harder to resize.  However, PHP &#8220;arrays&#8221; are not merely arrays &#8211; they&#8217;re hashtables crossed with doubly-linked lists.</p>
<p>On the other hand, your linked list is contaminated with hashtables as well &#8211; every single one of your items is a hashtable.  Intuitively, that should lose the efficiency race &#8211; so why did it win?</p>
<p>It took me until this point to see it, but you&#8217;ve actually filled your array with hashtable nodes, thus burdening your array with all of the inefficiencies of your linked list implementation on top of its own implementation.  Not doing arrays justice indeed!</p>
<p>I thought that smooth line was suspect, so I ran my own benchmarks:<br />
<a href="http://www.freeimagehosting.net/7kvmr" rel="nofollow">http://www.freeimagehosting.net/7kvmr</a></p>
<p>The line for linked lists cuts off about midway because&#8230; my Apache Server crashed at that point.  At that point, the linked list is showing memory usage that&#8217;s worse by a factor of three.</p>
<p>A true linked list (with no hashtable contamination) would be able to outperform PHP&#8217;s array, but a true array would still beat it by a factor of about two.  Linked lists really come into their own when you want to insert or remove elements in the middle of the sequence &#8211; for arrays, or just about any other data type, this is a linear-time operation, but linked lists can do it in constant time.  PHP arrays, being a hybridized linked list, should theoretically be able to pull that off, but as far as I know there is no PHP function that exposes that functionality.  A similar situation arises when you want to insert or remove elements at the beginning of the sequence &#8211; while there are several data types that can do that in constant time (for example, deques), PHP arrays are not one of them, at least when used conventionally; things may be a bit different when the array is in &#8220;associative mode&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stoimen</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/comment-page-1/#comment-16363</link>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
		<pubDate>Tue, 24 Jul 2012 12:41:26 +0000</pubDate>
		<guid isPermaLink="false">/?p=3259#comment-16363</guid>
		<description><![CDATA[@AfterMath - absolutely agree with that! Arrays are far more simple to use. However in some libraries there are built-in linked list implementations so if that&#039;s more memory efficient why not use it?]]></description>
		<content:encoded><![CDATA[<p>@AfterMath &#8211; absolutely agree with that! Arrays are far more simple to use. However in some libraries there are built-in linked list implementations so if that&#8217;s more memory efficient why not use it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AfterMath</title>
		<link>/2012/07/24/php-arrays-or-linked-lists/comment-page-1/#comment-16362</link>
		<dc:creator><![CDATA[AfterMath]]></dc:creator>
		<pubDate>Tue, 24 Jul 2012 12:39:12 +0000</pubDate>
		<guid isPermaLink="false">/?p=3259#comment-16362</guid>
		<description><![CDATA[You also have to take into consideration the overhead involved in programming a linked list.  Even to a developed programmer, the time it takes to program a linked list sometimes makes the ease of an array more attractive, particularly for simpler tasks.]]></description>
		<content:encoded><![CDATA[<p>You also have to take into consideration the overhead involved in programming a linked list.  Even to a developed programmer, the time it takes to program a linked list sometimes makes the ease of an array more attractive, particularly for simpler tasks.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
