Is 'yield return' slower than “old school” return?

后端 未结 5 1513
滥情空心
滥情空心 2020-12-19 10:01

I\'m doing some tests about yield return perfomance, and I found that it is slower than normal return.

I tested value variables (int, double, etc.) and some referenc

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-19 10:53

    I used yield return to give me results from an algorithm. Every result is based on previous result, but I don't need all all of them. I used foreach with yield return to inspect each result and break the foreach loop if I get a result meet my requirement.

    The algorithm was decent complex, so I think there was some decent work involved for saving states between each yield returns.

    I noticed it was 3%-5% percentage slower than traditional return, but the improvement I get form not needing to generate all results is much much bigger than the loss of performance.

提交回复
热议问题