Script Efficiency: set variable versus player.Is*

Post » Mon Jun 13, 2011 9:07 am

I don't know if a good way to test efficiency, and most of my script optimizations have been based on my knowledge of real programming. That said, here's what I'm curious about:

I have a script that runs every frame.
In 2 spots I want to test player.IsSneaking.
Is it more efficient to set a variable with the result of player.IsSneaking, then test against that variable, or to just use player.IsSneaking twice?
What matters is how trivial (or not) the IsSneaking function is; a very simple memory read would outperform a memory write (setting the variable).
But since I don't know how trivial it is, I'm here to ask; does anyone know?
For that matter, does anyone know which functions are cheaper than writing to a variable?

Queue
User avatar
Channing
 
Posts: 3393
Joined: Thu Nov 30, 2006 4:05 pm

Post » Mon Jun 13, 2011 2:36 pm

The thread http://www.gamesas.com/index.php?/topic/1139867-script-optimisation/ should help you find the answers. (The Code Comparer link is at the bottom of the first post.)
User avatar
Penny Flame
 
Posts: 3336
Joined: Sat Aug 12, 2006 1:53 am

Post » Mon Jun 13, 2011 5:03 am

Thank you again, that's the sort of information I was after. =]

Queue
User avatar
Connie Thomas
 
Posts: 3362
Joined: Sun Nov 19, 2006 9:58 am

Post » Mon Jun 13, 2011 2:37 pm

Ok, first test using CCC:
;CODE 1set iPlayerIsSneaking to player.IsSneakingif iPlayerIsSneakingelseendifif iPlayerIsSneakingelseendif

versus
;CODE 2if player.IsSneakingelseendifif player.IsSneakingelseendif

CODE 2 wins by a little bit.
9500	60.459518	29.904305	59.737179	36.3636369500	61.652309	29.620848	61.274536	36.3636329500	60.459518	29.904301	60.024036	36.3636409500	61.199539	29.922195	61.199543	36.3636329500	60.024036	29.904301	60.459518	36.363636

Had to get up to 9500 iterations per frame before my framerate dipped below 30.

Gonna expand it to 10 sneaking checks. Then I'm gonna test IsMoving.

Edit - At 10 sneak checks, it's a dead heat between the two. I mean, like, FPS is identical between the two.

Edit 2 - For 3 if tests, two of which included ==, it was cheaper to skip setting a local variable for player.IsMoving. Basically the same as for IsSneaking.

Queue
User avatar
lisa nuttall
 
Posts: 3277
Joined: Tue Jun 20, 2006 1:33 pm

Post » Mon Jun 13, 2011 11:24 am

That's good to know. :thumbsup:
User avatar
Ridhwan Hemsome
 
Posts: 3501
Joined: Sun May 06, 2007 2:13 pm


Return to Fallout: New Vegas