GNU C/C++ Compiler (gcc , g++)
GNU GCC is another powerful C/C++ compiler originally written as the compiler for the Unix, Linux and GNU operating system. The GNU system was developed to be 100% free software, and it is efficient with both 32-bit and 64-bit operations. This compiler can be used on Windows using MinGW or CygWin Linux Simulators. Compiled executable files need a small dll to run independently on Windows. It is mostly compatible with CLANG standards. GCC Compiler can be used by Visual Studio Code, Dev C++, Code Blocks, and can be directly used with Command Prompt or PowerShell. It is available on the official product page https://gcc.gnu.org/.
This is a C++ Builder example to find number of prime numbers in a given range. In this example we are measuring calculation time of count_primes() function in five times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <stdio.h> #include <iostream> #include <math.h> #include <time.h> using namespace std; #define max_n 1000000 //--------------------------------------------------------------------------- bool is_prime(unsigned int x) { if(x<=1) return(false); unsigned int q = floor(sqrt((float)x)); for(unsigned int i=2; i<=q; i++) { if((x%i)==0) return(false); } return(true); } //--------------------------------------------------------------------------- int count_primes(unsigned int maxN) { unsigned int res=0; for(int i=2; i<=maxN; i++) { if(is_prime(i)) res++; } return(res); } //--------------------------------------------------------------------------- int main() { unsigned int count; clock_t start,end; for(int k=0; k<5; k++) { start = clock(); count = count_primes(max_n); end = clock(); cout << "Number of primes between 0 to " << max_n << "= " << count << "\n"; cout << " Elapsed time:" << end-start << " ms" << "\n"; } return(0); } |
We compiled and run this code on Windows by using MinGW linux simulator. Here are the results on run time.