Note that there are some explanatory texts on larger screens.

plurals
  1. POc# assignment performance with different ways
    primarykey
    data
    text
    <p>everyone,recently i was debugging a program for improve performance.i notice a interest thing about assignment's performance.the below code is my test code.</p> <p>CODE A</p> <pre><code>public class Word{....} public class Chunk { private Word[] _items; private int _size; public Chunk() { _items = new Word[3]; } public void Add(Word word) { _items[_size++] = word; } } </code></pre> <p>main</p> <pre><code> Chunk chunk = new Chunk(); for (int i = 0; i &lt; 3; i++) { chunk.Add(new Word() { });// } </code></pre> <p>CODE B</p> <pre><code>public class Chunk { private Word[] _items; private int _size; public Chunk() { _items = new Word[3]; } public Word[] Words { get { return _items; } } public int Size { get{return _size;} set{_size=value;} } } </code></pre> <p>main</p> <pre><code> Chunk chunk = new Chunk(); for (int i = 0; i &lt; 3; i++) { chunk.Words[i] = new Word() { }; chunk.Size + = 1; } </code></pre> <p>in my test with visual studio'profiling tool,calling the main method 32000 times,that performance shows the CODE B FASTER than the CODE A.why the CODE B faster than the CODE A?who can give me a suggestion? thanks</p> <p>update:sorry,i forgot increase _size code in the CODE B,i have updated my CODE B</p> <p>update: @Shiv Kuma Yes, code A is similar with Code B in the situation of 30000 call times. I tested the 700K file and the code can be called 29000 times or so. Meanwhile, code B is 100 millisecond faster than Code A, and actually Code B is much better during the real segment. Here one more thing I’m wondering is why Code B is faster than Code A even for the same assignment? Anyway, thanks for you reply.</p>
    singulars
    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.
 

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