Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your test isn't really being fair to the property-based versions. The JIT is smart enough to inline simple properties so that they have a runtime performance equivalent to that of direct field access, but it doesn't seem smart enough (today) to detect when the properties access constant values.</p> <p>In your example, the entire loop body of the field access version is optimized away, becoming just:</p> <pre><code>for (int i = 0; i &lt; loopCount; i++) 00000025 xor eax,eax 00000027 inc eax 00000028 cmp eax,989680h 0000002d jl 00000027 } </code></pre> <p>whereas the second version, is actually performing the floating point division on each iteration:</p> <pre><code>for (int i = 0; i &lt; loopCount; i++) 00000094 xor eax,eax 00000096 fld dword ptr ds:[01300210h] 0000009c fdiv qword ptr ds:[01300218h] 000000a2 fstp st(0) 000000a4 inc eax 000000a5 cmp eax,989680h 000000aa jl 00000096 } </code></pre> <p>Making just two small changes to your application to make it more realistic makes the two operations practically identical in performance.</p> <p>First, randomize the input values so that they aren't constants and the JIT isn't smart enough to remove the division entirely.</p> <p>Change from:</p> <pre><code>Point point = new Point(12.0, 123.5, 0.123); </code></pre> <p>to:</p> <pre><code>Random r = new Random(); Point point = new Point(r.NextDouble(), r.NextDouble(), r.NextDouble()); </code></pre> <p>Secondly, ensure that the results of each loop iteration are used somewhere:</p> <p>Before each loop, set calculatedValue = 0 so they both start at the same point. After each loop call Console.WriteLine(calculatedValue.ToString()) to make sure that the result is "used" so the compiler doesn't optimize it away. Finally, change the body of the loop from "calculatedValue = ..." to "calculatedValue += ..." so that each iteration is used.</p> <p>On my machine, these changes (with a release build) yield the following results:</p> <pre><code>Direct field access: 133 Property access: 133 Total difference: 0 Average difference: 0 </code></pre> <p>Just as we expect, the x86 for each of these modified loops is identical (except for the loop address)</p> <pre><code>000000dd xor eax,eax 000000df fld qword ptr [esp+20h] 000000e3 fmul qword ptr [esp+28h] 000000e7 fdiv qword ptr [esp+30h] 000000eb fstp st(0) 000000ed inc eax 000000ee cmp eax,989680h 000000f3 jl 000000DF (This loop address is the only difference) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload