HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Which is faster ?

03-02-2007, 05:07 PM#1
zen87
kay i have a long list like below :
Collapse JASS:
        if id=='halt' then
            set i = GetRandomInt(1,2)
        elseif id=='hlum' then
            set i = GetRandomInt(4,6)
        elseif id=='hbla' then
            set i = GetRandomInt(11,14)
        elseif id=='harm' then
            set i = GetRandomInt(24,30)
        elseif id=='hars' then
            set i = GetRandomInt(55,65)
        elseif id=='hgra' then
            set i = GetRandomInt(90,120)
        elseif id=='hwtw' then
            set i = GetRandomInt(180,200)
        elseif id=='htow' then
            set i = GetRandomInt(10,15)
        endif
basically it returns a random number in certain range for diff unit-type, my question now would it be better i store all these integer in a gamecache and use HaveStoredInteger to get the random number i wanted ?

something like below :
Collapse JASS:
function GameInti takes nothing returns nothing // basically runs during game intialization
    call StoreInteger(cache,"LowerValue",I2S('halt'),1)
    call StoreInteger(cache,"UpperValue",I2S('halt'),2)
    call StoreInteger(cache,"LowerValue",I2S('hlum'),4)
    call StoreInteger(cache,"UpperValue",I2S('hlum'),6)
    call StoreInteger(cache,"LowerValue",I2S('hbla'),11)
    call StoreInteger(cache,"UpperValue",I2S('hbla'),14)
    ... //and so on
endfunction

function GetInt takes integer i returns integer
    if HaveStoredInteger("LowerValue",I2S(i))!=0 then
        return GetRandomInt(GetStoredInteger(cache,"LowerValue",I2S(i)),GetStoredInteger(cache,"UpperValue",I2S(i)))
    endif
    return 0
endfunction

03-02-2007, 05:15 PM#2
Captain Griffen
First one.
03-02-2007, 05:51 PM#3
PitzerMike
Depends how many unit types there can be.

Game cache at least provides constant time for what that's worth, whereas the speed of the first solution heavily depends on how far down in the list the id is.
03-02-2007, 07:44 PM#4
PipeDream
could also build a binary tree of if/then/else
03-03-2007, 01:34 AM#5
zen87
hmph so where is the crossing line ? i mean how long the list go untill the game cache is faster instead ?
03-03-2007, 08:34 AM#6
Captain Griffen
If you use a binary tree, then probably around 2^10, I'd say, which is over 1000. That assumes five integer comparisons are equal to one gamecache call (which is about more likely to be low; non-array variables are lightning fast, as are comparisons).
03-03-2007, 09:28 AM#7
zen87
uhhhhh... sry... but what's a binary tree ? @@ can i have some link to study more ? :)