How to speed up Program execution using Pragma in C++
In this tutorial, we will learn about How to speed up Program execution using Pragma in C++.
For that, we first need to know all about Pragma.
So, Pragma is a pre-processor directive. CPP file or C file may include no. of header files, suppose one CPP and other CPP may include the similar .h file so the compiler has to compile both .h file or go to that .h again twice. So, we use Pragma so that compilation will be done only once.
Advantage of using Pragma:
- Since the compilation is done only once so the code is less.
- Avoidance of name clashes.
- The compilation of one file in place of the various files will increase the compilation speed.
And we more focus here about the increase in compilation speed.
Types of Pragma Directive in C++:
Pragma Directive is of three types:
- First one is #pragma startup
- The second one is #pragma exit
- And the third one is #pragma warn
A pragma startup is a type of directive which makes any function run before the main function. There is no need to call the function written with this because that function run automatically when the program runs.
A pragma exit is a type of directive through which we can run any function when our main function is over.
So the function which we write with pragma exit run after the main and the function which we write with pragma startup run before the main.
#include <iostream.h> #include <conio.h> void before(void); void after(void); #pragma startup before #pragma exit after void before(void); void after(void); void main() { cout<<"\n We are in main function"; getch(); } void before(void) { clrscr(); cout<<"\n We are in startup function"; } void after(void) { clrscr(); cout<<"\n We are in exit function"; }
Output:
We are in startup function We are in main function We are in exit function
Here in this program pragma startup calls first then the main function and in last pragma exit function is called. And to use these function we don’t need to put brackets and the i/p of these functions will be always void because they don’t take any i/p.
A pragma warn is a type of directive which allows one to show or hide the warnings during compilation. We can use it in many ways:
- #pragma warn -rvl
When we use -rvl then all the warnings related to return value are solved. Like when we write any datatype in the return type and we don’t use any return statement anywhere in the program then compiler warning shows a warning that this function returns a value but if we use -rvl then warning is removed.
- #pragma warn -par
-par means parameter not used. In a program, if we declare a variable and not use that variable anywhere in the program then the compiler shows a warning that the parameter is not in use. This error can be removed by using -par.
- #pragma warn -rch
When in a program we don’t write any return type of main but in the program, we return 0; and below it, if we write getch(); or anything then the compiler generates a warning that the statements written below are useless, they never run during execution so they are unreachable, so in that case, the warning can be removed by using -rch.
The below code is given to explain all the pragma warn.
#include<iostream.h> #include<conio.h> //#pragma warn -rvl //#pragma warn -rch //#pragma warn -par int display(int); void main() { int a =10; cout<<"We are in main function"; show(a); return; getch(); } int show(int m) { cout<<"We are in show function"; }
When we comment on the pragma directives then three errors are shown but as we remove the comment all errors are removed and the output is given below.
Output:
We are in main function We are in show function
Thus Pragma in C++ is very useful to make our program run faster.
Also read: Finding the duration of an MP3 File in C++
Leave a Reply