Performance overhead for properties in .NET

前端 未结 9 1956
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 01:43

I read somewhere that having public properties is preferable to having public members in a class.

  1. Is this only because of abstaraction and modularity? Are

9条回答
  •  孤街浪徒
    2020-12-10 02:11

    Running the test 20 times in a row, ensuring that JIT optimization is enabled in the Release build:

    Time for 1 and 2 : 47,66
    Time for 1 and 2 : 37,42
    Time for 1 and 2 : 25,36
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 27,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 26,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    Time for 1 and 2 : 25,25
    

    Yes, the JITter is that good at inlining property accessors. Perf is a non-issue and should never be considered.

提交回复
热议问题