<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        SendActionScriptWorkerMessages2.5xFaster

        來源:懂視網(wǎng) 責編:小采 時間:2020-11-09 07:43:31
        文檔

        SendActionScriptWorkerMessages2.5xFaster

        SendActionScriptWorkerMessages2.5xFaster:Original Site:http://jacksondunstan.com/articles/2416 We know that sending messages between ActionScript workers is slow, but how can we make it faster Todays article discusses an alternative approach to message passing that yields a 2.
        推薦度:
        導讀SendActionScriptWorkerMessages2.5xFaster:Original Site:http://jacksondunstan.com/articles/2416 We know that sending messages between ActionScript workers is slow, but how can we make it faster Todays article discusses an alternative approach to message passing that yields a 2.

        Original Site:http://jacksondunstan.com/articles/2416 We know that sending messages between ActionScript workers is slow, but how can we make it faster? Today’s article discusses an alternative approach to message passing that yields a 2.

        Original Site:http://jacksondunstan.com/articles/2416

        We know that sending messages between ActionScript workers is slow, but how can we make it faster? Today’s article discusses an alternative approach to message passing that yields a 2.5x speedup. Read on to learn about the approach and un-block your workers.

        The ActionScript Worker Message Passing is Slow article used the MessageChannel class to send messages between threads. This is the natural way to communicate between them that is shown in much of the documentation on inter-thread communication. It provides an easy method to communicate between workers that looks and feels exactly like the rest of the Flash API’s Event-based system. If you want it to be even easier, you can use MessageComm.

        However, there is a downside to all of this convenience. MessageChannel has a high level of performance overhead. The biggest area of performance overhead is that all messages are put into a queue and delivered once per frame, just like other events such as COMPLETE and ENTER_FRAME. This effectively limits the frequency at which your threads can communicate to once per frame. A slower frame rate will reduce your thread communication frequency, which may compound the low frame rate problem.

        Luckily, there is an alternative way to pass messages between workers. It’s actually contained in the setup code used to establish the MessageChannel link between the threads: setting shared properties. Instead of calling Worker.setSharedProperty just once before the worker thread is started, you instead set it every time you have a message to pass to another thread. Think of it like a drop box. One thread places a message there and the other thread periodically checks (with Worker.getSharedProperty) to see if there is a message. You can indicate that you’ve received the message by setting the shared property to null or some other value. You can respond by setting it to a response value.

        Since this method doesn’t rely on Flash’s once-per-frame events system, you can perform as many set or get operations as you want every frame. You can perform them in a tight loop (as in the following code) or perhaps just periodically. For example, your worker thread might check only once it’s exhausted the work it has already been told to do. As you’ll see, the checks are much cheaper than messages sent via MessageChannel, so you don’t need to be exceptionally frugal with them.

        Now to test the performance of MessageChannel versus this new setSharedProperty-based method. The following tiny example app simply sends 1000 messages back and forth between two threads. The message is always a single-character string, so there’s not much in the way of data copying going on. This allows us to purely measure the overhead of the message-passing system. For fairness, I’ve set the stage’s frame rate to 1000 and done nothing to impede it from reaching that maximum value. In reality, MessageChannel will likely run even slower depending on your actual frame rate.

        package
        {
        	import flash.display.Sprite;
        	import flash.events.Event;
        	import flash.utils.getTimer;
        	import flash.utils.ByteArray;
        	import flash.text.TextField;
        	import flash.text.TextFieldAutoSize;
        	import flash.system.Worker;
        	import flash.system.WorkerDomain;
        	import flash.system.WorkerState;
        	import flash.system.MessageChannel;
         
        	/**
        	* Test to show the speed of passing messages between workers
        	* @author Jackson Dunstan (JacksonDunstan.com)
        	*/
        	public class MessagePassingTest extends Sprite
        	{
        	/** Output logger */
        	private var logger:TextField = new TextField();
         
        	/**
        	* Log a CSV row
        	* @param cols Columns of the row
        	*/
        	private function row(...cols): void
        	{
        	logger.appendText(cols.join(",")+"\n");
        	}
         
        	/** Message channel from the main thread to the worker thread */
        	private var mainToWorker:MessageChannel;
         
        	/** Message channel from the worker thread to the main thread */
        	private var workerToMain:MessageChannel;
         
        	/** The worker thread (main thread only) */
        	private var worker:Worker;
         
        	/** Number of messages to send back and forth in the test */
        	private var REPS:int = 1000;
         
        	/** Time before the message passing test started */
        	private var beforeTime:int;
         
        	/** Current message index */
        	private var cur:int;
         
        	/**
        	* Start the app in main thread or worker thread mode
        	*/
        	public function MessagePassingTest()
        	{
        	// Setup the logger
        	logger.autoSize = TextFieldAutoSize.LEFT;
        	addChild(logger);
         
        	// If this is the main SWF, start the main thread
        	if (Worker.current.isPrimordial)
        	{
        	startMainThread();
        	}
        	// If this is the worker thread SWF, start the worker thread
        	else
        	{
        	startWorkerThread();
        	}
        	}
         
        	/**
        	* Start the main thread
        	*/
        	private function startMainThread(): void
        	{
        	// Try to get the best framerate possible
        	stage.frameRate = 1000;
         
        	// Create the worker from our own SWF bytes
        	worker = WorkerDomain.current.createWorker(this.loaderInfo.bytes);
         
        	// Create a message channel to send to the worker thread
        	mainToWorker = Worker.current.createMessageChannel(worker);
        	worker.setSharedProperty("mainToWorker", mainToWorker);
         
        	// Create a message channel to receive from the worker thread
        	workerToMain = worker.createMessageChannel(Worker.current);
        	workerToMain.addEventListener(Event.CHANNEL_MESSAGE, onWorkerToMainDirect);
        	worker.setSharedProperty("workerToMain", workerToMain);
         
        	// Start the worker
        	worker.start();
         
        	// Begin the test where we use MessageChannel to send messages
        	// between the threads by sending the first message
        	beforeTime = getTimer();
        	mainToWorker.send("1");
        	}
         
        	/**
        	* Start the worker thread
        	*/
        	private function startWorkerThread(): void
        	{
        	// Get the message channels the main thread set up for communication
        	// between the threads
        	mainToWorker = Worker.current.getSharedProperty("mainToWorker");
        	workerToMain = Worker.current.getSharedProperty("workerToMain");
        	mainToWorker.addEventListener(Event.CHANNEL_MESSAGE, onMainToWorker);
        	}
         
        	/**
        	* Callback for when a message has been received from the main thread to
        	* the worker thread on a MessageChannel
        	* @param ev CHANNEL_MESSAGE event
        	*/
        	private function onMainToWorker(ev:Event): void
        	{
        	// Record the message and send a response
        	cur++;
        	workerToMain.send("1");
         
        	// If this was the last message, prepare for the next test where the
        	// two threads communicate with shared properties
        	if (cur == REPS)
        	{
        	// We receive "1" on our own Thread (the worker thread) and send
        	// "2" in response
        	setSharedPropertyTest(Worker.current, "1", "2", false);
        	}
        	}
         
        	/**
        	* Callback for when the worker thread sends a message to the main thread
        	* via a MessageChannel
        	* @param ev CHANNEL_MESSAGE event
        	*/
        	private function onWorkerToMainDirect(ev:Event): void
        	{
        	// Record the message and show progress (this version is slow)
        	cur++;
        	logger.text = "MessageChannel: " + cur + " / " + REPS;
         
        	// If this wasn't the last message, send another message to the
        	// worker thread
        	if (cur < REPS)
        	{
        	mainToWorker.send("1");
        	return;
        	}
         
        	// The MessageChannel test is done. Record the time it took.
        	var afterTime:int = getTimer();
        	var messageChannelTime:int = afterTime - beforeTime;
         
        	// Run the setSharedProperty test where the two threads communicate
        	// by directly setting shared properties on the worker thread. The
        	// main thread receives "2", sends "1", and is responsible for
        	// starting the process with an initial "1" message.
        	beforeTime = getTimer();
        	setSharedPropertyTest(worker, "2", "1", true);
        	afterTime = getTimer();
        	var setSharedPropertyTime:int = afterTime - beforeTime;
         
        	// Clear the logger and show the results instead
        	logger.text = "";
        	row("Type", "Time", "Messages/sec");
        	var messagesPerSecond:Number = messageChannelTime/Number(REPS);
        	row("MessageChannel", messageChannelTime, messagesPerSecond);
        	messagesPerSecond = setSharedPropertyTime/Number(REPS);
        	row("setSharedProperty", setSharedPropertyTime, messagesPerSecond);
        	}
         
        	/**
        	* Perform the setSharedProperty test where the two threads communicate
        	* by directly setting shared properties on the worker thread
        	* @param worker Worker the shared properties are set on
        	* @param inMessage Expected message this thread receives from the other
        	* @param outMessage Message to send to the other thread
        	* @param sendInitial If an initial message should be sent
        	*/
        	private function setSharedPropertyTest(
        	worker:Worker,
        	inMsg:String,
        	outMsg:String,
        	sendInitial:Boolean
        	): void
        	{
        	// Reset the count from the first test
        	cur = 0;
         
        	// Optionally send an initial outgoing message to start the process
        	if (sendInitial)
        	{
        	worker.setSharedProperty("message", outMsg);
        	}
         
        	// Send messages until we've hit the limit
        	while (cur < REPS)
        	{
        	// Check to see if the shared property is the incoming message
        	if (worker.getSharedProperty("message") == inMsg)
        	{
        	// Record the message and send a response by setting the
        	// shared property to the outgoing message
        	cur++;
        	worker.setSharedProperty("message", outMsg);
        	}
        	}
        	}
        	}
        } 
        


        Run the test

        I ran this test in the following environment:

      1. Release version of Flash Player 11.9.900.117
      2. 2.3 Ghz Intel Core i7
      3. Mac OS X 10.9.0
      4. Google Chrome 30.0.1599.101
      5. ASC 2.0.0 build 353981 (-debug=false -verbose-stacktraces=false -inline -optimize=true)
      6. And here are the results I got:

        Type Time Messages/sec
        MessageChannel 153 6.53
        setSharedProperty

        The results show the setSharedProperty approach at 2.5x faster than the MessageChannel approach. Clearly, you’ll want to avoid MessageChannel for any high-frequency communication between threads as a major speedup is not that hard to realize by simply using the dropbox-style approach with setSharedProperty.

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        SendActionScriptWorkerMessages2.5xFaster

        SendActionScriptWorkerMessages2.5xFaster:Original Site:http://jacksondunstan.com/articles/2416 We know that sending messages between ActionScript workers is slow, but how can we make it faster Todays article discusses an alternative approach to message passing that yields a 2.
        推薦度:
        標簽: send worker 2.5
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 美女视频黄的全免费视频| 毛片免费全部播放无码| 又粗又黄又猛又爽大片免费 | 香蕉成人免费看片视频app下载| 亚洲一区二区三区国产精品| 性生大片视频免费观看一级| 国产亚洲精久久久久久无码77777 国产亚洲精品成人AA片新蒲金 | 一级黄色免费网站| 国产亚洲精品看片在线观看| 国产在线播放线91免费| 亚洲va无码手机在线电影| 最近免费中文字幕高清大全| 亚洲一欧洲中文字幕在线| 岛国大片免费在线观看| 美女黄频a美女大全免费皮| 国产成人精品久久亚洲| 久久免费视频99| 久久狠狠爱亚洲综合影院| 青青草国产免费久久久91| 特级毛片爽www免费版| 亚洲AV永久无码精品| 无码精品A∨在线观看免费| 亚洲日韩乱码中文字幕| 亚洲国产成人久久精品99 | 国产黄在线观看免费观看不卡| 国产成人A人亚洲精品无码| 免费观看黄色的网站| 美女扒开尿口给男人爽免费视频| 国产精品V亚洲精品V日韩精品| 99久久久国产精品免费牛牛 | 国产真人无码作爱免费视频| 亚洲国产视频一区| 又粗又大又硬又爽的免费视频| 久久久久久久99精品免费| 亚洲另类无码专区丝袜| 亚洲中文字幕无码中文字在线| 24小时日本电影免费看| 无码精品人妻一区二区三区免费| 亚洲一区二区三区电影| 免费a级毛片无码av| 免费人成视频在线观看网站|