About Monkey 2 › Forums › Monkey 2 Programming Help › How do I get the size of an array in megabytes?
This topic contains 7 replies, has 3 voices, and was last updated by
Amon
1 year, 2 months ago.
-
AuthorPosts
-
January 26, 2018 at 3:43 am #13249
How do I get the size of an array in megabytes?
January 26, 2018 at 6:37 am #13250It’ll depend on the objects stored in the array…
January 26, 2018 at 10:06 am #13253Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869#Import "<std>"#Import "<libc>"Using std..Using libcFunction Main()' about the sizes' the size of a floatLocal vFloat:Float = 1.0Print("size of float is " + sizeof(vFloat)) ' this is 4' from now on the sizeof function can be wrapped inside the ArrayExtensions class' this is only for syntactic reasons of providing a streamlined APIPrint("size of float is " + ArrayExtensions.SizeInBytes(vFloat))Print("")' some disambiguation about array sizesLocal aFloat:Float[] = New Float[] (1.0, 2.0, 4.0, 5.0, 10.0, 30.0)Print("size of the data type of the array is " + sizeof(aFloat)) ' this is 4, it means the size of the datatypePrint("size of the n-dimension of an array is " + aFloat.GetSize(0)) ' this is 6, it means the # of the elements in a dimensionPrint("")' calculating the size of the array like this byteSize * numberOfElements' in this case the result is 24 (which means 4bytes * 6elements)Print("size of float array is " + ArrayExtensions.SizeInBytes(aFloat))Print("")' finding the sizes of a struct or a classLocal scolor := New SColorLocal ccolor := New CColorPrint("struct size is " + ArrayExtensions.SizeInBytes(scolor)) ' this is 4 (because 4fields * 1byte)Print("class size is " + ArrayExtensions.SizeInBytes(ccolor)) ' this is 4 againPrint("")' testing arrays of a struct and a classLocal asColor := New SColor[] (New SColor, New SColor, New SColor, New SColor)Local acColor := New CColor[] (New CColor, New CColor, New CColor, New CColor)Print("size of struct array " + ArrayExtensions.SizeInBytes(asColor)) ' it's 16Print("size of class array " + ArrayExtensions.SizeInBytes(acColor)) ' the sameEnd' Built in types cannot be extended otherwise it would be implemented as an extension' Struct @Array<T> ExtensionStruct ArrayExtensions AbstractFunction SizeInBytes<T>:Int(item_:T)Return sizeof(item_)EndFunction SizeInBytes<T>:Int(array_:T[])Return array_.Length * sizeof(array_)EndEndStruct SColorField R:ByteField G:ByteField B:ByteField A:ByteEndClass CColorField R:ByteField G:ByteField B:ByteField A:ByteEndJanuary 26, 2018 at 2:07 pm #13264Cool, thank you, Cocon. That’ll do lovely.
January 26, 2018 at 10:28 pm #13274Very nice Cocon, didnt know you could do that in MX2!
Does it work with an array of objects? I’ll have to check it out!
Also Amon, why did you want to know this?
January 27, 2018 at 6:35 am #13278Heya. The game I’m making is a platform game but a bit like an endless running game. The level scrolls downwards and you have to keep jumping up, destroying enemies etc. till you get to the top where you face the final boss. I noticed that there was about an eight seconds delay when switching to my game screen so I thought it must be the level array but I had no idea how to test its size to see if that was in fact, causing the delay. Thanks to Cocon’s code I found it was the level array. I split the array into smaller chunk arrays and now the loading is less than a second.
January 27, 2018 at 1:21 pm #13281I updated the example to include struct and classes.
Also testing the Monokai theme…
January 27, 2018 at 1:32 pm #13282Pretty cool. Thanks for the updates. This is now archived as must have MX2 code.
Thank you, Cocon.
-
AuthorPosts
You must be logged in to reply to this topic.