• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Values and Variables

Status
Not open for further replies.
Level 15
Joined
Aug 7, 2013
Messages
1,338
If the arguments you are passing are constant parameters, it is good style to store them into named variables, e.g. JASS_MAX_ARRAY_SIZE versus say 0x2000. The compiler should inline these constants, which means you'll get the advantage of just using the actual value and not having to dereference a variable.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,220
Direct values is normally faster. It depends how the system was coded though.
I believe they are still faster but it is a small amount. If it is easy enough I would stick with direct values. If you have a lot of related code using that same value I would make it a variable.

I actually thought variables were faster o_O

JASS:
call someFunction(GetUnitX(someunit))
//vs
local unit u = someunit
local real r = GetUnitX(u)
call someFunction(r)

I am almost sure the bellow example is faster?
 
The 'compiler' is the vJASS script compiler, which indeed inlines constants (don't make constant handles apparently). The performance issue should only be considered if it is used a lot in a bottleneck of code. In the end it is a speed vs. readability decision that you have to make.To answer your question though; literals are faster than variables (a literal is often referred to as a constant).
 
I actually thought variables were faster o_O

JASS:
call someFunction(GetUnitX(someunit))
//vs
local unit u = someunit
local real r = GetUnitX(u)
call someFunction(r)

I am almost sure the bellow example is faster?

The above is faster because it is a direct value. The unit and real are pointers. So think about a unit being in memory. (call it someunit)
For the above you point directly to the source object (the unit.) and return the x value. So this is 2 steps.

For the bottom one you first initialize a local object (the unit). Then load the unit and set the unit variable to the loaded unit. Next you initialize the real. Then you set the x value of the unit to it. To set the x it loads the local unit variable then it finds where the pointed object is. Then the x is set. So this is 5 steps.

Calling functions in jass is very slow. So when you have multiple function calls (2 or more) then the local variable initialization and pointer speed becomes faster than calling the function.
 
Status
Not open for further replies.
Top