Jump to content

VS2019 - Auto convert macro to constexpr results in slower code?

amckern

When i use VS2019 (16 Preview 3) to convert macro to constexpr i am getting much slower code

 

macro:

#define DEFAULT_LERP_ENABLED        true

constexpr:

int             g_lerp_enabled = DEFAULT_LERP_ENABLED;

Running time:

 

macro:

203.03 Seconds

 

constexpr

339.93 Seconds

 

Can some one shine some light on why a constant expression is slower then a macro? This code was originally built in the 1990s (its an enhanced Quake compiler known as ZHLT) so its quite insecure.

Link to comment
Share on other sites

Link to post
Share on other sites

In the first case the compiler can just substitute every occurrence of that constant with its value (in this case a char with value 1). This is the more efficient than allocating an int in memory, setting it to 1, then reading that memory location every time it's needed. There may be other subtleties that depend on the specific compiler but this is probably the main reason for the slowdown.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Sauron said:

probably the main reason for the slowdown.

Thanks.

 

Is this a prompt that MS is providing in an effort to ensure that programs are more secure, or is it a part of the C/C++ ISO?

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, amckern said:

Is this a prompt that MS is providing in an effort to ensure that programs are more secure, or is it a part of the C/C++ ISO?

No idea to be honest, I don't see how this would be more secure though

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, amckern said:

constexpr:


int             g_lerp_enabled = DEFAULT_LERP_ENABLED;

 

What is supposed to be constexpr about this? It's just a plain non-const int called g_lerp_enabled.

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, amckern said:

constexpr:


int             g_lerp_enabled = DEFAULT_LERP_ENABLED;

To make it a constant you need to declare it as such like this :

const int g_lerp_enabled = DEFAULT_LERP_ENABLED;

There is a huge difference between a variable and a constant. When C# build the code the constant value will be computed once and then it will replace the constant name everywhere with the constant value making it hard compiled into the code using it. Your current variable will be references to every single time it is used.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Franck said:

To make it a constant you need to declare it as such like this :


const int g_lerp_enabled = DEFAULT_LERP_ENABLED;

I'm just using the auto convert. I'll replace the function with a const int and see how it performs in comparison to the curent numbers.

 

Its also C++, not C#

Edited by amckern
using not useing
Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, amckern said:

I'm just using the auto convert. I'll replace the function with a const int and see how it performs in comparison to the curent numbers.

 

Its also C++, not C#

same thing for C++ plus it's the same syntax

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

×