<?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: Runnables as Function Pointers</title>
	<atom:link href="http://jacksondunstan.com/articles/323/feed" rel="self" type="application/rss+xml" />
	<link>http://jacksondunstan.com/articles/323</link>
	<description>Mastering AS3</description>
	<lastBuildDate>Thu, 29 Jul 2010 17:30:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Uwe Holland</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-76</link>
		<dc:creator>Uwe Holland</dc:creator>
		<pubDate>Sat, 26 Sep 2009 21:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-76</guid>
		<description>Find my answers &lt;a href=&quot;http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;. :)</description>
		<content:encoded><![CDATA[<p>Find my answers <a href="http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/" rel="nofollow">here</a>. :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Troy Gilbert</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-69</link>
		<dc:creator>Troy Gilbert</dc:creator>
		<pubDate>Sat, 26 Sep 2009 01:44:04 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-69</guid>
		<description>Wow, Flash Player on Windows performs so much better when it comes to memory allocations (the real difference between events and callbacks):
&lt;pre&gt;
numListeners: 100, events: 3470ms, callbacks: 187ms, observers: 74ms
&lt;/pre&gt;
That&#039;s the release standalone Flash Player 10, the fastest way to run Flash on the Mac.</description>
		<content:encoded><![CDATA[<p>Wow, Flash Player on Windows performs so much better when it comes to memory allocations (the real difference between events and callbacks):</p>
<pre>
numListeners: 100, events: 3470ms, callbacks: 187ms, observers: 74ms
</pre>
<p>That&#8217;s the release standalone Flash Player 10, the fastest way to run Flash on the Mac.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-67</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Sat, 26 Sep 2009 01:34:14 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-67</guid>
		<description>I saw and &lt;a href=&quot;http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/comment-page-1/#comment-8&quot; rel=&quot;nofollow&quot;&gt;commented&lt;/a&gt; about it. Thanks for doing the writeup. Between the three of us we have really explored the options an AS3 programmer has for doing callbacks!</description>
		<content:encoded><![CDATA[<p>I saw and <a href="http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/comment-page-1/#comment-8" rel="nofollow">commented</a> about it. Thanks for doing the writeup. Between the three of us we have really explored the options an AS3 programmer has for doing callbacks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Uwe Holland</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-66</link>
		<dc:creator>Uwe Holland</dc:creator>
		<pubDate>Sat, 26 Sep 2009 01:30:35 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-66</guid>
		<description>I&#039;m following your discussion interested and I&#039;ve just posted our approch on my blog relying to this.  http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/</description>
		<content:encoded><![CDATA[<p>I&#8217;m following your discussion interested and I&#8217;ve just posted our approch on my blog relying to this.  <a href="http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/" rel="nofollow">http://blog.rauschgenerator.com/2009/09/26/using-callback-commands-instead-of-events/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Using Callback Commands instead of Events &#124; RGB(log)</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-65</link>
		<dc:creator>Using Callback Commands instead of Events &#124; RGB(log)</dc:creator>
		<pubDate>Sat, 26 Sep 2009 01:21:29 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-65</guid>
		<description>[...] read posts about the speed advantages of callbacks over events on the blogs of Troy Gilbert and Jackson Dustan. But instead of using a strategy like stated in the comment here we use something which we call [...]</description>
		<content:encoded><![CDATA[<p>[...] read posts about the speed advantages of callbacks over events on the blogs of Troy Gilbert and Jackson Dustan. But instead of using a strategy like stated in the comment here we use something which we call [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-64</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Fri, 25 Sep 2009 22:11:14 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-64</guid>
		<description>Thanks for the demo. I edited your comment to fix the HTML safety stuff. My results on a 3.0Ghz Intel Core 2 Duo with 2GB of RAM on Windows XP are:

&lt;pre lang=&quot;text&quot;&gt;
numListeners: 100, events: 152ms, callbacks: 109ms, observers: 34ms
&lt;/pre&gt;

So right away the &quot;observer&quot; technique is 3x faster than callbacks, just like you say. It sure looks like your observer technique is almost the same as my runnable technique; perhaps the more correct terminology. &lt;a href=&quot;http://en.wikipedia.org/wiki/Observer_pattern&quot; rel=&quot;nofollow&quot;&gt;Wikipedia&lt;/a&gt; seems to think so. In any case, it seems as though your observer technique test validates my runnable technique test, which is good to hear.

Thanks very much for providing the test!</description>
		<content:encoded><![CDATA[<p>Thanks for the demo. I edited your comment to fix the HTML safety stuff. My results on a 3.0Ghz Intel Core 2 Duo with 2GB of RAM on Windows XP are:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">numListeners: 100, events: 152ms, callbacks: 109ms, observers: 34ms</pre></div></div>

<p>So right away the &#8220;observer&#8221; technique is 3x faster than callbacks, just like you say. It sure looks like your observer technique is almost the same as my runnable technique; perhaps the more correct terminology. <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow">Wikipedia</a> seems to think so. In any case, it seems as though your observer technique test validates my runnable technique test, which is good to hear.</p>
<p>Thanks very much for providing the test!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Troy Gilbert</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-63</link>
		<dc:creator>Troy Gilbert</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:55:10 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-63</guid>
		<description>Well, some of its been eaten up by making it HTML-safe, so cut-n-paste with care!</description>
		<content:encoded><![CDATA[<p>Well, some of its been eaten up by making it HTML-safe, so cut-n-paste with care!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Troy Gilbert</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-62</link>
		<dc:creator>Troy Gilbert</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:53:51 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-62</guid>
		<description>Okay, here&#039;s my test harness:

&lt;pre lang=&quot;actionscript3&quot;&gt;
package {

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;

	public class EventsVsFunctions extends Sprite
	{
		public function EventsVsFunctions() { addEventListener(Event.ADDED_TO_STAGE, onStage); }
		private function onStage(event:Event):void { setTimeout(doTests, 5000); }
		
		private function doTests():void
		{
			var results:String = testListeners(100);

			// output results
			var tf:TextField = new TextField();
			tf.autoSize = &quot;left&quot;;
			tf.text = results;
			addChild(tf);
		}
		
		private function testListeners(numListeners:int = 1):String
		{
			const NUM_ITERATIONS:int = 10000;
			var startTime:int, stopTime:int;
			var eventsDuration:int, callbacksDuration:int, observersDuration:int;
			var i:int;
			var event:Event;
			
			// setup event listeners
			var dispatcher:EventDispatcher = new EventDispatcher();
			for (i = 0; i &lt; numListeners; i++) dispatcher.addEventListener(Event.CHANGE, new ChangeObserver().onChange);
			
			// time event dispatchs
			startTime = getTimer();
			for (i; i &lt; NUM_ITERATIONS; i++) dispatcher.dispatchEvent(new Event(Event.CHANGE));
			stopTime = getTimer();
			eventsDuration = stopTime - startTime;
			
			// setup callbacks
			for (i = 0; i &lt; numListeners; i++) callbacks.push(new ChangeObserver().onChange);
			
			// time function calls
			startTime = getTimer();
			for (i = 0; i &lt; NUM_ITERATIONS; i++) doCallback(new Event(Event.CHANGE));
			stopTime = getTimer();
			callbacksDuration = stopTime - startTime;
			
			// setup observers
			for (i = 0; i &lt; numListeners; i++) observers.push(new ChangeObserver());
			
			// time observers
			startTime = getTimer();
			for (i = 0; i &lt; NUM_ITERATIONS; i++) doObserver(new Event(Event.CHANGE));
			stopTime = getTimer();
			observersDuration = stopTime - startTime;
			
			// results as a CSV
			return &quot;numListeners: &quot; + numListeners + &quot;, events: &quot; + eventsDuration + &quot;ms, callbacks: &quot; +
				callbacksDuration + &quot;ms, observers: &quot; + observersDuration + &quot;ms&quot;;
		}
		
		private var callbacks:Array = [ ];
		private function doCallback(event:Event):void
		{
			for each (var callback:Function in callbacks) callback(event);
		}
		
		private var observers:Array = [ ];
		private function doObserver(event:Event):void
		{
			for each (var observer:IChangeObserver in observers) observer.onChange(event);
		}
	}
}

import flash.events.Event;
interface IChangeObserver {  function onChange(event:Event):void; }
class ChangeObserver implements IChangeObserver { public function onChange(event:Event):void { } }
&lt;/pre&gt;

It tries to be a slightly more real-world usage scenario. I allocate multiple objects to receive notification (via event, callback or observer), and iterate through the targets through a container (you call directly on a local instance, which will make it seem even faster).

Looking back at my times, there is a difference between callbacks (Function references) and observers (runnables). A not insignificant difference, to be honest, particularly if you look at the times in the standalone Flash Player (as opposed to debug browser plug-in). But when compared to events, callbacks and observers are in the same class.

All that being said, observers look to give a 2x-3x improvement over callbacks, but that&#039;s only measurable over hundreds of thousands of calls. Given the huge drawback of having to implement a specific interface and being limited to a single function per-class, I think callbacks (Function references) are the winner.</description>
		<content:encoded><![CDATA[<p>Okay, here&#8217;s my test harness:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span> <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.display</span>.<span style="color: #004993;">Sprite</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">EventDispatcher</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.text</span>.<span style="color: #004993;">TextField</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getTimer</span>;
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">setTimeout</span>;
&nbsp;
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> EventsVsFunctions extends <span style="color: #004993;">Sprite</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> EventsVsFunctions<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">ADDED_TO_STAGE</span>, onStage<span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span>
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> onStage<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span> <span style="color: #004993;">setTimeout</span><span style="color: #000000;">&#40;</span>doTests, <span style="color: #000000; font-weight:bold;">5000</span><span style="color: #000000;">&#41;</span>; <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doTests<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> results<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = testListeners<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">100</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #009900;">// output results</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> tf<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">TextField</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">TextField</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            tf.<span style="color: #004993;">autoSize</span> = <span style="color: #990000;">&quot;left&quot;</span>;
            tf.<span style="color: #004993;">text</span> = results;
            <span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>tf<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> testListeners<span style="color: #000000;">&#40;</span>numListeners<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #6699cc; font-weight: bold;">const</span> NUM_ITERATIONS<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span> = <span style="color: #000000; font-weight:bold;">10000</span>;
            <span style="color: #6699cc; font-weight: bold;">var</span> startTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, stopTime<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
            <span style="color: #6699cc; font-weight: bold;">var</span> eventsDuration<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, callbacksDuration<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>, observersDuration<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
            <span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">int</span>;
            <span style="color: #6699cc; font-weight: bold;">var</span> event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span>;
&nbsp;
            <span style="color: #009900;">// setup event listeners</span>
            <span style="color: #6699cc; font-weight: bold;">var</span> dispatcher<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">EventDispatcher</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">EventDispatcher</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> numListeners; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> dispatcher.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span>, <span style="color: #0033ff; font-weight: bold;">new</span> ChangeObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.onChange<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #009900;">// time event dispatchs</span>
            startTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i; i <span style="color: #000000; font-weight: bold;">&lt;</span> NUM_ITERATIONS; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> dispatcher.<span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            stopTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            eventsDuration = stopTime <span style="color: #000000; font-weight: bold;">-</span> startTime;
&nbsp;
            <span style="color: #009900;">// setup callbacks</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> numListeners; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> callbacks.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> ChangeObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.onChange<span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #009900;">// time function calls</span>
            startTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> NUM_ITERATIONS; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> doCallback<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            stopTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            callbacksDuration = stopTime <span style="color: #000000; font-weight: bold;">-</span> startTime;
&nbsp;
            <span style="color: #009900;">// setup observers</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> numListeners; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> observers.<span style="color: #004993;">push</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> ChangeObserver<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
&nbsp;
            <span style="color: #009900;">// time observers</span>
            startTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #000000;">&#40;</span>i = <span style="color: #000000; font-weight:bold;">0</span>; i <span style="color: #000000; font-weight: bold;">&lt;</span> NUM_ITERATIONS; i<span style="color: #000000; font-weight: bold;">++</span><span style="color: #000000;">&#41;</span> doObserver<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span>.<span style="color: #004993;">CHANGE</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
            stopTime = <span style="color: #004993;">getTimer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
            observersDuration = stopTime <span style="color: #000000; font-weight: bold;">-</span> startTime;
&nbsp;
            <span style="color: #009900;">// results as a CSV</span>
            <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #990000;">&quot;numListeners: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> numListeners <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;, events: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> eventsDuration <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;ms, callbacks: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span>
                callbacksDuration <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;ms, observers: &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> observersDuration <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;ms&quot;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> callbacks<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span> <span style="color: #000000;">&#93;</span>;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doCallback<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> callback<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Function</span> <span style="color: #0033ff; font-weight: bold;">in</span> callbacks<span style="color: #000000;">&#41;</span> callback<span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> observers<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span> <span style="color: #000000;">&#93;</span>;
        <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> doObserver<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span> <span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> observer<span style="color: #000000; font-weight: bold;">:</span>IChangeObserver <span style="color: #0033ff; font-weight: bold;">in</span> observers<span style="color: #000000;">&#41;</span> observer.onChange<span style="color: #000000;">&#40;</span>event<span style="color: #000000;">&#41;</span>;
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.events</span>.<span style="color: #004993;">Event</span>;
interface IChangeObserver <span style="color: #000000;">&#123;</span>  <span style="color: #339966; font-weight: bold;">function</span> onChange<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>; <span style="color: #000000;">&#125;</span>
<span style="color: #9900cc; font-weight: bold;">class</span> ChangeObserver implements IChangeObserver <span style="color: #000000;">&#123;</span> <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onChange<span style="color: #000000;">&#40;</span>event<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span></pre></div></div>

<p>It tries to be a slightly more real-world usage scenario. I allocate multiple objects to receive notification (via event, callback or observer), and iterate through the targets through a container (you call directly on a local instance, which will make it seem even faster).</p>
<p>Looking back at my times, there is a difference between callbacks (Function references) and observers (runnables). A not insignificant difference, to be honest, particularly if you look at the times in the standalone Flash Player (as opposed to debug browser plug-in). But when compared to events, callbacks and observers are in the same class.</p>
<p>All that being said, observers look to give a 2x-3x improvement over callbacks, but that&#8217;s only measurable over hundreds of thousands of calls. Given the huge drawback of having to implement a specific interface and being limited to a single function per-class, I think callbacks (Function references) are the winner.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jackson</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-61</link>
		<dc:creator>jackson</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-61</guid>
		<description>We might be talking about two different comparisons. I think you&#039;re talking about events versus callbacks and I&#039;m talking about callbacks via Function variables versus callbacks via runnables. I totally agree about events being slower than callbacks via Function variables and am trying to show that there&#039;s a further speedup. It&#039;s kind of like this:

&lt;ol&gt;
&lt;li&gt;Events (slowest)&lt;/li&gt;
&lt;li&gt;Callbacks via Function variables (slow)&lt;/li&gt;
&lt;li&gt;Callbacks via runnables (fast)&lt;/li&gt;
&lt;/ol&gt;

This article is an attempt to improve even on callbacks via Function variables by implementing callbacks via runnables. There&#039;s nothing limiting runnables to callbacks, so I covered it in general rather than just the callbacks case. Now I&#039;m wondering if you have discovered a technique that improves even on callbacks via runnables. That would be awesome to see! You can either &lt;a href=&quot;mailto:jackson@jacksondunstan.com&quot; rel=&quot;nofollow&quot;&gt;e-mail&lt;/a&gt; it to me directly or post it in comments by wrapping it up:

&lt;pre lang=&quot;actionscript3&quot;&gt;
// code goes here
&lt;/pre&gt;

Thanks!</description>
		<content:encoded><![CDATA[<p>We might be talking about two different comparisons. I think you&#8217;re talking about events versus callbacks and I&#8217;m talking about callbacks via Function variables versus callbacks via runnables. I totally agree about events being slower than callbacks via Function variables and am trying to show that there&#8217;s a further speedup. It&#8217;s kind of like this:</p>
<ol>
<li>Events (slowest)</li>
<li>Callbacks via Function variables (slow)</li>
<li>Callbacks via runnables (fast)</li>
</ol>
<p>This article is an attempt to improve even on callbacks via Function variables by implementing callbacks via runnables. There&#8217;s nothing limiting runnables to callbacks, so I covered it in general rather than just the callbacks case. Now I&#8217;m wondering if you have discovered a technique that improves even on callbacks via runnables. That would be awesome to see! You can either <a href="mailto:jackson@jacksondunstan.com" rel="nofollow">e-mail</a> it to me directly or post it in comments by wrapping it up:</p>
<p>&lt;pre lang=&quot;actionscript3&quot;&gt;<br />
// code goes here<br />
&lt;/pre&gt;</p>
<p>Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Troy Gilbert</title>
		<link>http://jacksondunstan.com/articles/323/comment-page-1#comment-60</link>
		<dc:creator>Troy Gilbert</dc:creator>
		<pubDate>Fri, 25 Sep 2009 20:36:46 +0000</pubDate>
		<guid isPermaLink="false">http://jacksondunstan.com/?p=323#comment-60</guid>
		<description>I&#039;ve got a test case that compares events, callbacks and observers. What&#039;s the best way to post code to the blog (or I could email it to you)?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve got a test case that compares events, callbacks and observers. What&#8217;s the best way to post code to the blog (or I could email it to you)?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
