<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Swingpant&#039;s Game Lab &#187; slice</title>
	<atom:link href="http://swingpants.com/tag/slice/feed/" rel="self" type="application/rss+xml" />
	<link>http://swingpants.com</link>
	<description>Swingpant&#039;s Code Nurdlings</description>
	<lastBuildDate>Tue, 08 May 2012 21:07:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='swingpants.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/4a88693aa17d6b5e1d453a6684bc1c68?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Swingpant&#039;s Game Lab &#187; slice</title>
		<link>http://swingpants.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://swingpants.com/osd.xml" title="Swingpant&#039;s Game Lab" />
	<atom:link rel='hub' href='http://swingpants.com/?pushpress=hub'/>
		<item>
		<title>Fastest Way to Copy An Array: Concat() or Slice(0)</title>
		<link>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/</link>
		<comments>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 00:54:48 +0000</pubDate>
		<dc:creator>swingpants</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Benchtesting]]></category>
		<category><![CDATA[Code Snippet]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[benchtest]]></category>
		<category><![CDATA[concatenate]]></category>
		<category><![CDATA[slice]]></category>

		<guid isPermaLink="false">http://flashlabs.wordpress.com/?p=85</guid>
		<description><![CDATA[What is the fastest way to copy an array? Concat or Slice? There is only one way to find out. FIGHT! OK, so we can dismiss any kind of for-loop &#8211; far too slow, so that leaves us with: 1) array.concat() &#8211; i.e. concatenate an array onto nothing and deliver that as a new array. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=85&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is the fastest way to copy an array? Concat or Slice? There is only one way to find out. FIGHT!</p>
<p>OK, so we can dismiss any kind of for-loop &#8211; far too slow, so that leaves us with:<br />
    1)   array.concat() &#8211; i.e. concatenate an array onto nothing and deliver that as a new array.<br />
    2)   array.slice(0) &#8211; i.e. return a new array consisting of all of the elements of the old array &#8211; from 0 till the end (up to a max of 16777215)</p>
<p>I&#8217;ve set up an array with a cool 1million entries (ok, it is not big, and it is not clever so it certainly isn&#8217;t cool). I need to copy this. The following code executes each method once on every iteration. It keeps a running total and records the average time each takes. I&#8217;ve limited the code to 100 iterations.</p>
<blockquote>
<p>package<br />
{<br />
	import flash.display.Sprite;<br />
	import flash.events.Event;<br />
	import flash.events.MouseEvent;<br />
	import flash.text.TextField;<br />
    import flash.utils.*;<br />
	public class TestConcat extends Sprite<br />
	{<br />
		private var iteration_count:int=0<br />
		private var concat_total:int=0<br />
		private var slice_total:int=0<br />
		private var clone_total:int=0<br />
		private var tf:TextField = new TextField()<br />
		private var test_array:Array = [];</p>
<p>		public function TestConcat():void<br />
		{<br />
			tf.x = tf.y = 100; tf.width = 600;<br />
			addChild(tf)</p>
<p>			//Set up array to copy<br />
			for(var i:int = 0; i &lt; 1000000; i++) test_array.push(i);<br />
			//Mouse click to rerun test<br />
			stage.addEventListener(MouseEvent.CLICK, go);<br />
			//First run<br />
			go()<br />
		}</p>
<p>		private function go(e:Event = null):void<br />
		{<br />
			iteration_count=concat_total=slice_total=clone_total=0<br />
			addEventListener(Event.ENTER_FRAME, iterate)<br />
		}</p>
<p>		//Loop through tests<br />
		private function iterate(e:Event=null):void<br />
		{<br />
			concat_total +=testConcat()<br />
			slice_total += testSlice()<br />
			clone_total += testByteArrayClone()<br />
			iteration_count++<br />
			tf.text = &quot;Av. Concat time=&quot; + (concat_total / iteration_count)<br />
					+ &quot;ms  Av. Slice time=&quot; + (slice_total / iteration_count)<br />
					+ &quot;ms  Av. BA Clone time=&quot; + (clone_total / iteration_count) + &quot;ms&quot;;<br />
			if(iteration_count&lt;99) removeEventListener(Event.ENTER_FRAME,iterate)<br />
		}</p>
<p>		//test array slice<br />
		private function testSlice():int<br />
		{<br />
			var time_slice_start:Number = getTimer();<br />
			var slice_copy:Array = test_array.slice(0);<br />
			return getTimer()-time_slice_start<br />
		}</p>
<p>		//test array concat<br />
		private function testConcat():int<br />
		{<br />
			var time_concat_start:Number = getTimer();<br />
			var concat_copy:Array = test_array.concat();<br />
			return getTimer()-time_concat_start<br />
		}</p>
<p>		//test BA Clone method<br />
		private function testByteArrayClone():int<br />
		{<br />
			var time_concat_start:Number = getTimer();<br />
			var concat_copy:Array = clone(test_array);<br />
			return getTimer()-time_concat_start<br />
		}</p>
<p>		//Clone method for Deep Objects(via Bruno)<br />
		private function clone(source:Object):*<br />
		{<br />
			var myBA:ByteArray = new ByteArray();<br />
			myBA.writeObject(source);<br />
			myBA.position = 0;<br />
			return(myBA.readObject());<br />
		}<br />
	}<br />
}</p>
</blockquote>
<p>On my laptop I&#8217;m clocking the <strong>concat at 14ms</strong> and the <strong>slice at over 29ms</strong>. </p>
<p>So a conclusive result. concat is twice the speed (with large arrays &#8211; the difference diminishes considerably with smaller arrays)</p>
<p>Give the code a few run throughs and see what you get. Let me know if your results are markedly different.</p>
<p>UPDATED:<br />
I have updated the code and added a swf to try out <a href="http://www.swingpantsflash.com/array_copy/">here</a> and the source code <a href="http://www.swingpantsflash.com/array_copy/TestConcat.as">here</a><br />
<div id="attachment_350" class="wp-caption aligncenter" style="width: 460px"><a href="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg"><img src="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg?w=655" alt="Fastest way to copy an array" title="copy_test"   class="size-full wp-image-350" /></a><p class="wp-caption-text">Test the array copy for yourself</p></div><br />
I&#8217;ve also added in a test for the Byte Array Clone method suggested by Bruno (see his comments below). This method seems a great one for copying &#8216;deep&#8217; arrays &#8211; arrays of complex objects (arrays, objects or other types). In this context and test (copying shallow arrays) the instantiation, writing and reading adds too much overhead. I&#8217;ll need to test this in a useful context: with deep arrays.</p>
<p>Demo: <a href="http://www.swingpantsflash.com/array_copy/">Array Copy test</a><br />
Source: <a href="http://www.swingpantsflash.com/array_copy/TestConcat.as">Source.as</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/flashlabs.wordpress.com/85/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/flashlabs.wordpress.com/85/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/flashlabs.wordpress.com/85/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=swingpants.com&#038;blog=5703708&#038;post=85&#038;subd=flashlabs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://swingpants.com/2009/03/12/fastest-way-to-copy-an-array-concat-or-slice0/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1cebf29489913586137c5bdad0414c88?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">swingpants</media:title>
		</media:content>

		<media:content url="http://flashlabs.files.wordpress.com/2009/03/copy_test.jpg" medium="image">
			<media:title type="html">copy_test</media:title>
		</media:content>
	</item>
	</channel>
</rss>
