0

I have looked up all the "more efficient" posts on here and none answers this directly, so I need some big brain to help me out.

I currently store settings in app.config and get the values as I need them. I use this method:

public static string AppConfig_GetKeyValue(string strAppSettingKeyName)
        {
            ConfigurationManager.RefreshSection("appSettings");
            string myAppSettingValue = ConfigurationManager.AppSettings[strAppSettingKeyName];
            return myAppSettingValue;
        }

The QUESTION is ... if I'm doing a ton of operations, like SQL inserts or writing info to the screen or terminal, is it better to store that value globally for speed/efficiency? For example:

If on EVERY SQL insert, we check the app.config for the value:

<add key="mySQLLogging" value="true"/> 

Versus declaring a global like:

public static bool mySQLLoggingOn = bool.Parse(TTLog.AppConfig_GetKeyValue("mySQLLogging"));

Which is better? Maybe a brief explanation as to why?

As a second example, I log things to the screen for the user, but include the text DEBUG in each line if I want detailed information, but don't want to show it unless that mode is "true" in app.config:

public static void DoWriteLine(string strMessage, bool blnShowDateStamp = true, ConsoleColor clrForeColor = ConsoleColor.Green, ConsoleColor clrBackColor = ConsoleColor.Black)
    {
        if (strMessage.ToLower().Contains("debug") && !(bool.Parse(AppConfig_GetKeyValue("myModeDebugOn"))) 
            return; // If app.config key is false, don't process message to the screen
        Console.ForegroundColor = clrForeColor;
        Console.BackgroundColor = clrBackColor;
        if (blnShowDateStamp)
            Console.WriteLine(("[ " + DateTime.Now.ToString("ddd MM/dd/yyyy hh:mm:ss.fff") + " ] -> " + strMessage).PadRight(Console.WindowWidth - 1));
        else
            Console.WriteLine((strMessage).PadRight(Console.WindowWidth - 1));
        Console.ResetColor();
    }   

The above, obviously, corresponds to the key in the app.config:

<add key="myModeDebugOn" value="True"/>

Which then, potentially every second, has to process this:

if (strMessage.ToLower().Contains("debug") && !(bool.Parse(AppConfig_GetKeyValue("myModeDebugOn"))) 
                return;

So are all these file read operations WAY less efficient? I just remember from day one of learning programming, NEVER use global variables, it's bad.

Thanks!

Mike Hawk
  • 41
  • 4
  • 1
    When asking about efficiency, it is really hard to tell... You need to set up benchmarks and measure it yourself. Results greatly depend upon particular machines, so even running benchmarks on your PC would give just some insight, not definitive answer. – Michał Turczyn Feb 06 '21 at 22:01
  • Calling that RefreshSection means that you want to reread the configuration without stopping the program. Is it allowed to change the configuration while the program runs? This could be expensive. You need to test your code with and without that line. Of course without it you should get a better timing. – Steve Feb 06 '21 at 22:21
  • 1
    [Obligatory link to Eric Lippert's rant on performance questions](https://ericlippert.com/2012/12/17/performance-rant/). Basically, measure it for yourself under the circumstances in which you are running the code. – Heretic Monkey Feb 06 '21 at 22:42
  • Thanks @HereticMonkey and Michal ... I guess I'll have to learn how to benchmark now ... was curious if someone else had already experienced globals vs file reads, but I'll figure it out I guess. Thx. – Mike Hawk Feb 08 '21 at 04:41

1 Answers1

1

Just don't refresh the section in the first place and the variable is already stored in a "global" variable. Refreshing will force a file read, which is an IO operation and thus expensive.

Check Does ConfigurationManager.AppSettings[Key] read from the web.config file each time? to see the dissasembled code of the AppSettings property. The array is loaded once and then it's accessible via the static property.

Same as your own static variable.

So this:

public static string AppConfig_GetKeyValue(string strAppSettingKeyName)
        {
            // ConfigurationManager.RefreshSection("appSettings");
            return ConfigurationManager.AppSettings[strAppSettingKeyName];
        }

Is almost equivalent to your own global static variable. I would expect minimal differences, mostly based on machine load at the time of the benchmarks between those two.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • 1
    Indeed. That line serves only the purpose to reload the section in case a change is expected to happen in the appSettings section once the program started. If the asker expects changes to happen then there is no point in keeping a global variable to avoid an inexistent delay in reading the section key from file – Steve Feb 06 '21 at 22:47
  • My problem was, without the refresh, I was not getting the actual value. If the value was changed somewhere else, I would not get the new value and the refresh solved the problem ... but ... may be creating more overhead. – Mike Hawk Feb 08 '21 at 04:40