how to judge of the trade-off of lua closure and lua coroutine?(when both of them can perform the same task)

霸气de小男生 提交于 2019-12-11 10:23:32

问题


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


回答1:


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.

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

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.

You use os.clock() to time things.

See also Programming in Lua on profiling.



来源:https://stackoverflow.com/questions/2092910/how-to-judge-of-the-trade-off-of-lua-closure-and-lua-coroutinewhen-both-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!