简体   繁体   English

注册表调用与在静态变量中存储用户选项的效率

[英]Efficiency of registry call vs. storing user options in a static variable

I have a program that stores some user options in the registry (about 5 options). 我有一个程序,在注册表中存储一些用户选项(约5个选项)。 The options are fetched from the registry within an inline function. 选项是从内联函数中的注册表中获取的。 The options need to be checked several times during run time. 在运行时需要多次检查选项。 More specifically, the options are checked inside a function that may be called upwards of a 100 times during one routine. 更具体地说,在一个例程中检查选项可以在一个例程中向上调用100次。

My question is which would be more efficient: 1) Call the inline function which gets the option from registry every time the option needs to be checked; 我的问题是哪个更有效:1)每次需要检查选项时,调用内联函数从注册表中获取选项; or 2) Call the inline function once and then store the result in a static variable, which will then be used to check the option. 或者2)调用内联函数一次,然后将结果存储在静态变量中,然后将其用于检查选项。

Please note that I am not concerned with options being changed during run time as they are rarely changed and do not need to take effect until the next run of the program. 请注意,我不关心在运行时更改选项,因为它们很少更改,并且在下一次运行程序之前无需生效。

Any feedback would be highly appreciated. 任何反馈都将受到高度赞赏。

From a theoretical performance perspective, it seems quite obvious that cached variables will be more efficient than repeated registry access (which incurs system calls and maybe even disk I/O, as opposed to mere memory accesses if the settings are cached). 从理论性能的角度来看,似乎很明显缓存变量比重复注册表访问更有效 (这会导致系统调用甚至是磁盘I / O,而不是仅仅在缓存设置时进行内存访问)。 But as noted by @MarkRansom in the comments, 100 registry accesses are unlikely to make a big difference to your program's performance unless your routine is called very often (eg. in a tight loop). 但正如@MarkRansom在评论中指出的那样,100个注册表访问不太可能对您的程序性能产生很大影响,除非您经常调用例程(例如,在紧密循环中)。

As usual with any performance/optimization problem: you shouldn't bother unless you actually know that this poses a performance issue (eg. your profiler tells you so, or you can easily prove it yourself). 与通常的任何性能/优化问题一样:除非您确实知道这会造成性能问题(例如,您的分析器告诉您,或者您可以轻松地自己证明),否则您不应该费心。


However, there is another issue at hand. 但是,还有另一个问题。

You say " I am not concerned with options being changed during run time " but IMHO you should: what happens if the user changes an option while your program is executing? 你说“ 我不关心在运行期间被更改的选项 ”,但恕我直言,你应该:如果用户在你的程序执行时更改了一个选项,会发生什么? I mean, you've started a computation based on specific options which induce specific assumptions, and suddenly the option changes. 我的意思是,您已经开始基于特定选项的计算,这些选项会导致特定的假设,并且突然选项会发生变化。 It could easily mess up your invariants/assumptions and introduce coherency problems. 它可能很容易搞乱您的不变量/假设并引入一致性问题。

So, irrelevant of the performance issues, you should always cache your user-defined settings in variables so that if they get modified by the user at runtime your program stays coherent. 因此,与性能问题无关, 您应始终将用户定义的设置缓存在变量中,这样如果用户在运行时修改它们,您的程序就会保持一致。

In other words, to me it's not so much a performance matter but rather a program correctness matter. 换句话说,对我来说,不仅仅是表现问题,而是程序正确性问题。

@CaptainObvlious raises an interesting point: if you actually need to update your settings whenever the user (or another application) changes them, then do it in a controlled manner (as he suggests, monitoring the registry is one way) and updating your cached variables only when it is fit to do so. @CaptainObvlious提出了一个有趣的观点:如果您确实需要在用户(或其他应用程序)更改它们时更新您的设置,那么以受控方式(如他所建议,监视注册表是一种方式)并更新缓存变量只有在适合这样做的时候。 This ensures that your settings won't change in the middle of a computation when this computation actually expects the same settings throughout. 这可以确保在计算过程中实际需要相同的设置时,您的设置不会在计算过程中发生变化。

Your best option is going to be caching the settings in variables. 您最好的选择是在变量中缓存设置。 When you read the value you'll end up with a (single) load instruction vs calling a system API which may do file I/O or other time consuming tasks to retrieve the value. 当您读取该值时,您将最终使用(单个)加载指令与调用系统API,该系统API可能会执行文件I / O或其他耗时的任务来检索该值。 If you do need to deal with settings being updated by external applications you can always monitor the registry for changes - which may require additional locks for multi-threaded access. 如果确实需要处理外部应用程序正在更新的设置,则可以始终监视注册表中的更改 - 这可能需要额外的锁以进行多线程访问。 Even then the performance will still be significantly greater than always reading the registry. 即便如此,性能仍将远远高于总是阅读注册表。

[See syam's answer for some considerations about program correctness. [有关程序正确性的一些考虑,请参阅syam的答案。 It's something you should keep in mind for multiple settings that affect one another.] 对于相互影响的多个设置,您应该记住这一点。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM