-1

I have a list contains 100 sprites, in some condition i want to hide the first 50 and another condition i want to hide the last 50.

i'm using FOR LOOP:

        for (int i = 0; i < 50; i++) {
            spritelist [i].SetActive (false);
        }

I don't want my code to loop for a 50 times, i'm afraid this may lead to lag of performance, but i'm thinking there is a better way like this:

        spritelist[0, 50].SetActive (false);

IS THIS POSSIBLE, THANKS IN ADVANCE.

Botros
  • 3
  • 2
  • 1
    *somewhere* some code needs to loop through these 50 sprites, whether that is your own `for` loop or some "[ForEach](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach)" method that does it for you – Hans Kesting May 06 '19 at 15:11
  • 3
    If you have to do the same thing to fifty of something there's going to be a loop. So it's probably not going to matter. In this scenario I recommend whatever makes the code easier to read. The little things we try to optimize often don't make a difference, but the code we have a hard time reading is what really slows us down. – Scott Hannen May 06 '19 at 15:11
  • An array in essence is set of a memory cells. Modern CPUs don't have instructions which would allow to make some action over multiple memory cells at once (the exception is SIMD instructions but they're limited by size), so you cannot expect that there is some magical thing between your code and CPU instructions which would allow that, regardless of how it might look in the C# code - finally it has to be unfolded into 50 iterations. But from readability perspective you can use linq. – Dmytro Mukalov May 06 '19 at 15:27

1 Answers1

3

i'm afraid this may lead to lag of performance

This is called premature optimization. You are worrying about performance but you have done nothing to actually test whether this fear is justified. Now you are considering taking action to correct a problem that you have not verified actually exists.

How can we test? Let's consider a very simple program :

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace ConsoleApp3
{
    class Program
    {
        class Sprite
        {
            private bool _isActive;
            public void SetActive(bool isActive)
            {
                _isActive = isActive;
            }
        }
        static void Main(string[] args)
        {
            List<Sprite> sl = new List<Sprite>();
            for(int i = 0; i<100; i++)
            {
                sl.Add(new Sprite());
            }

            Stopwatch spw = new Stopwatch();
            spw.Restart();
            for (int i = 0; i < 50; i++)
            {
                sl[i].SetActive(false);
            }
            Console.Write(1e6 * spw.ElapsedTicks / TimeSpan.TicksPerSecond);
            Console.WriteLine(" microseconds");
            Console.ReadLine();

        }
    }
}

On my system (a five year-old i7) this produces a result typically between 20-30 microseconds. If you consider a target performance of 100fps, that's 10ms per frame, so this operation is consuming about 0.3% of your time budget per frame. Do you think that's worth worrying about? I don't. Just write the code so that it is readable and makes sense. Worry about performance when it becomes a problem.

J...
  • 30,968
  • 6
  • 66
  • 143