Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #1 
Hey all. It's been a very long time since I posted here. I miss the activity on this forum so though I'd post some interesting results I found recently whilst doing optimisation tests.

I wanted to find out if a for loop or a foreach loop is faster.
The results below show that a for loop runs faster overall compared to a foreach loop. The lower the dot on the screen, the faster the operation time.

For loop vs Foreach loop.png 
Here's the code I used to time the operations:

for (int i = 0; i < m_fChartHolderWidth; i++)
{
for (int j = 0; j < actions.Length; j++)
{
stopWatch.Reset();
stopWatch.Start();

//call the function we want to time
actions[j]();

stopWatch.Stop();
PlotPoint(i, stopWatch.ElapsedMilliseconds, m_PointColors[j]);

m_lTotalTimes[j] += stopWatch.ElapsedMilliseconds;
m_lAvgTimes[j] = m_lTotalTimes[j] / (i + 1);

}

UpdateStatsText();
m_iNOfTests += 1;

//so that the point gets drawn on the screen
yield return new WaitForEndOfFrame();
}

And here's the 2 functions I tested. The for loop:

private void ForLoopOperation()
{
int[] arrayOfInts = new int[10000000];
for (int j = 0; j < arrayOfInts.Length; j++)
{

}
}


And the foreach loop:

private void ForEachLoopOperation()
{
int[] arrayOfInts = new int[10000000];
foreach (int number in arrayOfInts)
{

}
}


So perhaps the learning point here is; if you're wanting to squeeze more performance out of your code, then actively use for loops in exchange foreach loops.

It'll be good to hear what you guys think or if I've completely missed something out.

Happy Developing 😉
Martin


0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #2 
 
Quote:
What is the time for the "for loop" if you put
int number = arrayOfInts[j];
within?
 
 
Interesting. I added the int iNumber assignment below:
 
private void ForLoopOperation()
    {
        int[] arrayOfInts = new int[10000000];
        for (int j = 0; j < arrayOfInts.Length; j++)
        {
            int iNumber = arrayOfInts[j];
        }
    }
 
    private void ForEachLoopOperation()
    {
        int[] arrayOfInts = new int[10000000];
        foreach (int number in arrayOfInts)
        {
            int iNumber = number;
        }
    }
 
 
And the results were that the foreach loop is faster. Im guessing because the int already gets to the 'number' variable in the head of the loop.
 
For loop vs Foreach loop - 2.jpg 

0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #3 
Quote:
Originally Posted by "DaDonik, post: 6001160, member: 313815"
Try this instead. Should be what the foreach loop is doing internally.
 
 
    private void ForLoopOperation()
    {
        int[] arrayOfInts = new int[10000000];
        int length = arrayOfInts.Length;
        for (int j = 0; j < length; j++)
        {
            int iNumber = arrayOfInts[j];
        }
    }
  
Yeah I gave that try too. Looks like it makes very little difference on the for loop:
 
For loop vs Foreach loop - 3.jpg 
Here's the updated code I tested:

private void ForLoopOperation()
{
int[] arrayOfInts = new int[10000000];
for (int j = 0; j < arrayOfInts.Length; j++)
{
int iNumber = arrayOfInts[j];
}
}

private void ForEachLoopOperation()
{
int[] arrayOfInts = new int[10000000];
foreach (int number in arrayOfInts)
{
int iNumber = number;
}
}

private void ForLoopWithCachedLengthOperation()
{
int[] arrayOfInts = new int[10000000];
int iLength = arrayOfInts.Length;
for (int j = 0; j < iLength; j++)
{
int iNumber = arrayOfInts[j];
}
}


0
ajwad_imran

Avatar / Picture

Member
Registered:
Posts: 42
Reply with quote  #4 

Hey Martin,

 

Good to see you back man!

 

As far I know the for loop is faster than foreach loop if you only access the array/collection element once.

 

Best,

Ajwad


__________________

Checkout my Games on GP:

https://play.google.com/store/apps/developer?id=TnTn

0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #5 
Quote:
Originally Posted by ajwad_imran

Hey Martin,
Good to see you back man!
As far I know the for loop is faster than foreach loop if you only access the array/collection element once.



Hey Ajwad. Good to hear from you 😉

Yeah it looks like the for loop is faster. I have to do a few more tests on this one. I'm also planning to do a more of these tests for things like arrays vs lists, string comparisons, and more. Will post them here.

Chat soon man
0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.