In this post we compared runtime performance of C++ Builder Console Application with CLANG Compiler and GCC Compiler.
Prime numbers are interesting area to research. A prime number, it is also called prime shortly , is a natural number (a positive integer) greater than 1 that is not a product of two smaller natural numbers. If a number is not prime then it is called as a composed number. There are several mathematical questions regarding prime numbers are still unsolved. Finding them and relations and their effects on some other functions, graphics really interesting in mathematical phenomena. We tested Prime numbers before in this Easily Learn To Find Prime Numbers In Modern C++ post. We have made few changes to check if compiled 32bits or 64bits as below. Here is the Counting Prime Numbers example for a C++ Builder Console Application.
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 49 50 51 52 53 54 55 56 |
#include <stdio.h> #include <iostream> #include <math.h> #include <time.h> //--------------------------------------------------------------------------- #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 _tmain(int argc, _TCHAR* argv[]) { unsigned int count; clock_t start,end; std::cout << "Testing is_prime(" << max_n << ") "; #if _WIN32 || _WIN64 #if _WIN64 std::cout << "64bits\n"; #else std::cout << "32bits\n"; #endif #endif for(int k=0; k<5; k++) { start = clock(); count = count_primes(max_n); end = clock(); std::cout << "Number of primes between 0 to " << max_n << "= " << count << " Elapsed:" << end-start << " ms\n"; } getchar(); return 0; } |
And the results are;
Here below is the Counting Prime Numbers example for a GNU C++ Console Application. We used Visual Studio Code with GCC Compiler on runtime.
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#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; cout << "Testing is_prime(" << max_n << ") "; #if _WIN32 || _WIN64 #if _WIN64 cout << "64bits"; #else cout << "32bits"; #endif #endif #if __GNUC__ #if __x86_64__ || __ppc64__ cout << "GNUC 64bits\n"; #else cout << " GNUC 32bits\n"; #endif #endif 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 << " Elapsed:" << end-start << " ms" << endl ; } return(0); } |
And results are;