I had a couple thoughts on this. The first is that allocating global variables may be faster, since they are only allocated once, at the time the program is first spawned, whereas local variables must be allocated every time a function is called. My second thought is that since local variables are on the stack, they are accessed through the base pointer register, so the value stored in the base pointer must be decremented every time a local variable is accessed; global variables are accessed directly through their static addresses in the data segment. Is my thinking accurate?
Are global variables faster than local variables in C?
7.1k Views Asked by user628544 At
1
There are 1 best solutions below
Related Questions in C
- Passing arguments to main in C using Eclipse
- kernel module does not print packet info
- error C2016 (C requires that a struct or union has at least one member) and structs typedefs
- Drawing with ncurses, sockets and fork
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- Configured TTL for A record(s) backing CNAME records
- Allocating memory for pointers inside structures in functions
- Finding articulation point of undirected graph by DFS
- C first fgets() is being skipped while the second runs
- C std library don't appear to be linked in object file
- gcc static library compilation
- How to do a case-insensitive string comparison?
- C programming: Create and write 2D array of files as function
- How to read a file then store to array and then print?
- Function timeouts in C and thread
Related Questions in VARIABLES
- Simplexml get path from variable
- Python - Dynamically naming variables
- Azure Web App PATH Variable Modification
- How can I pass variable to ansible playbook in the command line?
- Fetching URL vars into form and submitting to other page
- Iframe not passing url parameters
- Stata: Retrieve variable label in a macro
- How to make all variables in javascript script have two decimal points
- Javascript works with HTML but not in Rails
- Running Line of code twice causes activeX error 0x8000ffff
- Erlang syntax error unclear
- CMD specifying columns to save?
- Simplify code with sending Methods as Variable
- Calling a variable in Matlab without using the full name?
- How to call c-style cleaner functions implicitly?
Related Questions in SCOPE
- Python: Variable scope for nested generator expression
- angular load more tweets onclick
- Local variable scope in Java vs Python
- Maddening scope issue
- Compiling ES6 arrow functions to Es5 using Babel.js
- Is it possible in matlab to declare functions that will be accessible from other m files in a similar way as in c coding?
- Memory allocation of local variables within nested {}
- angular unit testing $scope.$on in a service not working
- Scope rails redirect to index
- Expire specific managed bean instance after time interval
- JavaScript function parameter and scope
- Scope of a variable and function name
- importing function giving scoping error
- javascript: scoping and accessing nested entities
- Block scope, function scope and local scope in Javascript
Related Questions in GLOBAL-VARIABLES
- How do you run a javascript outside of the localhost directory with requirejs?
- Use same Python variable in multiple if-statements
- Declaring global char array in C header file
- Add global string to checkedlistbox?
- How to Declare Global Array Variable in SAS?
- What will be the possible causes for the 1st code fragment to output differently than 2nd and 3rd?
- Unexpected performance with global variables
- Using ng-init to call controller functions and passing vars between controllers
- Defining Global Variables or passing variables through function in Matlab?
- Global List not updating when using multiprocessing in python
- Difference between static and global variables
- Javascript, using one single local object instead of many local variables
- How do I organize input global variables?
- c++ program crash when creating global instance of class whose constructor references a global variable
- Javascript - Calling A Variable From Outside Of If/Else Block Returns Undefined
Related Questions in LIFETIME
- Does <'a, 'b: 'a> mean that the lifetime 'b must outlive the lifetime 'a?
- Getting 'Missing Lifetime specifier' error
- Generic fn, channel, and thread spawn
- Mismatch in Number/Types of Arguments
- Struct vs enum lifetime differences
- Lifetime parameters for an enum within a struct
- How can I specify lifetimes in associated types?
- Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime
- Acessing data from a global struct, gives error "borrowed value does not live long enough"
- Are global variables faster than local variables in C?
- Explanation for the output of the code
- Rust lifetime error
- Is it possible to disable Rust's lifetime elision?
- return a closure that mutates its environment
- Why I can't return fmt::Arguments<'a> from &'a T?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
It's rather inaccurate.
If you study computer architecture, you'll find out that the fastest storage are the registers, followed by the caches, followed by the RAM. The thing about local variables is that the compiler optimizes them to be allocated from the registers if possible, or from the cache if not. This is why local variables are faster.
For embedded systems, sure it might be possible to compile to a tiny memory model in which case your data segment may possibly fit into a modern controller's SRAM cache. But in such cases, your local variable usage would also be very tight such that they are probably operating entirely on registers.
Conclusion: In most cases, local variables will be faster than global variables.