Code efficiency with regard to computing resources

This is where all discussion of WC3 mapmaking should be held. This forum exists particularly for mapmaking needs. If you have a question on how something works or have need for some code review, this is the place to put it.
Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Code efficiency with regard to computing resources

Post by Greenspawn » Tue Jan 19, 2010 1:04 am

What I'm trying to do is figure out which ways of doing things would be most efficient (i.e. take less time or demand less of a processor), so I'm hoping some people here know something about this (i haven't taken any compsci courses yet, so I don't know).

For example, which of the following methods is better:

Code: Select all

GetRandomReal(0, 1) <= 0.33
GetRandomInt(1, 100) <= 33
(I'm guessing the integer is better)

And any general notes on stuff like this would be appreciated
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

User avatar
Oxygen
Grandmaster of Grammar
Posts: 1281
Joined: Thu Jan 17, 2008 6:14 pm

Re: Code efficiency with regard to computing resources

Post by Oxygen » Tue Jan 19, 2010 1:51 pm

Less numbers and less calculus = more efficient :]

Rectifier
Revenent of the Replies
Posts: 259
Joined: Sun Mar 23, 2008 4:47 pm

Re: Code efficiency with regard to computing resources

Post by Rectifier » Tue Jan 19, 2010 5:56 pm

Computers use a lot of resources for iterative processes, like calling a function within a function, and algorithms that solve abstract problems, like calculus.

Floats and integers tend to use a similar amount of data, but it depends on the language used. I believe they are both 4 bytes for each variable, but don't quote me on that.

Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Re: Code efficiency with regard to computing resources

Post by Greenspawn » Tue Jan 19, 2010 6:09 pm

Oxygen wrote:Less numbers and less calculus = more efficient :]
So the integer IS better, because there are a finite number of integers between 1 and 100, and an infinite number of numbers between 0 and 1?
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

User avatar
Oxygen
Grandmaster of Grammar
Posts: 1281
Joined: Thu Jan 17, 2008 6:14 pm

Re: Code efficiency with regard to computing resources

Post by Oxygen » Tue Jan 19, 2010 7:46 pm

Greenspawn wrote:
Oxygen wrote:Less numbers and less calculus = more efficient :]
So the integer IS better, because there are a finite number of integers between 1 and 100, and an infinite number of numbers between 0 and 1?
That would sound so right!

User avatar
Rising_Dusk
Chosen of the Intargweeb
Posts: 4031
Joined: Sat Dec 15, 2007 1:50 pm
Contact:

Re: Code efficiency with regard to computing resources

Post by Rising_Dusk » Tue Jan 19, 2010 8:44 pm

The difference in this case is negligible, if present at all. Use whichever makes you feel better about yourself.
"I'll come to Florida one day and make you look like a damn princess." ~Hep

Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Re: Code efficiency with regard to computing resources

Post by Greenspawn » Thu Jan 21, 2010 10:49 am

Rectifier wrote:Computers use a lot of resources for iterative processes, like calling a function within a function, and algorithms that solve abstract problems, like calculus.
So would it be better to:

Code: Select all

SetUnitPositionLoc( GetEnumUnit(), GetUnitLoc(GetSpellAbilityUnit()) )
or

Code: Select all

local unit u = GetEnumUnit()
local unit v = GetSpellAbilityUnit()
SetUnitPositionLoc(u, GetUnitLoc(v) )
or is there little difference?
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

User avatar
Rising_Dusk
Chosen of the Intargweeb
Posts: 4031
Joined: Sat Dec 15, 2007 1:50 pm
Contact:

Re: Code efficiency with regard to computing resources

Post by Rising_Dusk » Thu Jan 21, 2010 12:11 pm

The better solution is to not use locations at all, but rather use coordinates and then SetUnitX() and SetUnitY(). The thing with what you're doing in the first one is that you're leaking the location returned by GetUnitLoc(). Coordinate reals don't leak, though, so you could inline it.

Code: Select all

SetUnitPosition(GetEnumUnit(), GetUnitX(GetSpellAbilityUnit()), GetUnitY(GetSpellAbilityUnit())
"I'll come to Florida one day and make you look like a damn princess." ~Hep

Rectifier
Revenent of the Replies
Posts: 259
Joined: Sun Mar 23, 2008 4:47 pm

Re: Code efficiency with regard to computing resources

Post by Rectifier » Thu Jan 21, 2010 1:44 pm

You know a buttload about the coding language for wc3.

User avatar
TheDeathstalker
Keeper of the Keys
Posts: 476
Joined: Fri May 30, 2008 11:17 pm

Re: Code efficiency with regard to computing resources

Post by TheDeathstalker » Thu Jan 21, 2010 2:43 pm

Next to Vex himself, I don't think there's too many people who are on the same tier as Dusk in terms of both knowledge of JASS and contribution to the community as a whole.
And though you come out of each gruelling bout,
All broken and beaten and scarred,
Just have one more try—it’s dead easy to die,
It’s the keeping-on-living that’s hard.

User avatar
Oxygen
Grandmaster of Grammar
Posts: 1281
Joined: Thu Jan 17, 2008 6:14 pm

Re: Code efficiency with regard to computing resources

Post by Oxygen » Thu Jan 21, 2010 8:44 pm

TheDeathstalker wrote:Next to Vex himself, I don't think there's too many people who are on the same tier as Dusk in terms of both knowledge of JASS
bobo!
TheDeathstalker wrote:and contribution to the community as a whole.
yuck nevermind

User avatar
Rising_Dusk
Chosen of the Intargweeb
Posts: 4031
Joined: Sat Dec 15, 2007 1:50 pm
Contact:

Re: Code efficiency with regard to computing resources

Post by Rising_Dusk » Thu Jan 21, 2010 9:43 pm

You flatter me, I'm really not that useful. Oo;
"I'll come to Florida one day and make you look like a damn princess." ~Hep

Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Re: Code efficiency with regard to computing resources

Post by Greenspawn » Tue Jan 26, 2010 8:01 pm

Rising_Dusk wrote:The better solution is to not use locations at all, but rather use coordinates and then SetUnitX() and SetUnitY(). The thing with what you're doing in the first one is that you're leaking the location returned by GetUnitLoc(). Coordinate reals don't leak, though, so you could inline it.

Code: Select all

SetUnitPosition(GetEnumUnit(), GetUnitX(GetSpellAbilityUnit()), GetUnitY(GetSpellAbilityUnit())
What about when creating special effects at a point? Or creating units at a point?

Code: Select all

call AddSpecialEffectLoc("Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", Location(GetLocationX(GetUnitLoc(GetEnumUnit())), GetLocationY(GetUnitLoc(GetEnumUnit()))) )
Does Location(x,y) leak?
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

User avatar
Fledermaus
Keeper of the Keys
Posts: 354
Joined: Fri Feb 01, 2008 9:55 am
Location: New Zealand
Contact:

Re: Code efficiency with regard to computing resources

Post by Fledermaus » Tue Jan 26, 2010 9:12 pm

Code: Select all

native Location takes real x, real y returns location
(Yes)

Greenspawn
Keeper of the Keys
Posts: 434
Joined: Fri Jan 18, 2008 4:50 pm
Location: Massachusetts

Re: Code efficiency with regard to computing resources

Post by Greenspawn » Tue Jan 26, 2010 11:20 pm

So is there a way to do it in a leakless way?
Math is # |e^iπ|
"I can't imagine getting hit by a giant rock and not being maimed or crippled or ruined" -Dusk

Logue: Please replace the toilet paper when you use it all. For some reason my 5 year old son believes if it's not there he does not have to wipe.

Post Reply

Return to “Custom Map Creation”