We have released a lot of Prime Number Benchmarks posted in LearnCPlusPlus.org before, Easily Learn To Find Prime Numbers In Modern C++ and Interesting C++ Builder Compiler Counting Prime Number Benchmarks are some of tests, you can find more here with Dev-C++, GNU C/C++, VS Code examples. It is good to test mathematical speed of programming language and we are able to compare same code with other cases in different programming languages.
In this test we will test two brothers, C++ Builder and Delphi on these Prime numbers, both are great programming languages that comes with RAD Studio.
Table of Contents
1. System Description
PC setups that we used in tests here is;
OS: Windows 10 64 bit
Setup-1:
Processor AMD Ryzen 7 1800X (3.6 Ghz), 8 Core(s), 16 Logical Processor(s).
Setup-2:
Haswell: Processor Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz, 3601 Mhz, 4 Cores, 8 Logical Processors
Setup-3:
Processor AMD Ryzen 5 3600X 6-Core Processor, 3793 Mhz, 6 Core(s), 12 Logical Processor(s).
- Note that all tests below done on single core tests, do not consider number of cores
2. Calculating Prime Numbers in C++ Builder
C++ Builder includes compilers for Win32, Win64, Android and iOS. C++Builder has both CLANG Enhanced C/C++ Compiler and a Borland C/C++ Compiler. It also features a modern, high-productivity RAD Studio IDE, debugger tools, and enterprise connectivity for to accelerate cross-platform UI development. You can develop GUI based applications easily, as it comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs. There is a free C++ Builder Community Edition that can be used by students, beginners and startups with limitations.
In this test we will use same code we used before in C++ Builder.
Bruneau Babet who is one of major developers in Embarcadero is a great developer as other developers in Embarcadero and he was very helpful on this comparison topic. As a result of discussion there, In C++ Builder, we optimized Prime Time test as below,
cpptest.cpp file which has optimized is_prime() and count_prime() functions,
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 |
// Contains C++ version of 'count_primes' #if defined(USE_SQRT) #include <math.h> #endif #if 0 static bool is_prime(unsigned x) { if (x < 2) return false; if (x % 2 == 0) return (x == 2); #if defined(USE_SQRT) // Use 'sqrt' to calculate cut-off int q = sqrt(static_cast<double>(x)); for (int i=3; i<=q; i+= 2) #else for (int i=3; (i*i) <= x; i+= 2) #endif if (x % i == 0) return false; return true; } #else bool is_prime(unsigned n) { if (n <= 3) return n > 1; if (n % 2 == 0 || n % 3 == 0) return false; for (int i=5; i*i<=n; i+=6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } #endif int count_primes(unsigned maxN) { unsigned int res=0; for (unsigned i=2; i<=maxN; i++) if (is_prime(i)) res++; return res; } test.cpp |
test.cpp which links and tests both c++ and delphi/pascal codes
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 |
#include <iostream> #include <chrono> #include <assert.h> #if defined(__BORLANDC__) #include "pastest.hpp" #pragma link "pastest" #pragma link "cpptest" #endif int count_primes(unsigned maxN); // cpptest.cpp const unsigned MAX_N = 1000000; #if defined(__BORLANDC__) void testPas() { auto start = std::chrono::steady_clock::now(); int count = countPrimes(MAX_N); auto end = std::chrono::steady_clock::now(); auto diff = end - start; std::cout << "Pas: " << std::chrono::duration<double, std::milli> (diff).count() << " ms" << std::endl; assert(count == 78498); } #endif void testCpp() { auto start = std::chrono::steady_clock::now(); int count = count_primes(MAX_N); auto end = std::chrono::steady_clock::now(); auto diff = end - start; std::cout << "C++: " << std::chrono::duration<double, std::milli> (diff).count() << " ms" << std::endl; assert(count == 78498); } int main(int argc, char *argv[]) { #if defined(_WIN64) std::cout << "WIN64" #elif defined(_WIN32) std::cout << "WIN32" #endif #if defined(__INTEL_LLVM_COMPILER) " Intel " #elif defined(_MSC_VER) " Microsoft " #elif defined(__BORLANDC__) " EMB " #endif "\n"; int count = argc>1 ? atoi(argv[1]) : 1; #if defined(__BORLANDC__) for (int i=0; i<count; ++i) testPas(); #endif for (int i=0; i<count; ++i) testCpp(); } |
This below is a build.bat that compiles both Delphi and C++ files and links as test32.exe and as test64exe. Here in compilation we used -ffast-math -Xdriver -mavx parameters with -O2 optimization to compile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@echo off setlocal REM Batch file to build 32-bit and 64-bit versions of Pascal/C++ countPrime dcc32.exe -jphne -$O+ -$D0 -$L- -$Y- pastest.pas bcc32c.exe -tJ -O2 -ffast-math -Xdriver -mavx -S cpptest.cpp bcc32c.exe -tJ -O2 -ffast-math -Xdriver -mavx -c cpptest.cpp bcc32c.exe -tJ -O2 -otest32.exe test.cpp dcc64.exe -jphne -$O+ -$D0 -$L- -$Y- pastest.pas bcc64.exe -tJ -O2 -ffast-math -c cpptest.cpp bcc64.exe -tJ -O2 -otest64.exe test.cpp |
3. Calculating Prime Numbers in Delphi
Delphi is another compiled (non-interpreted) faster programming language which has support to 32 and 64bits on Windows , Android , iOS, Mac-OS and Linux Operating Systems. C++ Builder and Delphi are using RADS IDE and both has VCL and FMX UI Frameworks.
This is Delphi code modified and that we tested,
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 |
unit pastest; interface function countPrimes(MaxN: FixedUInt): integer; implementation // Port of code from https://en.wikipedia.org/wiki/Primality_test#Python_code function isPrime(n: FixedUInt): Boolean; begin if (n <= 3) then Exit(n > 1); if (n mod 2 = 0) or (n mod 3 = 0) then Exit(False); var I: FixedUInt := 5; while ((I*I) <= n) do begin if (n mod i = 0) or (n mod (I+2) = 0) then Exit(False); Inc(I, 6); end; Exit(True); end; function countPrimes(MaxN: FixedUInt): integer; begin Result := 0; for var i:FixedUInt := 2 to MaxN do begin if isPrime(i) then Inc(Result); end; end; end. |
4. 32bits and 64bits Results in Delphi and C++ Builder
To compile both cpp and delphi (pas) files;
- Run RAD Studio Command Prompt
- Go to your project folder which has codes above
- run ‘build.bat’ to compile both .pas and .cpp files into test32.exe and test64.exe files
- run ‘test32.exe 5’ , here 5 parameter runs 1M tests 5 times
- run ‘test64.exe 5’, here 5 parameter runs 1M tests 5 times
Here are the test results on the 1st Setup with AMD Ryzen 7 1800X,
These are the results from Bruneau Babet with Setup-2, HaswellProcessor Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
These are the results of Intel C++, MS C++, Embarcadero C++ Builder and Delphi from Bruneau Babet with Setup-2, Haswell Processor Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
WIN64 Intel | WIN64 Microsoft | WIN64 EMB | WIN64 EMB |
---|---|---|---|
C++: 63.5429 ms | C++: 73.6702 ms | C++: 72.6415 ms | Pas: 72.1771 ms |
C++: 63.9021 ms | C++: 72.2942 ms | C++: 73.5697 ms | Pas: 71.8799 ms |
C++: 64.2266 ms | C++: 73.7869 ms | C++: 73.2985 ms | Pas: 72.5148 ms |
C++: 63.5858 ms | C++: 72.6505 ms | C++: 72.9837 ms | Pas: 71.1668 ms |
And these results below are also from Bruneau Babet with another Ryzen CPU Setup-3, Processor AMD Ryzen 5 3600X 6-Core Processor, 3793 Mhz, 6 Core(s), 12 Logical Processor(s).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
32-bit: Delphi C++ Builder Visual C++ 103.742 ms 100.927 ms 100.609 ms 103.671 ms 101.019 ms 100.455 ms 103.554 ms 100.846 ms 100.731 ms 103.602 ms 101.029 ms 100.329 ms 64-bit: Delphi C++ Builder Visual C++ 108.305 ms 100.807 ms 100.808 ms 108.338 ms 100.557 ms 100.95 ms 108.333 ms 100.71 ms 100.815 ms 108.289 ms 100.701 ms 100.622 ms |
5. Comparison
C++ Builder vs Delphi Fight Finals here, the 32 bits results compared in a table as below,
And 64bits results compared as below,
As a result C++ Builder Wins the 32bits Round with about 3% faster results, and on the 64bits Round Winner is C++ Builder with only about 8% faster results.
6. Conclusion
C++ is one of the fastest programming language and Delphi is also another faster programming language we know, both are non-interpreted (compiled) native programming languages. I personally had tests on pascal codes many years ago (about in 98-2000) and all tests were showing C++ Builder was faster than Delphi, and in this post we found time to test these two awesome programming language again.
In this Fight!,
as a result C++ Builder Wins the 32bits Round with about 3% faster results
and on the 64bits Round Winner is C++ Builder with only about 8% faster results.
Actually, i wasn’t expecting this, i was expecting more difference between them because of my tests done before many years ago.
Here, if we look at the results the latest Delphi is also Winner !! i think because of the improvements in the last decade, it proofs that it is faster and stronger as fast as C++. This shows Delphi has great improvements in compilation during the last years, and it has very good results in 32bits compilation, 64bits compilation is also great.
These results are from runs on single core and do not consider number of cores. In real, this prime number test has few functions on runtime, not means all but i think these tests are enough to give some information about how fast the programming language. There might be more tests including more mathematics, 2D graphics, 3D graphics, GUI elements. I don’t think there will be difference between Delphi and C++ Builder, and not sure about graphics.
We also applied some tests with Intel and Visual C++ compilers on this new optimized Prime Time tests as given above, C++ compilers were had about same results with very small changes.
In conclusion, amazingly these results mean you can use both of these C++ Builder and Delphi programming language in all cases; including heavy mathematics, AI technologies, ML applications, Image Processing, Parallel Programming, Engineering Simulations, Games etc. Probably there will be only 3-8% difference in these tests.
Can we speed up more on both Delphi and C++ Builder? Of course Yes! We just used single core, we have also tested on parallel programming which uses all cores, We can also speed-up these results by using CPU and GPU together. Please keep in touch with LearnCPlusPlus.org.
Get started building powerful apps with C++Builder!