[Closed] Little question about struct and functions
When I write a function included in a structure, is the function unique in memory or a copy of this function is realized for each element defined by this structure?
struct person (
name, height, weight,
fn bmi = ( weight/(height^2) )
)
bill = person name:"Bill" height:1.82 weight:72
joe = person name:"Joseph" height:1.68 weight:78
bill.bmi()
joe.bmi()
For example here the function bmi () is twice copied in memory : once for bill and once for joe ?
Thanks
Yes I believe this is the case. You are making an instance of the whole struct.
Paul, thank you very much for your reply.
That is an important point when i use big arrays of struct…
I guess that I look at Structs as having two seperate jobs, one for managing data and the other for managing code and I don’t tend to mix them. If you want to drop your function in a struct then have a second struct that you call to do all the work. That way you only need one instance of it running.
I don’t think this is correct, see my previous thread–
http://forums.cgsociety.org/showthread.php?t=285432&highlight=function
Ah well that is good news. I thought that it was the other way. Thanks for stepping in.
stuh, do you remember what kind of speed test you used ?
I would have thought of a memory test… but that does not seem possible in maxscript.
changed your name, prettypixel?
stuh, do you remember what kind of speed test you used ?
I did a speed test, because allocating memory is bound to take time.
To make a controlled experiment I create both a small and large struct, then make X instances of one of those, and take the total time. It should not take more time to make 100,000 instances of one or the other.
If you run the test sometimes one is faster or the other, or sometimes they are equal…this is due to GC interference
<<script attached>>
I would have thought of a memory test… but that does not seem possible in maxscript.
Actually, it is:
#()
5417244
58136250
(smallStruct a:1)
(bigStruct )
-- Error occurred in i loop; filename: D:\Documents and Settings\Owner\Desktop\speedTest.ms; position: 35079
-- Frame:
-- i: 171628
-- Runtime error: Out of scripter memory - use options to increase
58137031
#()
5800632
58137187
(smallStruct a:1)
(bigStruct )
-- Error occurred in i loop; filename: D:\Documents and Settings\Owner\Desktop\speedTest.ms; position: 35308
-- Frame:
-- i: 180945
-- Runtime error: Out of scripter memory - use options to increase
58137921
Here you can see the scripter ran out of memory at 171628 small structs, and also at 180945 large structs…so the large ones definitely arent taking up more memory
It is a crash test
well as usual you are right.
We can even create more copies of the biggest structure
That is the precision of the computers… The worst is to understand that there is probably a logical reason in it.
nice tree ^^
Thank you mister Stuh
[left]sysinfo.[b]getSystemMemoryInfo/b
[/left]
[left]Returns a 7 element array containing system memory status data. The array elements contain the following, respectively:
[/left]
[ul]<LI style=“FONT-SIZE: 10pt”>[left]percent of memory in use
[/left]
<LI style=“FONT-SIZE: 10pt”>[left]bytes of physical memory
[/left]
<LI style=“FONT-SIZE: 10pt”>[left]free physical memory bytes
[/left]
<LI style=“FONT-SIZE: 10pt”>[left]bytes of paging file
[/left]
<LI style=“FONT-SIZE: 10pt”>[left]free bytes of paging file
[/left]
<LI style=“FONT-SIZE: 10pt”>[left]user bytes of address space
[/left]
[li][left][left]free user bytes[/left][/left][/li][/ul][left][left] [/left][/left]