[Closed] Strange behaviour running a script in an empty scene
Take the following script which basically does some arbitrary calculations (creates then deletes 100 spheres):
fn createSpheres i =
(
local t = timestamp()
for j = 1 to 100 do
(
local s = sphere()
delete s
)
format "Attempt %: % milliseconds
" i (timestamp() - t)
)
for i = 1 to 20 do createSpheres i
In an empty scene it takes progressively longer and longer to run, by the end taking over ELEVEN seconds:
Attempt 1: 157 milliseconds
Attempt 2: 484 milliseconds
Attempt 3: 984 milliseconds
Attempt 4: 922 milliseconds
Attempt 5: 1344 milliseconds
Attempt 6: 1672 milliseconds
Attempt 7: 2187 milliseconds
Attempt 8: 2610 milliseconds
Attempt 9: 3125 milliseconds
Attempt 10: 3672 milliseconds
Attempt 11: 4422 milliseconds
Attempt 12: 4719 milliseconds
Attempt 13: 5578 milliseconds
Attempt 14: 6547 milliseconds
Attempt 15: 7672 milliseconds
Attempt 16: 7468 milliseconds
Attempt 17: 8984 milliseconds
Attempt 18: 10422 milliseconds
Attempt 19: 10953 milliseconds
Attempt 20: 11750 milliseconds
And if you run the script twice, the calculation times get increasingly huge, and only a garbage collection solves the problem.
However, just place one sphere in the scene before running the script and it's more like you would expect, a few milliseconds per iteration.
Attempt 1: 78 milliseconds
Attempt 2: 63 milliseconds
Attempt 3: 78 milliseconds
Attempt 4: 62 milliseconds
Attempt 5: 78 milliseconds
Attempt 6: 63 milliseconds
Attempt 7: 62 milliseconds
Attempt 8: 78 milliseconds
Attempt 9: 63 milliseconds
Attempt 10: 78 milliseconds
Attempt 11: 62 milliseconds
Attempt 12: 78 milliseconds
Attempt 13: 63 milliseconds
Attempt 14: 62 milliseconds
Attempt 15: 78 milliseconds
Attempt 16: 63 milliseconds
Attempt 17: 78 milliseconds
Attempt 18: 62 milliseconds
Attempt 19: 78 milliseconds
Attempt 20: 79 milliseconds
However, in max 9, the times are reduced (THREE times slower than max 8 though), but STILL increase:
Attempt 1: 141 milliseconds
Attempt 2: 172 milliseconds
Attempt 3: 156 milliseconds
Attempt 4: 172 milliseconds
Attempt 5: 172 milliseconds
Attempt 6: 172 milliseconds
Attempt 7: 188 milliseconds
Attempt 8: 187 milliseconds
Attempt 9: 203 milliseconds
Attempt 10: 204 milliseconds
Attempt 11: 203 milliseconds
Attempt 12: 203 milliseconds
Attempt 13: 219 milliseconds
Attempt 14: 218 milliseconds
Attempt 15: 219 milliseconds
Attempt 16: 234 milliseconds
Attempt 17: 235 milliseconds
Attempt 18: 234 milliseconds
Attempt 19: 250 milliseconds
Attempt 20: 266 milliseconds
OK
OK
Attempt 1: 265 milliseconds
Attempt 2: 266 milliseconds
Attempt 3: 265 milliseconds
Attempt 4: 282 milliseconds
Attempt 5: 281 milliseconds
Attempt 6: 297 milliseconds
Attempt 7: 297 milliseconds
Attempt 8: 297 milliseconds
Attempt 9: 312 milliseconds
Attempt 10: 313 milliseconds
Attempt 11: 328 milliseconds
Attempt 12: 328 milliseconds
Attempt 13: 344 milliseconds
Attempt 14: 328 milliseconds
Attempt 15: 359 milliseconds
Attempt 16: 344 milliseconds
Attempt 17: 359 milliseconds
Attempt 18: 375 milliseconds
Attempt 19: 375 milliseconds
Attempt 20: 375 milliseconds
It’s very strange. Any ideas?
That IS very strange… I tried it with undo off/redraw off contexts but like you said, having one or more objects in the scene speeds up the function dramatically… Weird shit
I would assume that would have to do with some form of undo being cached in the scene, but never released due to the iterative nature of the script. That’s merely a wild guess though.
However, with max 9 there seems to be a known issue of memory leaks.
I added a clearUndoBuffer() to the function and the results were much better. The results would be similar with gc().
(
fn createSpheres i =
(
local t = timestamp()
for j = 1 to 100 do
(
local s = sphere()
delete s
)
format "Attempt %: % milliseconds
" i (timestamp() - t)
clearUndoBuffer()
)
for i = 1 to 20 do createSpheres i
)
Results on an empty scene:
Attempt 1: 188 milliseconds
Attempt 2: 188 milliseconds
Attempt 3: 188 milliseconds
Attempt 4: 203 milliseconds
Attempt 5: 265 milliseconds
Attempt 6: 203 milliseconds
Attempt 7: 203 milliseconds
Attempt 8: 203 milliseconds
Attempt 9: 203 milliseconds
Attempt 10: 188 milliseconds
Attempt 11: 188 milliseconds
Attempt 12: 188 milliseconds
Attempt 13: 188 milliseconds
Attempt 14: 187 milliseconds
Attempt 15: 187 milliseconds
Attempt 16: 187 milliseconds
Attempt 17: 187 milliseconds
Attempt 18: 204 milliseconds
Attempt 19: 203 milliseconds
Attempt 20: 203 milliseconds
Yeah, it’s no big deal, just something curious that came up as I was testing for something else. :curious:
Hey !
How do you do this “Time calculation test” on your scripts sure looks usefull…
(
-- The timeStamp() function gives time-of-day in milliseconds.
-- the following line stores the current timeStamp value in a variable.
local t = timeStamp()
/*
...
insert some time consuming operation here
...
*/
-- substract the stored timeStamp [b]t[/b] from the current timeStamp
-- to calculate the number of milliseconds passed.
local dur = timeStamp() - t
-- output the result to the Listener
format "The operation took % milliseconds.
" dur
)
Martijn