Jump to content

Which script would be easier on memory?


genericuser27

Recommended Posts

Posted

I'm just wandering the best way to set up a script so that it takes up less memory (unless they are the same amount). I'm working on a much larger script and for the ease of it just made these as examples:

 

1.

 

 

 

event OnDeath(Player)

dothis()

dothat()

endevent

 

 

function dothis()

do thing one <---- I know this isn't an actual command, I'm just using it to save time.

endfunction

 

function dothat()

do thing two <---- I know this isn't an actual command, I'm just using it to save time.

endfunction

 

 

 

 

2.

 

 

 

 

event OnDeath(player)

dothisandthat()

endevent

 

 

function dothisandthat()

do thing one <---- I know this isn't an actual command, I'm just using it to save time.

do thingtwo <---- I know this isn't an actual command, I'm just using it to save time.

endfunction

 

 

 

 
Posted

How about this? The difference is negligible but still uses the least amount of memory between three alternatives.

 

3.

event OnDeath(player)

do thing one

do thingtwo

endevent

 

Order of memory usage: 1 > 2 > 3

Posted

How about this? The difference is negligible but still uses the least amount of memory between three alternatives.

 

3.

event OnDeath(player)

do thing one

do thingtwo

endevent

 

Order of memory usage: 1 > 2 > 3

 

I'm no coder but that sounds just like the first option

Guest MonsterFish
Posted

No the first option is using a function to call what to do and thus uses more memory. In their version it doesn't use a function and just does is it as is. Which I suppose is better if you're not using a function to gain data.

Posted

Calling a function consumes memory on the stack (that is not too much), but consumes power to execute the actual call (that in Papyrus is: find the container, find the name, find the state of the function, find the actual implementation, save the call stack, put the parameters on the stack, call the function, grab the parameters.

 

So, in terms of "memory" the difference is close to zero.

But in therm of efficiency the last post from Bjork is the better one. Don't use a function if you don't need.

 

 

Just for info:

 

Try to execute this:

 

 

int i = 10000
int r = 0
Actor a = <something>
while i
  i -= 1
  if a.isEnabled()
    r += 1
  endIf
endWhile

 

and

 

 

int i = 10000
int r = 0
Actor a = <something>
while i
  i -= 1
  if !a.isDisabled()
    r += 1
  endIf
endWhile
Posted

Okay so I'm using the functions to transfer the value of c between different events. 

int b = 2
int c, k

event OnActivate(something)
dothis()
endevent

event OnHit(
dothat()
endevent

event OnDeath(Player)

getvaluek(c)
if (k >= 20)
do something
else (k < 20)
do anotherthing
endif
endevent

function dothis()
c = 0
endfunction

function dothat(
c += b
endfunction


int function getvaluek(c)
k = c + 1
return k
endfunction

I think what I need is for the variables to be the same between all of the events. If I did have the functions in there but instead replaced the call functions with the math would the other events still be able to alter the same "c" or would they all have their own unique "c"
 
Also if you see anything wrong with the script, please point it out. I'm a noob at scripting thanks for the help.

Posted

The only event that may be executed many times is the "OnHit".

All others are called only once or not often, so prefer readable code.

 

If your "DoThat()" that is the only function inside the OnHit, does only a sum, then you spend more power in calling the function that actually executing the code.

 

 In your code I see that you are using side effects for the variables.

"getValueK()" returns the value, but you modify the script global variable K.

This will make really hard to understand problems (of course for real code.)

 

 

Last advice: if you can avoid a function, then avoid it. But not remove all of them if the code became no more readable.

Check the code inside cycles. Here you can lose power and time if you don't do it right.

 

 

int i = 0
while i < actors.length
  if actors[i] != Game.getPlayer()
    actors[i].kill()
 endIf
 i = i + 1
endWhile

 

if about 1000 times slower than:

 

int i = actors.length
Actor thePlayer = Game.getPlayer()
while i
  i -= 1
  if actors[i] != thePlayer
    actors[i].kill()
 endIf
endWhile
 
Posted

Okay so if I declare my parameters at the beginning of the script, and not inside an event, it can be used throughout the whole script right?

 

If I alter the said parameters inside an event, they will be altered for the whole script right? That way I won't have to use functions to return values inside an event if it is already changed up top, right?

Posted

you can define your variables as normal variables or Properties.

Normal variables are faster but cannot be accessed outside the script.

Properties are slightly slower but can be accessed outside the script.

 

The only thing yo ushould not do is to use side effect.

 

If "K" is an important variable for you, the you should follow one of the two options:

1) Have it global and never use it as parameter or as result value.

2) never have it global and always pass it as parameter or return value

 

If you mix the two, then you may introduce difficult-to-find errors in your code.

 

Posted

I'm having issues trying to add 

 

SexLabFramework property SexLab auto

 

to my script. Am I missing the psc files? I'm not sure where I can get them. I'm up to date on the CK, SKSE, SexLab, and skyrim.

post-110867-0-73547900-1449843452_thumb.png

Posted

the SexLab's PSC file sslUtility.psc seems to be here. It relies on a SKSE plugin to implement the native code.

Just try to re-install SexLab files (the Scripts folder and the SKSE folder should be enough.)

 

Are you trying to compile under MO or directly in your data folder?

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...