|
于 2013-11-4 22:08, Thijs Schreijer 写道:
It is really weird. I also got similar result running your code. After some exploring, I think I have got some points, but not quit clear. Well, although I don't know why, some experiments shows that the *main thread* runs slower than *user created thread(coroutines)*. Please someone would tell me the deep reason for that?Thanks for trying that and the hint, I get different results though; - coroutines consistently slower than the mainloop - corowatch (using debug hooks) consistently runs fastest Attached my code. Here's the output; C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.28079986572266 Coroutine: 0.27924041748047 Corowatch: 0.2574010848999 corowatch is -8% slower than unprotected coroutine is 0% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.28079986572266 Coroutine: 0.28704032897949 Corowatch: 0.26520118713379 corowatch is -8% slower than unprotected coroutine is -3% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.26832046508789 Coroutine: 0.28080024719238 Corowatch: 0.26052074432373 corowatch is -8% slower than unprotected coroutine is -5% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.27924003601074 Coroutine: 0.28391990661621 Corowatch: 0.26988162994385 corowatch is -5% slower than unprotected coroutine is -2% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.27923984527588 Coroutine: 0.29015998840332 Corowatch: 0.27144165039063 corowatch is -7% slower than unprotected coroutine is -4% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.2683198928833 Coroutine: 0.2808012008667 Corowatch: 0.26520023345947 corowatch is -6% slower than unprotected coroutine is -5% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.2776798248291 Coroutine: 0.2854808807373 Corowatch: 0.26988067626953 corowatch is -6% slower than unprotected coroutine is -3% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.27923984527588 Coroutine: 0.28860111236572 Corowatch: 0.26988048553467 corowatch is -7% slower than unprotected coroutine is -4% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples>performance.lua Mainloop : 0.28080158233643 Coroutine: 0.28547992706299 Corowatch: 0.26987991333008 corowatch is -6% slower than unprotected coroutine is -2% faster than the main loop C:\Users\Thijs\Dropbox\Lua projects\corowatch\examples> Well, forget about it. I bet we both went the wong direction. I ran that script from your original post this evening, just to find the result is slight different from yesterday's. Then I added some unrelated code at the beginning. I say it unrelated because it just prints some text to the screen. Surprisingly I found the result changed again. This time the execution time of the main loop and the "watched" coroutine became almost identical. Among several runs, the result showed some randomness - tha latter might be either slower or faster, but the average execution time diffence is near 0(and this is what I had expected at the very beginning). More experiments showed that if I insert unrelated codes randomly, the results could all be affected in somewhat random manner. You may try something similar to see whether it'll make some difference if you want. So I suppose the diffence is not due to whether it is the main thread or a coroutine, nor to whether it is "watched"(hooked). As I stated earlier, the perfomance *should* have little difference as long as the debug hook is installed but not triggered, according to my understanding of the source code of Lua. Probably the diffence in the experiments was kind of butterfly effect, caused by some unknown, subtle factors. I don't know. |