getenv 是否缓存结果? [英] Does getenv cache the result?
问题描述
我在我的代码中多次调用 getenv
(多次调用),所以我看到了优化的潜力.我的问题是,getenv
是在内部以某种方式缓存结果,还是在每次调用时查询环境变量?
I have several calls to getenv
in my code(called a lot of times), so I see the potential for an optimization. My question is, does getenv
somehow cache the result internally, or does it query the environment variables on each call?
我已经对代码进行了概要分析,getenv
不是瓶颈,但如果它更有效,我仍然想更改它.
I have profiled the code, getenv
is not a bottleneck, but I'd still like to change it if it's more efficient.
作为一个附带问题,可以在程序运行时更改程序的环境变量吗?我没有这样做,所以在我的情况下缓存结果是安全的,这只是出于好奇.
As a side question, can an environment variable be changed for a program while it is running? I'm not doing that, so in my case caching the result would be safe, it's just curiosity.
推荐答案
环境变量通常存在于给定进程的内存中,因此没有什么可缓存的,它们随时可用.
Environment variables usually live in the memory of given process so there is nothing to cache there, they are readily available.
至于更新,正在运行的进程的任何组件都可以调用 putenv
来更新环境,如果您希望发生这种情况,则不应长时间缓存它.
As for updates, any component of a running process can call putenv
to updated the environment, you should not cache it for prolonged periods if you expect that to happen.
这篇关于getenv 是否缓存结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!