NerdOnline
NerdOnline

Reputation: 153

Optimizing performance of array incrementation

regarding performance with java code: which action would be better and why?:

given the following array for example:

int[] arr = new int[n];//n is a random int

Which method would you choose to increment parts of arr for the best speed performance?

arr[i++] = x;//x and i are random ints

or

arr[i] = x;
++i;

Upvotes: 0

Views: 54

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85799

I would forget about performance in this one and choose the first because it's easier to read, understand and maintain than the other.

Why to forget about performance? Because this is a premature optimization and will save nanoseconds (if any). Since there's no great performance improvement when choosing between any of these, then look at other factors.

Upvotes: 5

Related Questions