[转]Callback Strategies

本文通过测试AS3中不同回调方法的性能,包括Function、Runnable、Event及as3 signals库等,展示了各种方法在单个及多个监听器情况下的表现,并分析了其速度差异的原因。
[url]http://jacksondunstan.com/articles/573[/url]


I’ve previously covered ways of implementing in my article on Runnables (aka observers) which showed how to call back about 15 times faster than just using a Function object. There are still more ways to call back though and I didn’t cover them at the time. Today I’ll be adding to Function and Runnables by testing Event and the as3signals library by Robert Penner.


As you most certainly know, AS3′s ubiquitous Event class is a way of call back a whole collection of functions at the same time. These have numerous problems ranging from performance to object allocation and garbage collection to more stylistic concerns. This prompted Robert Penner to create the as3signals system as a replacement for Events. Today I will test both as3signals and Events against simple Vectors of Functions and Runnables. I will also test as3signals and Events against a single Function and a single Runnable. The purpose of this is to show how various levels of complexity and features affect performance. Clearly as3signals and Event are far more advanced than a single callback or a simple Vector of callbacks, but sometimes that’s all you need. That said, let’s take a look at the test app:

package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;

import org.osflash.signals.*;

[SWF(backgroundColor=0xEEEADB,frameRate=1000)]

/**
* A test of various callback techniques
* @author Jackson Dunstan
*/
public class CallbacksTest extends Sprite
{
public var signal:Signal;
public var funcs:Vector.<Function> = new Vector.<Function>();
public var runnables:Vector.<Runnable> = new Vector.<Runnable>();

public function CallbacksTest()
{
var logger:TextField = new TextField();
logger.autoSize = TextFieldAutoSize.LEFT;
addChild(logger);

this.signal = new Signal();

var i:int;

const FUNC_CALL_REPS:int = 1000000;
const NUM_LISTENERS:int = 10;
const EVENT_TYPE:String = "test";
var runnable:Runnable = new MyRunnable();
var func:Function = runnable.run;

// Single call

var beforeTime:int = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
func();
}
logger.appendText("Func call time: " + (getTimer()-beforeTime) + "\n");

beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
runnable.run();
}
logger.appendText("Runnable call time: " + (getTimer()-beforeTime) + "\n");

addEventListener(EVENT_TYPE, onEvent0);
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
dispatchEvent(new Event(EVENT_TYPE));
}
logger.appendText("Event (1 listener) time: " + (getTimer()-beforeTime) + "\n");
removeEventListener(EVENT_TYPE, onEvent0);

this.signal.add(onSignal0);
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
this.signal.dispatch();
}
logger.appendText("Signal (1 listener) time: " + (getTimer()-beforeTime) + "\n\n");
this.signal.remove(onSignal0);

// Calling a list

for (i = 0; i < NUM_LISTENERS; ++i)
{
this.funcs.push(func);
}
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
dispatchFuncs(this.funcs);
}
logger.appendText("Func call (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");

for (i = 0; i < NUM_LISTENERS; ++i)
{
this.runnables.push(runnable);
}
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
dispatchRunnables(this.runnables);
}
logger.appendText("Runnable call (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");

for (i = 0; i < NUM_LISTENERS; ++i)
{
addEventListener(EVENT_TYPE, this["onEvent"+i]);
}
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
dispatchEvent(new Event(EVENT_TYPE));
}
logger.appendText("Event (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");

for (i = 0; i < NUM_LISTENERS; ++i)
{
this.signal.add(this["onSignal"+i]);
}
beforeTime = getTimer();
for (i = 0; i < FUNC_CALL_REPS; ++i)
{
this.signal.dispatch();
}
logger.appendText("Signal (" + NUM_LISTENERS + " listeners) time: " + (getTimer()-beforeTime) + "\n");
}

private function dispatchFuncs(funcs:Vector.<Function>): void
{
var len:int = funcs.length;
for (var i:int = 0; i < len; ++i)
{
funcs[i]();
}
}

private function dispatchRunnables(runnables:Vector.<Runnable>): void
{
var len:int = runnables.length;
for (var i:int = 0; i < len; ++i)
{
Runnable(runnables[i]).run();
}
}

