如何判断lua封闭和lua协程的权衡?(当两者都可以执行相同的任务时) [英] how to judge of the trade-off of lua closure and lua coroutine?(when both of them can perform the same task)

查看:137
本文介绍了如何判断lua封闭和lua协程的权衡?(当两者都可以执行相同的任务时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ps:更不用说完成同一任务的闭包实现的代码复杂性了.

ps:let alone the code complexity of closure implementation of the same task.

推荐答案

闭包的内存开销将小于协程的内存开销(除非您在其中有很多个"upvalues"封闭,而协程中没有).调用闭包的时间开销也可以忽略不计,而调用协程的开销很小.从我所见,Lua在协程开关方面做得很好,但是如果性能很重要,并且您可以选择不使用协程,则应该探索该选项.

The memory overhead for a closure will be less than for a coroutine (unless you've got a lot of "upvalues" in the closure, and none in the coroutine). Also the time overhead for invoking the closure is negligible, whereas there is some small overhead for invoking the coroutine. From what I've seen, Lua does a pretty good job with coroutine switches, but if performance matters and you have the option not to use a coroutine, you should explore that option.

如果您想自己进行基准测试,请使用Lua中的此功能或其他功能:

If you want to do benchmarks yourself, for this or anything else in Lua:

您使用collectgarbage("collect");collectgarbage("count")报告所有不可回收的内存的大小. (您可能想几次收集",而不仅仅是一次.)在创建某种东西(一个闭包,一个协程)之前和之后都要这样做,以了解它消耗了多少大小.

You use collectgarbage("collect");collectgarbage("count") to report the size of all non-garbage-collectable memory. (You may want to do "collect" a few times, not just one.) Do that before and after creating something (a closure, a coroutine) to know how much size it consumes.

您使用os.clock()计时.

另请参阅在Lua中进行编程关于概要分析.

See also Programming in Lua on profiling.

这篇关于如何判断lua封闭和lua协程的权衡?(当两者都可以执行相同的任务时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