Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading environment variables is slow operation?

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

like image 287
Narek Avatar asked Sep 18 '11 09:09

Narek


2 Answers

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).

like image 75
Merlyn Morgan-Graham Avatar answered Sep 24 '22 06:09

Merlyn Morgan-Graham


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.

like image 45
Mark Hamlin Avatar answered Sep 20 '22 06:09

Mark Hamlin