Is bool check faster than null check?

前端 未结 3 658
暗喜
暗喜 2021-01-18 22:23

E.g. do I need to extract

bool xIsNull = x == null

from the loop where I check x == null?

As I know if (a == tr

3条回答
  •  难免孤独
    2021-01-18 22:49

    So with modern compilation and hardware optimizations I doubt there would be enough performance difference to make enough difference to worry about (if any).

    Part of the point higher level languages like C# is to take these insignificant optimization details out of the hands of app devs and leave them to compiler devs who will do it much better and free up app devs to make their decisions off of readability/maintainablity and spend more time on high level algorithmic efficiency rather than the low level stuff. If you are having performance issues this is probably the least of your worries.

    Bottom line I would recommend using whatever you feels makes your code most readable.

提交回复
热议问题