Macros v/s Functions

In this article we'll be learning how we can improve our C code by using Macros instead of functions. How simple functions can be converted into macros to save on some CPU instructions. Some basic knowledge of C and a little bit Assembly is considered as pre-requisites.

The Code

We'll be testing a basic C code, that Squares a number '10' and displays it on the screen.

Differences

From the Comparison above its quite trivial to note the differences :-

1. Size

The size of the “Function.c” bytecode is 9 bytes more than that of “Macros.c” bytecode.

2. Instructions

We are 12 instructions in Function.c:main, while we are using 10 instructions in Macros.c:main.

The differences is mainly because we are actually wasting 2 instructions, 1st to load the 'esp' with the input and 2nd to Call the function. Why? Because in Macros.c, the Preprocessor actually hardcodes "10*10", instead of calling a function.