13

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

4
  • 1
    The couple of dozen nanoseconds it takes is not observable in an interpreted scripting language. Commented Sep 18, 2011 at 9:26
  • @Hans: A blanket statement, tho I agree with it :) Commented Sep 18, 2011 at 9:28
  • Please note my comment under the accepted answer. I assume the author will edit their answer so there's no reason for me to write a new one, however I figured I should notify the OP, so here it is.
    – Hi-Angel
    Commented Apr 23, 2021 at 10:24
  • Though i have no resources, but for example in python and in javascript it's recommended to do one system call, save all env vars and then read from dictionary (object) if you have many variables, but i'm not sure how much it's beneficial in regard to performance. May be faster only if many calls, which is not so usual i guess. Commented Aug 13, 2021 at 11:41

3 Answers 3

14

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:

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

7
  • You mean that system calls are slow operations? Why? Aren't they a simple, ordinary functions?
    – Narek
    Commented Sep 18, 2011 at 9:24
  • 1
    @Narek: Nope, they're not always so simple :) Depending on the specific call you are making (not sure for environment variables, and even then it would probably be OS dependent), it might have to cross process and address space (user -> kernel) boundaries. A function in your own app wouldn't have to do this - it would be in the same process, and the same address space. Commented Sep 18, 2011 at 9:26
  • Thanks now I understand why it is slow! Just I need to concatenate your and Mark Hamlin answers in order to have the full picture.
    – Narek
    Commented Sep 18, 2011 at 9:34
  • So many upvotes for the wrong answer. For starters, your answer contradicts this one, so someone is wrong here. To figure out who I conducted an experiment: I wrote a testcase that retrieves an env. variable, then I looked at the list of system calls. Here's the log. It can be seen that there is no syscall between the print "Before retrieving" and the next one that prints the variable. With that said, there might be some platforms where it is different, but in general what you say is not necessarily true.
    – Hi-Angel
    Commented Apr 23, 2021 at 10:21
  • I wrote a small Console App that reads a variable from the ENV, assuming it is set, I had an average impact of 11ms in 100000 iterations, not sure how representable that is, but I expected it to take longer to be honest...
    – Thiemo
    Commented Mar 27, 2023 at 7:18
7

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.

3
  • 1
    "There are however alternative implementations which while more "expensive", offer advantages, performance aside." - can you elaborate on this point a bit further? Commented Sep 18, 2011 at 9:49
  • As you indicate in your response, an alternative is a system call. While this will take longer, you'll the get the current value, instead of the value at initialisation. Commented Sep 18, 2011 at 17:55
  • Are you aware how it works in Golang? Commented May 10, 2023 at 8:11
0

Yes, on Unix-like systems it's a bit slower, but no system calls.

On Unix-like systems (e.g. Linux), environment variables are passed to processes in a block of memory, in this form:

A=xxx\0B=yyy\0...

It's null-terminated strings in the form of NAME=value. So to get an environment variable's value, the program has to parse this format to find the string you want, and the value is always a (C-style) string.

In high level programming languages, the runtime (or the script engine) may help you by parsing it once at startup, so later access can be faster, but it's still a dictionary (or map, or whatever your language calls it) lookup. Local variables can usually be accessed without such lookups (it knows where the value is).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.