HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Which is better for optimization?

08-25-2007, 12:08 PM#1
Pyrogasm
Which of the two is better for optimization?:
Collapse JASS:
//Code where "I" could be set to anything

set I = I+1
call SetTableInt(UTable, "CSFE Total Unsleep Functions", I)
call SetTableString(UTable, "CSFE Unsleep Functions "+I2S(I), functionName)
Or this:
Collapse JASS:
//Code where "I" could be set to anything

call SetTableInt(UTable, "CSFE Total Unsleep Functions", I+1)
call SetTableString(UTable, "CSFE Unsleep Functions "+I2S(I+1), functionName)
08-25-2007, 12:15 PM#2
Ammorth
Depends on how many times you use "I".
I'm not sure exactly the value, but normally after 4-5 uses of "I+1", I set it to a variable.
08-25-2007, 12:46 PM#3
cohadar
There will be no performance differences no matter witch one you choose.
08-25-2007, 12:50 PM#4
Pyrogasm
Okay then.
08-25-2007, 12:51 PM#5
NightBreeze
If all you do is add 1 to I, then I would say the second is faster. However, if you're looping it, won't you want to store the new value of I anyway? I update the variable whenever I use it more than twice, but like Ammorth I don't know the exact numbers.

Cohadar: No noticable differences, he just wants to know which one is faster.
08-25-2007, 01:04 PM#6
Pyrogasm
What I meant to convey was "if someone were to tell me to optimize some code containing this, which would I use?"

I personally don't care which of the two is faster as such as small thing is unimportant to me. I was merely curious.

I used the first one.
08-25-2007, 01:11 PM#7
cohadar
Offtopic:

@NightBreeze
I really needed that explanation,
maybe if you could put it in red letters next time
I think that underlining is not obvious enough for my small brain.
08-25-2007, 01:15 PM#8
DioD
first is calculated ONCE, second - every time, this is only one cycle difference.

BUT local declaration takes up hurge time (hash table generation) if you have any temp global, better to use it.
08-25-2007, 01:20 PM#9
cohadar
Quote:
Originally Posted by DioD
first is calculated ONCE, second - every time, this is only one cycle difference.

BUT local declaration takes up hurge time (hash table generation) if you have any temp global, better to use it.

Why are locals using hash tables?
08-25-2007, 01:52 PM#10
Pyrogasm
Oh dear god. Have I sparked another of those threads that go on and on about locals, globals, synching, and hash tables? ><
08-25-2007, 02:27 PM#11
NightBreeze
DioD, could you tell us more about what you're saying? Local declaration takes a lot of time you say. How do you figure and do you have any indication as to HOW much time? Let's say.. compare it to adding 1 to an integer. I don't see how this has anything to do with the declaration of a variable. The variable has already been declared, it's merely being incremented.

In short: Please share your information and its source if you can :)

@Cohadar: I meant nothing by it and don't know why you're insulted.

@Pyro: Perhaps it was inevitable? You wanted to know which one is faster.. not so surprising that the mechanics behind declaring/setting a variable are discussed. Let's just make sure the 'on and on' part is not realized ^^
08-25-2007, 02:46 PM#12
DioD
What is faster

allocate memory for var
save allocated memory addresses somewhere (if memory defragmented its table\array\or linked list)
create a key for new var (eg name, eg hash table log)
set value for a var

or

set value for a var

???

Actualy any programm is set of instructions for CPU, let me explain.
Take some paper and pen, write any ten digits. You cant find digit 8 without cheking every digit present in list.(if you can do it, dont read this - rule the world) Then you may have named digits, digit 8 will get title "one more day". You still can check every value looking for this title, but if you have billion of values, how to find what are you looking, checking every value will take ages, much more better to allocate hack table or list, what will register every var inside it and return value from its title.
08-25-2007, 02:59 PM#13
NightBreeze
I don't think you described the two options pyro gave. In my opinion they were:

1. Calculate x + 1 once and store it in x, then read x twice
2. Calculate x + 1 twice

So the question is this: Is storing a value + reading it twice faster than adding 1 to an arbitrary integer? If not, what is the factorial speed difference between adding 1 to an integer and reading a variable (the storing is only done once, so becomes negligible for a large number of reads).

Also, x already exists, you don't have to allocate memory for it. Furthermore, "set value for a var" is done in both of your options, but is only done in one of the two pyro suggested.
08-25-2007, 03:16 PM#14
cohadar
Let me add a few things:

First of all memory allocation has NOTHING to do with locals in JASS.

memory allocation techniques do not apply to individual variables but to blocks of memory. wiki

Second when you use local reals and integers there is no additional overhead whatsoever.

Only when you declare local handles (units, items, groups..)
you will get overhead because these handles need to be counted and recycled so system keeps track of them. (with hashtable probably)

This hashing is extremely fast, so fast that in fact it will not make a difference
unless you call function several thousand times.
It is written in C++ after all.

So when it comes to optimization there are much bigger issues to worry about than locals.
08-25-2007, 03:17 PM#15
Strilanc
Unless you're calling this function a million times per second (literally), don't worry about it. The time difference is negligible and you're better off focusing on finishing your map or making the code elegant.

Only worry about optimizations when the game starts to skip when events fire (note: use an older computer to make sure).