开发者

Reading environment variables is slow operation?

If it is right that working with environment variables is slower than with o开发者_高级运维rdinary variables (in scripting languages?) then how it is explained?


Retrieving the value of an environment variable will incur a system call. Ordinary variables are built into the language you are running in, will be in the same address space, and might even be in a CPU register (depending on the language and how it is executed).

It's simply a longer trip to get the data.

That being said, it probably won't be noticeably slow in most scenarios. Unless you're accessing them very often (e.g. constantly using environment variables while in a tight loop, or reading them on a web server during every web request), I wouldn't worry about the performance difference.

Edit:

Turns out the answer is, it depends:

  • https://stackoverflow.com/a/10636826/232593

  • https://stackoverflow.com/a/7460612/232593

Whether the performance difference actually matters at all or not is situational. And in all cases, you should probably measure the performance difference first for your very specific situation, before spending a lot of time worrying about it (what language? what OS? is it faster if you locally cache, etc).


Depends on the language & interpreter design. If the environment is read on initialisation and exposed via standard global variables like in php, there will be no performance difference, with the disadvantage that external changes to the env variable are not seen in the program.

There are however alternative implementations which while more "expensive", offer advantages, performance aside.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