c0dehunter
c0dehunter

Reputation: 6150

Performance: should I use a global variable in a function which gets called often?

First off, let me get of my chest the fact that I'm a greenhorn trying to do things the right way which means I get into a contradiction about what is the right way every now and then.

I am modifying a driver for a peripheral which contains a function - lets call it Send(). In the function I have a timestamp variable so the function loops for a specified amount of time.

So, should I declare the variable global (that way it is always in memory and no time is lost for declaring it each time the function runs) or do I leave the variable local to the function context (and avoid a bad design pattern with global variables)?

Please bear in mind that the function can be called multiple times per milisecond.

Upvotes: 8

Views: 2590

Answers (4)

Non-maskable Interrupt
Non-maskable Interrupt

Reputation: 3911

For drivers, with is usually position independent, global variables are accessed indirectly with GOT table unless IP-relative operations is available (i.e. x86_64, ARM, etc)

In case of GOT, you can think it as an extra indirect pointer.

However, even with an extra pointer it won't make any observable difference if it's "only" called in mill-second frequency.

Upvotes: 0

John Dibling
John Dibling

Reputation: 101456

Did you profile a fully-optimized, release build of your code and identify the bottleneck to be small allocations in this function?

The change you are proposing is a micro-optimization; a change to a small part of your code with the intent to make it more efficient. If the question to the above question is "no" as I'd expect, you shouldn't even be thinking of such things.

Select the correct algorithm for your code. Write your code using idiomatic techniques. Do not write in micro-optimizations. You might be surprised how good your compiler is at optimizing your code for you. It will often be able to optimize away these small allocations, but even if it can't you still don't know if the performance penalty imposed by them is even noticeable or significant.

Upvotes: 2

TRKemp
TRKemp

Reputation: 518

Speed of execution shouldn't be significantly different for a local vs. a global variable. The only real difference is where the variable lives. Local variables are allocated on the stack, global variables are in a different memory segment. It is true that local variables are allocated every time you enter a routine, but allocating memory is a single instruction to move the stack pointer.

There are much more important considerations when deciding if a variable should be global or local.

Upvotes: 7

barak manos
barak manos

Reputation: 30136

When implementing a driver, try to avoid global variables as much as possible, because:

  1. They are thread-unsafe, and you have no idea about the scheduling scheme of the user application (in fact, even without threads, using multiple instances of the same driver is a potential problem).

  2. It automatically yields the creation of data-section as part of the executable image of any application that links to your driver (which is something that the application programmer might want to avoid).

Upvotes: 5

Related Questions