471,049 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,049 software developers and data experts.

Local or global variables in frequently called functions?

Let me start by saying that this is more a question about principle
than practice - with the speed of today's computers it's probably
rarely an actual issue. Still I'd like to know...

If I have a function that is called thousands of times per second, it
seems to me that, performance-wise, it would be best to make all
variables used in it global, so that memory for them doesn't have to
be allocated and released with each call. However, I know this
clutters up the name space and can make the code more bug-prone.

Would it ever make sense to use global variables over local ones in
this situation? Would the same answer apply to threads instead of
functions? Thanks for satisfying my curiosity!
Jun 27 '08 #1
1 1550
On 2008-04-13 22:56, danep2 wrote:
Let me start by saying that this is more a question about principle
than practice - with the speed of today's computers it's probably
rarely an actual issue. Still I'd like to know...

If I have a function that is called thousands of times per second, it
seems to me that, performance-wise, it would be best to make all
variables used in it global, so that memory for them doesn't have to
be allocated and released with each call. However, I know this
clutters up the name space and can make the code more bug-prone.

Would it ever make sense to use global variables over local ones in
this situation? Would the same answer apply to threads instead of
functions? Thanks for satisfying my curiosity!
Since local variables are allocated on the stack the cost of allocation
is virtually zero (it comes for free with the stack-frame creation). If,
however, the variables contains an object with non-trivial constructor
and/or destructor you would have to pay the price of running these on
each function call. If that is not acceptable you might use static
variables.

Using static variables is unfortunately not a very good idea if you use
threads since two threads running concurrently would be accessing the
same variables at the same time, to solve this you would have to use
either normal local variables, or thread-local variables (if your
platform supports it). Either way, global variables is not a good idea.

--
Erik Wikström
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by mrbog | last post: by
8 posts views Thread by Dan | last post: by
10 posts views Thread by Matt | last post: by
7 posts views Thread by Michael | last post: by
10 posts views Thread by ankisharma | last post: by
23 posts views Thread by Timothy Madden | last post: by
23 posts views Thread by deepakvsoni | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.