Question: Mathematical Operations Between Constants
#1

I hope the title reflect my question as I wasn't sure how to express it.

Anyway, if I'm doing a mathematical operation between 2 constants (or more), does the compiler optimize it?

For example:

pawn Код:
// Assuming that MAX_PLAYERS is 500, and MAX_VEHICLES is 2000.
new
    x = MAX_PLAYERS * MAX_VEHICLES; // Equivalent to x = 1000000 ?
I think I've seen this in ******' thread about code optimization a while ago.
Reply
#2

The preprocessor will simply change MAX_PLAYERS to 500 and MAX_VEHICLES to 2000 before compiling there is no optimization, the only benefit you get is the values can be changed in one place which will affect the code anywhere those defines are used. So really the only optimization is saving you coding time by changing one value instead of potentially hundreds.
Reply
#3

Yes, that would be correct. If you then used x elsewhere in the code, it would be equal to 1000000
Reply
#4

What he's asking is, does the compiler interpret that as a constant expression (right word?) and make it 1000000, or does it store the multiplication and process that every time 'x' is declared.

I'm not sure.
Reply
#5

Well if it's a global variable it would initialized at runtime then anywhere x is used would be 1000000 I don't think the constant expression would continue to be evaluated.
Reply
#6

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
The preprocessor will simply change MAX_PLAYERS to 500 and MAX_VEHICLES to 2000 before compiling there is no optimization, the only benefit you get is the values can be changed in one place which will affect the code anywhere those defines are used. So really the only optimization is saving you coding time by changing one value instead of potentially hundreds.
That's what I'm asking, if it will simply replace it with 500 and 2000 or compile it as 1000000 and not as 500 * 2000.

Quote:
Originally Posted by MattyG
Посмотреть сообщение
Yes, that would be correct. If you then used x elsewhere in the code, it would be equal to 1000000
No that's not what I'm asking, I know it will be 1000000, read my post again...

Quote:
Originally Posted by MP2
Посмотреть сообщение
What he's asking is, does the compiler interpret that as a constant expression (right word?) and make it 1000000, or does it store the multiplication and process that every time 'x' is declared.

I'm not sure.
Not quite what I asked.

I wanted to ask if the compiler will have to calculate the operation between the constants when I want to assign it to x, or if it will replace it as 1000000 and assign it to x.
Reply
#7

Quote:
Originally Posted by ******
Посмотреть сообщение
Yes, in most cases, but it isn't very clever so some times you have to help it out. Also, I don't think it does ANY optimisations on floats (but you can check).

This is why you can do:

pawn Код:
case 8 * 8:
But not:

pawn Код:
case 8 * a:
Unless "a" is actually declared as "const", in which case the compiler can actually sometimes optimise that too.

Doing this:

pawn Код:
new b = a * 10 + 5
Will NOT optimise because you need "a" before any evaluation can be done. Even something like this will not optimise (again, I don't think) because of operator ordering:

pawn Код:
new b = a * 10 / 2;
But this will because the order has been forcibly changed:

pawn Код:
new b = a * (10 / 2);
Thank you very much!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)