连续调用多少次才算太多?

发布于 2024-12-06 07:45:02 字数 1087 浏览 1 评论 0原文

我正在调试一些代码,并看到一个循环,其中调度事件并对每条记录进行远程调用。

一切都工作正常,直到有数百条记录(确切地说是 700 条)为止。这会让 Flash 播放器卡顿吗?我应该转移到排队系统吗?多少条记录太多?

感谢您提供任何有用的提示。

以下是发送连续调用的更新方法:

var counter:int= 0;
        for each ( var item:ObjectVo in itemColl)
        {
            counter = counter + 1;
            var evt:DataValidationEvent = new DataValidationEvent();
            evt.myItem = item;
            evt.eventType = DataValidationEvent.EVENT_TYPE_PASTE_FROM_EXCEL
            if( counter == ( itemColl.length ) ){
                evt.isLastCall=true;
            }else{
                evt.isLastCall=false;
            }
            evt.dispatch();
        }

这是事件处理程序。在“isLastCall”设置为 true 后,它仅被调用一次。

private function addItemsFromList( item:itemVo ):void{
            var myObj:ObjVo = new ObjVo();
            myObj.description = item.description;
            myObj.rule = item.objRule;

            this.itemsColl.addItem( myObj );
            this.itemsColl.itemUpdated( myObj );
            this.itemsColl.refresh();
        }

I'm debugging some code and see a loop where an event is dispatched and a remote call is made for every record.

Everything was working fine until there were several hundred records (700) to be a exact. Is that going to make the flash player chug? Should I move to a queued system? How many records is too many?

Thanks for any helpful tips.

Here is the updated method that sends the successive calls out:

var counter:int= 0;
        for each ( var item:ObjectVo in itemColl)
        {
            counter = counter + 1;
            var evt:DataValidationEvent = new DataValidationEvent();
            evt.myItem = item;
            evt.eventType = DataValidationEvent.EVENT_TYPE_PASTE_FROM_EXCEL
            if( counter == ( itemColl.length ) ){
                evt.isLastCall=true;
            }else{
                evt.isLastCall=false;
            }
            evt.dispatch();
        }

This is the event handler. It gets called only once, after 'isLastCall' is set to true.

private function addItemsFromList( item:itemVo ):void{
            var myObj:ObjVo = new ObjVo();
            myObj.description = item.description;
            myObj.rule = item.objRule;

            this.itemsColl.addItem( myObj );
            this.itemsColl.itemUpdated( myObj );
            this.itemsColl.refresh();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2024-12-13 07:45:02

如果您要进行数百个远程调用,那么也可能是服务器放弃了您。我想知道 Flash Player 是否是这里真正的瓶颈。 AVM2 可以在几毫秒内发起数千次调用。

我能用这些小信息建议的是测量循环完成所需的时间,如果确实是那个循环需要时间,请尝试通过有选择地注释掉循环体的部分来找到最昂贵的位。

编辑:

优秀的补间引擎能够以 60FPS 的速度对 25K 对象上的少数属性进行动画处理,远高于每秒一百万次调用(以及 60 帧的渲染)。您的代码一定有问题。

代码缓慢的地方:

  • 实例化比简单调用昂贵几个数量级。在性能关键场景中(实际上不是),使用对象池或将自己限制为基元会更好。
  • 调用函数对象而不是方法也很慢——由于这个问题和上一个问题,常用的事件机制慢得要命。还是每帧700应该没问题。
  • 最后一点似乎是最令人不安的。在不知道什么处理事件的情况下,很难知道,但最坏的情况是调度每个事件,您会处理所有项目,这会给您带来 O(N^2) 运行时成本并导致 N*(N+1) 单次迭代的成本,如果单次迭代意味着重新绘制网格,那么这真的可能会出错。无论如何,我认为在整个循环之后调度一个事件就足够了。

If you're making several hundred remote calls, it might just as well be the server, that's giving up on you. I'd be wondering if Flash Player is the real bottle-neck here. The AVM2 can make a few thousand calls within a few milliseconds.

All I can advise with this little information is to measure the time your loop takes to complete and if it's really that loop that takes time, try to find the most expensive bits by selectively commenting out parts of the loop body.

edit:

Good tweening engines peak at animating a handful of properties on 25K objects at 60FPS, which is well over a million calls (and rendering of 60 frames) per second. Something must be quite wrong with your code.

Things that are slow about your code:

  • instantiation is orders of magnitude more expensive than simple calls. In performance critical scenarios (which this one actually isn't), you're far better off with object pooling or restricting yourself to primitives.
  • calling function objects instead of methods is also slow - as a consequence of this and the previous problem, the commonly practiced event mechanisms are slow as hell. Still 700 per frame should be no problem.
  • the last bit seems the most disturbing one. without knowing what handles the event, it's hard to know, but worst case with every event dispatched, you process all the items, which gives you O(N^2) runtime cost and results in N*(N+1) the cost of a single iteration and if a single iteration means redrawing the grid, then this can really go wrong. In any case, I think dispatching one event after the whole loop should suffice.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文