private function onEvent0(ev:Event): void {}
private function onEvent1(ev:Event): void {}
private function onEvent2(ev:Event): void {}
private function onEvent3(ev:Event): void {}
private function onEvent4(ev:Event): void {}
private function onEvent5(ev:Event): void {}
private function onEvent6(ev:Event): void {}
private function onEvent7(ev:Event): void {}
private function onEvent8(ev:Event): void {}
private function onEvent9(ev:Event): void {}

private function onSignal0(): void {}
private function onSignal1(): void {}
private function onSignal2(): void {}
private function onSignal3(): void {}
private function onSignal4(): void {}
private function onSignal5(): void {}
private function onSignal6(): void {}
private function onSignal7(): void {}
private function onSignal8(): void {}
private function onSignal9(): void {}
}
}
internal interface Runnable
{
function run(): void;
}
internal class MyRunnable implements Runnable
{
public function run(): void {}
}
Here are the results I get for the single listener versions:

ENVIRONMENT FUNC (1) RUNNABLE (1) EVENT (1) SIGNAL (1)
3.0 Ghz Intel Core 2 Duo, 4GB, Windows XP 67 4 1336 1412
2.0 Ghz Intel Core 2 Duo, 4GB, Mac OS X 10.5 90 7 6108 2275
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 82 6 5115 2057
And here are the results for the multiple-listener versions:

ENVIRONMENT FUNC (10) RUNNABLE (10) EVENT (10) SIGNAL (10)
3.0 Ghz Intel Core 2 Duo, 4GB, Windows XP 668 180 2877 2804
2.0 Ghz Intel Core 2 Duo, 4GB, Mac OS X 10.5 964 342 38643 4314
2.2 Ghz Intel Core 2 Duo, 2GB, Mac OS X 10.6 880 311 31512 3958
The above table shows that we don’t quite have a separation between the single listener tests and the multiple listener tests, which is rather disappointing. The single Runnable test is shockingly fast compared to as3signals (300x slower) and Event (300x slower on Windows, 600x slower on Mac). However, as pointed out above, these systems are much more complex and have many more features than a single callback. Event, for example, requires allocating a new Event object and supports such advanced features as bubbling, canceling, and multiple listening functions. This is also true in a comparison of a simple Function object to either the as3signals or Event system. So, assuming you need that extra power and can’t get by with just a simple callback, let’s move on to where as3signals and Event are more justified: multiple callbacks.

Here we see the winner again is a Vector of Runnables, which beats out second place Vector of Functions by about three-to-one. Then come Event and as3signals, which are practically the same speed on Windows, but as3signals is about 10x faster on Mac. This is good news for those desiring an arguably-cleaner interface than Event presents and also good news for those hoping for a speed boost by using as3signals on a Mac, but not Windows. On the contrary, it’s good news for those with significant investments in the Event system who are only targeting Windows performance: there’s no need to re-write using as3signals to get a speedup so long as you keep to Windows. You might, rather, re-write to use a Vector of Runnables though if you’re interested in a a 15x speedup on Windows and 15-100x speedup on Mac.

What you’d lose in the process of converting from Event or as3signals to a simpler approach like a Vector of Runnables is a lot of the sophistication of those approaches. You’ll lose the aforementioned bubbling and canceling, some safety regarding changes to the Vector during a dispatch operation, type safety in the case of as3signals, consistency with the Flash API in the case of the Event system, and likely many more niceties. What you gain is raw speed. Only you can choose the appropriate system for you application.

