Would this small optimiziation to the corefx generic CopyTo method of the Stack make a difference?

Because I was bored and recently got a new keyboard I decided to practice my typing and chose the Stack class of the .Net Core framework as a template.

I tried to understand how internals of the class work at the same time, because I always wanted to know how some classes are implemented.

When typing though I noticed the following snippet:

 int srcIndex = 0; int dstIndex = arrayIndex + _size; for (int i = 0; i < _size; i++) array[--dstIndex] = _array[srcIndex++]; 

Basically it is copying the Stack into a as an parameter passed generic array.

But I noticed that srcIndex is initialized to the same value the index i in the for loop is. So wouldn’t this be a small optimization saving the need to increment an integer after each iteration:

 int srcIndex = 0; int dstIndex = arrayIndex + _size; while(srcIndex < _size) { array[--dstIndex] = _array[srcIndex++]; } 

I am aware that this is not going to make (probably even measurable) difference on small Stacks, but assuming the Stack has multiple million records, would this be worth fixing?

submitted by /u/Encore-
[link] [comments]

Leave a Reply