简体   繁体   中英

Is it worth to make an O(1) function a coroutine in Unity3d?

I have 2 functions, Foo() and Bar() .

Foo's time complexity is θ(1) , Bar's is θ(n²) , they execute one after the other respectively and Bar has no dependecies with Foo.

Is it worth to make Foo a coroutine to have some speedup?

Unity coroutines are still executed on main thread. One after another. Unless you are using something like "Thread Ninja - Multithread Coroutine" from asset store, you will not gain anything from coroutines.

Is it worth parallelizing? It depends. The trade-off here is between time performance and readibility / maintainability of your code.

I would only implement time performance improvements if necessary. First question you should ask, do you or the users notice a lag or bad performance? If no, then no need for optimizations.

Second step is to profile and measure. You should know which part of the code is critical. So for Foo() and Bar() you should measure how much time they take in a average and worst case. Then see how much time you can save. Then you need to decide is it worth it. ;)

Like mentioned in the comments the big O notation in general will not help you much there. Measure it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM