Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How expensive is a for loop every frame?

Discussion in 'Scripting' started by AnthonySturdy, Jun 1, 2016.

  1. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Hi, I was just wondering if putting a for loop in the Update() would impact my performance much? The loop runs 6 times and is used to check something.

    Thanks.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Not particularly expensive. Especially if it's only 6 iterations.

    It's a for loop and not a foreach right? And you're not generating any garbage (temporary objects) through boxing or the 'new BlahBlah' operator? Because that can hurt performance over the long run. But it's technically not the loop itself causing the performance issue.
     
  3. AnthonySturdy

    AnthonySturdy

    Joined:
    Feb 24, 2014
    Posts:
    55
    Cheers for the reply. Nah I don't create anything in the loop. There are 2 if statements and it changes some values in another script.
     
  4. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    AnthonySturdy likes this.