Performance of struct field

Hey everyone.

I have a quick question about performance for my game.

I won’t bore you with the game logic, so I only want to know if there is a significant difference between 2 examples of code:

 for (int i = 0; i < 10000; i++)
        {

            exampleStruct[i] = new ExampleStruck("example" , 1);

        }

And:

for (int i = 0; i < 10000; i++)
        {

            exampleArray[i] = "example";

        }

Short explanation:
Let’s say I have a struct with 2 fields, string and int. every index i need to use the “new” approach for changing the values.
In the second way, I only assign an array with 1 value.
this is for Job threads, and I can use both technics with some adjustments.

Again, for using the second way I have to do changes to my code, I just want to know if it’s worth it, because the first way are more clearer.

Note: I do this every frame for the job handle.

So, does it have a major impact or it’s pretty much the same?

There will be a garbage saving if you initialised a new “struck” before the loop and overwrote it’s two values ( if changed ) on every i that it is applied to. Assuming you didn’t want every i to be the exact same “struck” for every struck I am assuming the data will differ. The only thing you can do is reduce the garbage. The iteration speed of the 10,000 probably wouldn’t change much in either case, I would put it down to however you prefer to work on that data. Whether you wanted the struck type or of that you wanted a string with an _ reference to the accompanying value. Which will require parsing back.
the garbage saving is only important if you’re doing this at run time while wanting to do other things and not have a fps drop.
Maybe somebody else has deeper insight than me here.

1 Like

Thanks for the reply!

In my case, I do use it inside Update so it will be executed a lot, on multiple players while run time.

I’m aiming for mobile game so I could use every FPS.

Thanks for the insight, looks like I would rather use the second approach in my situation.

Nope, there would be no difference in memory allocations here. local variables are allocated on the stack. Struct types allocated on the stack do not allocate any memory on the heap. The string inside the struct would require some heap memory as a string is a reference type. However if you always assign the same string constant (as shown in the example) all would actually use the same string. Structs are copied when you assign them somewhere.

So declaring the variable outside the for loop does not change anything.

3 Likes

When he posted it wasn’t a struct it was a “struck”;
I had read that Struct does not increase mem. Vector3 Struct
But i saw maybe this custom struct with a string Hmm i was thinking;

But i am sure you are 100% correct.

Thank you, I appreciate it.

While results are somewhat spurious, mainly writing new structs is clearly faster than writing new classes. And using a constructor is clearly faster than new.

8497682--1131221--upload_2022-10-8_0-24-50.png

1 Like