Tags: callbacks, events, functions, performance, runnables, signals
/opt/ros/foxy/include/rclcpp/create_service.hpp:43:3: error: no matching function for call to ‘rclcpp::AnyServiceCallback<std_srvs::srv::Trigger>::set(std::_Bind<void (LaserMappingNode::*(LaserMappingNode*, std::_Placeholder<1>, std::_Placeholder<2>))(std::shared_ptr<const std_srvs::srv::Trigger_Request_<std::allocator<void> > >, std::shared_ptr<std_srvs::srv::Trigger_Response_<std::allocator<void> > >)>)’ 43 | any_service_callback.set(std::forward<CallbackT>(callback)); | ^~~~~~~~~~~~~~~~~~~~ In file included from /opt/ros/foxy/include/rclcpp/service.hpp:28, from /opt/ros/foxy/include/rclcpp/callback_group.hpp:25, from /opt/ros/foxy/include/rclcpp/any_executable.hpp:20, from /opt/ros/foxy/include/rclcpp/memory_strategy.hpp:24, from /opt/ros/foxy/include/rclcpp/memory_strategies.hpp:18, from /opt/ros/foxy/include/rclcpp/executor_options.hpp:20, from /opt/ros/foxy/include/rclcpp/executor.hpp:33, from /opt/ros/foxy/include/rclcpp/executors/multi_threaded_executor.hpp:26, from /opt/ros/foxy/include/rclcpp/executors.hpp:21, from /opt/ros/foxy/include/rclcpp/rclcpp.hpp:146, from /home/liu/chapt7_ws/src/FAST_LIO/src/laserMapping.cpp:45: /opt/ros/foxy/include/rclcpp/any_service_callback.hpp:67:8: note: candidate: ‘template<class CallbackT, typename std::enable_if<rclcpp::function_traits::same_arguments<CallbackT, std::function<void(std::shared_ptr<std_srvs::srv::Trigger_Request_<std::allocator<void> > >, std::shared_ptr<std_srvs::srv::Trigger_Response_<std::allocator<void> > >)> >::value, void>::type* <anonymous> > void rclcpp::AnyServiceCallback<ServiceT>::set(CallbackT) [with CallbackT = CallbackT; typename std::enable_if<rclcpp::function_traits::same_arguments<CallbackT, std::function<void(std::shared_ptr<typename ServiceT::Request>, std::shared_ptr<typename ServiceT::Response>)> >::value>:
10-25
【无线传感器】使用 MATLAB和 XBee连续监控温度传感器无线网络研究(Matlab代码实现)内容概要:本文围绕使用MATLAB和XBee技术实现温度传感器无线网络的连续监控展开研究,介绍了如何构建无线传感网络系统,并利用MATLAB进行数据采集、处理与可视化分析。系统通过XBee模块实现传感器节点间的无线通信,实时传输温度数据至主机,MATLAB负责接收并处理数据,实现对环境温度的动态监测。文中详细阐述了硬件连接、通信协议配置、数据解析及软件编程实现过程,并提供了完整的MATLAB代码示例,便于读者复现和应用。该方案具有良好的扩展性和实用性,适用于远程环境监测场景。; 适合人群:具备一定MATLAB编程基础和无线通信基础知识的高校学生、科研人员及工程技术人员,尤其适合从事物联网、传感器网络相关项目开发的初学者与中级开发者。; 使用场景及目标:①实现基于XBee的无线温度传感网络搭建;②掌握MATLAB与无线模块的数据通信方法;③完成实时数据采集、处理与可视化;④为环境监测、工业测控等实际应用场景提供技术参考。; 阅读建议:建议读者结合文中提供的MATLAB代码与硬件连接图进行实践操作,先从简单的点对点通信入手,逐步扩展到多节点网络,同时可进一步探索数据滤波、异常检测、远程报警等功能的集成。
内容概要:本文系统讲解了边缘AI模型部署与优化的完整流程,涵盖核心挑战(算力、功耗、实时性、资源限制)与设计原则,详细对比主流边缘AI芯片平台(如ESP32-S3、RK3588、Jetson系列、Coral等)的性能参数与适用场景,并以RK3588部署YOLOv8为例,演示从PyTorch模型导出、ONNX换、RKNN量化到Tengine推理的全流程。文章重点介绍多维度优化策略,包括模型轻量化(结构选择、输入尺寸调整)、量化(INT8/FP16)、剪枝与蒸馏、算子融合、批处理、硬件加速预处理及DVFS动态调频等,显著提升帧率并降低功耗。通过三个实战案例验证优化效果,最后提供常见问题解决方案与未来技术趋势。; 适合人群:具备一定AI模型开发经验的工程师,尤其是从事边缘计算、嵌入式AI、计算机视觉应用研发的技术人员,工作年限建议1-5年;熟悉Python、C++及深度学习框架(如PyTorch、TensorFlow)者更佳。; 使用场景及目标:①在资源受限的边缘设备上高效部署AI模型;②实现高帧率与低功耗的双重优化目标;③掌握从芯片选型、模型换到系统级调优的全链路能力;④解决实际部署中的精度损失、内存溢出、NPU利用率低等问题。; 阅读建议:建议结合文中提供的代码实例与工具链(如RKNN Toolkit、Tengine、TensorRT)动手实践,重点关注量化校准、模型压缩与硬件协同优化环节,同时参考选型表格匹配具体应用场景,并利用功耗监测工具进行闭环调优。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值