Reputation: 3586
chunk_indices.AddRange(new int[6] { 0 + length, 1 + length, 2 + length, 3 + length, 2 + length, 1 + length });
It seems to run pretty slowly and takes about 1 microsecond which is a long time considering how many times I'm calling it, how can I make it faster?
Upvotes: 1
Views: 152
Reputation: 216343
Another way is Array.Copy like this
int[] source = new int[6] { 0 + length, 1 + length, 2 + length, 3 + length, 2 + length, 1 + length });
//
// Assuming the chunk_indices has 6 elements already allocated.
//
Array.Copy(source, chunk_indices, 6);
The internal implementation of AddRange calls Array.Copy or CopyTo normally. Therefore, it is impossible for AddRange to perform better than a plain Array.Copy.
Further research revelead another option, faster than previous one:
// Copy the first 24 bytes from source to chunk_indices
Buffer.BlockCopy(source, 0, chunk_indices, 0, 6 * sizeof(int));
Upvotes: 1
Reputation: 1
Generally AddRange method accepts parameter array so you can try
chunk_indices.AddRange(length, 1 + length, 2 + length, 3 + length, 2 + length, 1 + length);
Upvotes: 0
Reputation: 112682
AddRange
is handy if you have an array or another collection that you can transform into an array to start with; however, here you could call Add
repeatedly as well.
chunk_indices.Add(length);
chunk_indices.Add(length + 1);
chunk_indices.Add(length + 2);
chunk_indices.Add(length + 3);
chunk_indices.Add(length + 2);
chunk_indices.Add(length + 1);
Upvotes: 1