• Uncategorized

About performance : Reading-environment-variables-is-slow-operation

Question Detail

If it is right that working with environment variables is slower than with ordinary variables (in scripting languages?) then how it is explained?

Question Answer

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.


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.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.