embedded
embedded

Reputation: 11

Get data by Return vs passing pointer

int get1(void) { return test; }
void get2(int* arg) { *arg = test;}

static int test = 5;

int main()
{
int result = 0;

result = get1();

get2(&result);

}

get1() vs get2() - which one performs better for embedded system?

Upvotes: 1

Views: 223

Answers (2)

TheCodeArtist
TheCodeArtist

Reputation: 22477

get1() is usually faster on common architectures. (ARM, MIPS, x86 etc.)

Reasons :

  • get2() uses an additional 4bytes of memory on the stack to pass the pointer itself. This introduces 2 additional memory accesses i.e. a PUSH arg to stack before calling get2() and a POP arg from stack within the function before dereferencing it.
    Note that depending on the calling-conventions the actual parameter arg may be passed using the CPU register. But even then the register needs to be backed-up before calling get1() within the calling-function and restored afterwards (i.e push/pop to/from stack).

  • get2() dereferences a pointer and performs a write to that memory location. On a system with write-through cache, it will add an additional delay due to the memory access. get1() can avoid this delay by using one of the CPU registers to hold the return value as is common on most architectures.


A general rule of thumb is to reduce the number of parameters to be passed.

Usually this is interpreted to mean the use of pointers to an array/structure containing a bunch of individual parameters is better; i.e. 1 param (pointer) is better than N params (individual values).

In the above case however, passing the return value by val (i.e. using return and NOT by ref) allows the function to be implemented without using any parameters at all; i.e. 0 params is better than 1 param.

Upvotes: 5

DaoWen
DaoWen

Reputation: 33019

The fact that you're on an embedded system means we have no idea what architecture you're using, so we can only guess about the performance.

However, passing scalar values (i.e. not structs or arrays) via return is usually faster than using a pointer. Using a pointer would require the value to be written to memory, whereas all the architectures I'm familiar with will use a dedicated register for returning an int. This means that get1 just needs to load the value from test into a register, whereas get2 might have to do a memory-to-memory move, or even a load followed by a store (depending on the architecture).

Upvotes: 2

Related Questions