Skip to content

dev PerformanceCoding

oguyon edited this page Apr 10, 2021 · 1 revision

Performance Coding Tips

1. const and restrict

A few guidelines to provide information to the compiler (and the programmer).

Use const

const float pi = 3.14;
printf("pi = %f\n", pi);

Use restrict type qualifying for non-aliased pointers

errno_t examplefunction(
    const char *__restrict__ stringinput
)
{
	printf("string : %s\n", stringinput);
	return RETURN_SUCCESS;
}

2. Function Attributes

For details, see GCC Common Function Attributes

pure

void __attribute__ ((pure)) myfunction()
{
	// function that has no observable effects on the state of the program other than to return a value
}

hot

void __attribute__ ((hot)) myfunction()
{
	// some performance-critical code
}

cold

void __attribute__ ((cold)) myfunction()
{
	// some non-performance-critical code
}