Jump to content

Do compilers combine math operations?

TickleForce
Go to solution Solved by laxer,

Since no one has really successfully answered this question I will do it.

 

Yes the compiler will optimize the code, It is known as Constant Folding.

 

How you program it is personal preference. The performance is negligible when it is processed through the parser. (It will create one fewer node)

 

If you follow the holy bible of programming(Code Complete by McConnell)

 

It is better to write it out as the code is generally easier to follow.

 

In all honesty, absolutely no one cares about seconds saved during compiling. It is more important to make the code readable than it is to compile quickly.

Since no one has really successfully answered this question I will do it.

 

Yes the compiler will optimize the code, It is known as Constant Folding.

 

How you program it is personal preference. The performance is negligible when it is processed through the parser. (It will create one fewer node)

 

If you follow the holy bible of programming(Code Complete by McConnell)

 

It is better to write it out as the code is generally easier to follow.

 

In all honesty, absolutely no one cares about seconds saved during compiling. It is more important to make the code readable than it is to compile quickly.

Link to comment
Share on other sites

Link to post
Share on other sites

In all honesty, absolutely no one cares about seconds saved during compiling. It is more important to make the code readable than it is to compile quickly.

It's probably not going to be seconds

 

2^32 divisions in a loop took about 13.5 seconds and 2^32 multiplications took about 1 second. I tested it using assembly and subtracted the base loop run time to calculate those.

For even a large project, the difference it going to be on the scale of microseconds.

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you for the information Art,

 

Even in worse case scenario once it is compiled it will perform exactly the same as it would if you smashed things down yourself.

 

Unless it is an interpreted language the end product will be exactly the same and even then you don't use interpreted languages when performance is the goal.

 

 

---------------------------

 

Somewhat related, if you did it in a loop the compiler only had to optimize it once.

 

If you truly wanted to test it you would have to write it out or compile it with the flag that expands loops.

Link to comment
Share on other sites

Link to post
Share on other sites

They do as a means of optimization. There's no point in (a = 5 * 4) being calculated at runtime if that's the initial value so the compiler will just set (a = 20) instead.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×