Mark Aron Szulyovszky
Mark Aron Szulyovszky

Reputation: 313

Slower operations on Array<SomeStruct> than on Array<SomeClass>... Why?

I did some comparison here: https://github.com/itchingpixels/structs-vs-classes and it seems like inserting a struct into an array of structs is 10x slower than inserting a class to an array of classes (with the same data).

Is something wrong with my tests?

What could be the reason of this? or.. is this expected?

Upvotes: 1

Views: 53

Answers (1)

BaseZen
BaseZen

Reputation: 8718

Expected. Classes use references (4-8 byte memory addresses); structs are value types so the entire struct must be inlined. Try with a tiny struct versus one that's hundreds of bytes in size. Try inserting at the end of an array versus the beginning.

Upvotes: 2

Related Questions