Search Unity

Performance question - "for loop twice" VS "for loop with modulus + division"

Discussion in 'Scripting' started by Rocketballs, Mar 5, 2015.

  1. Rocketballs

    Rocketballs

    Joined:
    Sep 13, 2012
    Posts:
    23
    (I'm asking this out of curiosity. It all began with a thought experiment kinda o_O)
    (I don't know much about performance tests and such, so I figure I'd be better asking someone who does)

    Which one performs faster?

    Code (CSharp):
    1. int x = 100;
    2. int y = 100;
    3. int area = x * y;
    4. float inverseY = 1f / y;
    5.  
    6. -----------------------------------------------------------
    7. for(int i = 0; i < area; i++)
    8. {
    9.     DoSomething( i % y, i / y);
    10. }
    11. -----------------------------------------------------------
    12. for(int i = 0; i < area; i++)
    13. {
    14.     DoSomething( i % y, Mathf.FloorToInt(i * inverseY));
    15. }
    16. -----------------------------------------------------------
    17. for(int a = 0; a < x; a++)
    18. {
    19.     for(int b = 0; b < y; b++)
    20.     {
    21.         DoSomething( a, b);
    22.     }
    23. }
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    244
    Put this in a key press:
    Code (CSharp):
    1. float num = Time.timeSinceLevelLoad;
    2. for(int j = 0; j < 1000; j++){
    3. //your code here
    4. }
    5. Debug.Log(Time.timeSinceLevelLoad-num);
    So essentially it will tell you how long it took to run your code 1000 times. You can adjust the 1000 number, the higher the amount, the bigger the difference you'll see between different types of code.
     
  3. Rocketballs

    Rocketballs

    Joined:
    Sep 13, 2012
    Posts:
    23
    Thanks :)
    I wasn't expecting it to be so easy. I thought something like the profiler would be needed.
    I'll post my findings here when I'm done.
     
  4. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    I can guarantee that the Mathf loop will be the slowest one, followed by the loop with the division. ;)
    Math functions and divisions are costy and loops generally have no overhead at all.
     
  5. Rocketballs

    Rocketballs

    Joined:
    Sep 13, 2012
    Posts:
    23
    Code (CSharp):
    1. Iterations   -   1000            10000        100000     1000000
    2.                                        
    3. Division     - 0.0774355s      0.775082s     7.79576s      78s*
    4.  
    5. FloorToInt   - 0.2430296s      2.414379s     24.1166s      241s*
    6.  
    7. Loops        - 0.0473442s      0.476334s     4.70244s      47.26746s
    * - These values were estimated.
    Each iteration ran 10K times, so 1K was 10M, 10K 100M, etc
    Loops are indeed the fastest options followed by division.

    You win sir :D
     
    Sharp-Development and CDMcGwire like this.
  6. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    This is good to know. Thanks for sharing!