i written code using for loop in 2 ways like below
first way
for(int i=0;i<myList.Items.Count;i++)
{
.....
.....
.....
}
second way
int itemsCount = myList.Items.Count;
for(int i=0;i<itemsCount;i++)
{
.....
.....
.....
}
in my view
first way is better even though each and every incremnt of i value its checking
(i< myList.Items.Count)
because at runtime it simply replace myList.Items.Count with memeroy refrence
so according to mine if i taken a new local variable to assign count of items its just a wastage of memory.
i want to know how it acts when i written code like above and tel me which way is more better in the view of performance aspect
I believe the first way is (in some compilers) the more optimal way in some circumstances.
You do not state a language, but it could be c# – so I will answer for that compiler. In C# I believe the former way of running is faster as it allows the compiler to ditch array bounds checking if you are accessing members of items in your loop. If you use an itemsCount variable it becomes more complicated for the compiler to track that the variable used in the loop has a safe value. This is only the case for a locally scoped array.
That argument may hold for other languages like Java that might use a similar looking loop.
Here is another question that addresses this question with for-loops in C#.
I would also add (following the comment from @Evan on the question) that optimization like this is seldom necessary, unless you have a specific need for a fast section of code. Write what is the most readable and less likely to cause issues first, then optimize where necessary.