Is there a difference in performance between 'const' and 'var' declarations in Roslyn?










0















In a .NET application, there's a definitive performance difference between these two ways of declaring a string value:



  1. const string MyConst = "value";


  2. var MyConst = "value";


The former (#1) is a compile-time declaration so there is no performance loss at runtime, compared to the latter (#2) which will allocate and initialize a String object every time that line of code is executed.



In my project I utilize Roslyn to compile user-specified C# scripts using CSharpCodeProvider.CompileAssemblyFromSource, extract the relevant methods (MethodInfo objects) and keep them around in memory to be invoked later as needed.



My question is, do the aforementioned performance differences still apply in this case? Are const, static and other statements that are normally compile-time, still behave in that fashion when a script is compiled and executed in-memory, as described above?










share|improve this question

















  • 3





    Strings are interned. I doubt you will see any meaningful difference.

    – Robert Harvey
    Nov 15 '18 at 20:20












  • Are you interested in const generally? Or const string specifically?

    – mjwills
    Nov 15 '18 at 20:22











  • @mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

    – taiji123
    Nov 15 '18 at 20:32






  • 1





    The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

    – mjwills
    Nov 15 '18 at 20:35







  • 1





    It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

    – Hans Passant
    Nov 15 '18 at 22:31
















0















In a .NET application, there's a definitive performance difference between these two ways of declaring a string value:



  1. const string MyConst = "value";


  2. var MyConst = "value";


The former (#1) is a compile-time declaration so there is no performance loss at runtime, compared to the latter (#2) which will allocate and initialize a String object every time that line of code is executed.



In my project I utilize Roslyn to compile user-specified C# scripts using CSharpCodeProvider.CompileAssemblyFromSource, extract the relevant methods (MethodInfo objects) and keep them around in memory to be invoked later as needed.



My question is, do the aforementioned performance differences still apply in this case? Are const, static and other statements that are normally compile-time, still behave in that fashion when a script is compiled and executed in-memory, as described above?










share|improve this question

















  • 3





    Strings are interned. I doubt you will see any meaningful difference.

    – Robert Harvey
    Nov 15 '18 at 20:20












  • Are you interested in const generally? Or const string specifically?

    – mjwills
    Nov 15 '18 at 20:22











  • @mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

    – taiji123
    Nov 15 '18 at 20:32






  • 1





    The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

    – mjwills
    Nov 15 '18 at 20:35







  • 1





    It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

    – Hans Passant
    Nov 15 '18 at 22:31














0












0








0








In a .NET application, there's a definitive performance difference between these two ways of declaring a string value:



  1. const string MyConst = "value";


  2. var MyConst = "value";


The former (#1) is a compile-time declaration so there is no performance loss at runtime, compared to the latter (#2) which will allocate and initialize a String object every time that line of code is executed.



In my project I utilize Roslyn to compile user-specified C# scripts using CSharpCodeProvider.CompileAssemblyFromSource, extract the relevant methods (MethodInfo objects) and keep them around in memory to be invoked later as needed.



My question is, do the aforementioned performance differences still apply in this case? Are const, static and other statements that are normally compile-time, still behave in that fashion when a script is compiled and executed in-memory, as described above?










share|improve this question














In a .NET application, there's a definitive performance difference between these two ways of declaring a string value:



  1. const string MyConst = "value";


  2. var MyConst = "value";


The former (#1) is a compile-time declaration so there is no performance loss at runtime, compared to the latter (#2) which will allocate and initialize a String object every time that line of code is executed.



In my project I utilize Roslyn to compile user-specified C# scripts using CSharpCodeProvider.CompileAssemblyFromSource, extract the relevant methods (MethodInfo objects) and keep them around in memory to be invoked later as needed.



My question is, do the aforementioned performance differences still apply in this case? Are const, static and other statements that are normally compile-time, still behave in that fashion when a script is compiled and executed in-memory, as described above?







c# .net performance roslyn






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 20:18









taiji123taiji123

150210




150210







  • 3





    Strings are interned. I doubt you will see any meaningful difference.

    – Robert Harvey
    Nov 15 '18 at 20:20












  • Are you interested in const generally? Or const string specifically?

    – mjwills
    Nov 15 '18 at 20:22











  • @mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

    – taiji123
    Nov 15 '18 at 20:32






  • 1





    The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

    – mjwills
    Nov 15 '18 at 20:35







  • 1





    It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

    – Hans Passant
    Nov 15 '18 at 22:31













  • 3





    Strings are interned. I doubt you will see any meaningful difference.

    – Robert Harvey
    Nov 15 '18 at 20:20












  • Are you interested in const generally? Or const string specifically?

    – mjwills
    Nov 15 '18 at 20:22











  • @mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

    – taiji123
    Nov 15 '18 at 20:32






  • 1





    The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

    – mjwills
    Nov 15 '18 at 20:35







  • 1





    It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

    – Hans Passant
    Nov 15 '18 at 22:31








3




3





Strings are interned. I doubt you will see any meaningful difference.

– Robert Harvey
Nov 15 '18 at 20:20






Strings are interned. I doubt you will see any meaningful difference.

– Robert Harvey
Nov 15 '18 at 20:20














Are you interested in const generally? Or const string specifically?

– mjwills
Nov 15 '18 at 20:22





Are you interested in const generally? Or const string specifically?

– mjwills
Nov 15 '18 at 20:22













@mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

– taiji123
Nov 15 '18 at 20:32





@mjwills, I was thinking generally, but didn't realize there was a qualitative difference/something special about 'const string' as opposed to 'const anything-else'...?

– taiji123
Nov 15 '18 at 20:32




1




1





The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

– mjwills
Nov 15 '18 at 20:35






The main 'downside' of const generally is that the the value gets inlined whenever used - stackoverflow.com/questions/24227690/… . This can mean your final binaries are larger than if you didn't use const. In practice, you are incredibly unlikely to experience issues due to this.

– mjwills
Nov 15 '18 at 20:35





1




1





It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

– Hans Passant
Nov 15 '18 at 22:31






It makes no difference. You have to use the const somewhere, it will look like var something = MyConst; Which is exactly the same as the 2nd statement. The metadata is a wee bigger for the 1st statement since it has to store the const definition, the perf deterioration due to the less efficient usage of VM is not measurable.

– Hans Passant
Nov 15 '18 at 22:31













0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327297%2fis-there-a-difference-in-performance-between-const-and-var-declarations-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327297%2fis-there-a-difference-in-performance-between-const-and-var-declarations-in-r%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto