In this post, you’ll get answers to these questions:
- How can I increase arithmetic speed in C++ Builder?
- How can I optimize my C++ Builder application?
- Can I use -O0, -O1, -O2, or -O3 optimization options in Embarcadero’s C++ compiler, the same as in GNU C/C++ and other C++ compilers?
- Where can I find more information about C++ optimization?
Arithmetic Speed is important in computational areas such as high DPI image processing, data mining, and complex engineering analysis like Fluid Mechanic analysis. A relatively new and increasingly common area of challenges for computational efficiency is those of machine learning, deep learning, and similar AI operations. In this post, we present 5 important areas you can look at to speed up and optimize these math-heavy computational operations in C++ Builder. Throughout we used the latest RAD Studio with the CLANG compiler.
The RAD Studio 11 release introduces several new features, enhancements, and quality updates in key areas including:
- You can provision apps for Windows 11 and the Windows 11 app store.
- Apps can be compiled for Android API 30.
- You can compile for macOS M-series (Apple Silicon) processors.
- Apps can be designed on and for high-DPI 4k+ screens and similar.
- You can now view VCL Styles in design time.
- There are new & modernized VCL components providing some great new functionality.
- There is enhanced remote desktop support to make it easier to collaborate or work remotely.
What does optimization mean in C++?
Mathematical optimization is really important in programming. Mathematical Optimization, also called Mathematical Programming is the selection of the best mathematical elements or functions with regard to some criterion, from some set of available alternatives. In programming, optimization problems of sorts arise in all quantitative disciplines from computer science and engineering to operations research and economics, and the development of solution methods has been of interest in mathematics for centuries.
If you click on the C++ Compiler section you will see Compilation Options, Compiler Options, and General Options. Details about these options can be found here in the official DocWiki of Embarcadero. These options may help your arithmetic speeds. For example, you can enable RTTI you can set to Modern Instruction set to Pentium Pro for 32bit Intel applications. Data alignment improves your arithmetic’s on data. In default, it is Quad Word, in some specific applications you can set it to Word or Byte to reduce data and to speed up arithmetic on this data. You can use CLANG compiler instead of classic Borland Compiler because CLANG is being standard in C++ programming.
Here are the default RAD Studio 11 C++ Compiler settings
Note that some of these options may result to you application not running on older CPU/GPU architectures.
What optimization options are available in C++ Builder?
C++ Builder, Visual C++, Objective-C++ and GNU C++ have optimization options which are generalized as -O0, -O1, -O2, -O3. In RAD Studio 11, Every C++ Builder “Debug” project comes with no optimization (-O0). You can use -O2 optimization option in debugging also. That makes affect your arithmetic operations. You can use -O3 (means slower compilation) optimization to speed up more if needed. Release
Here are the default “Release” optimization settings in RAD Studio 11.
More details about these optimization options can be found here in the official DocWiki of Embarcadero.
You should know well about optimization options -O0, -O1, -O2, -O3 and other necessary options required for the new microchip architectures. These options may improve the performance of your methods or functions. On the other side, these options increase the compilation time, in alpha coding processes you don’t need to set them always on. If you are a coder compiling after every few changes in most cases (as me) disable all optimizations would be faster to code. No optimization can be used in Debug settings which is also Default in a new Project. On the release, you can use the -O2 or -O3 option if it is possible. If you really don’t know which options are good for your arithmetic operations you can test the speed of your functions in different optimizations.
How can I use advanced compiler options to help optimize my programs?
There are also some advanced compiler Options for professionals developers. You can set assembler options, floating-point options, source code can be optimized, and strings. More details about the meaning and use of these optimization options can be found here in the official DocWiki of Embarcadero.
There are other compatibility options too.
Also, we should note that the compiler console command has more options that may be useful in some applications and tests. For example,
1 |
bcc32c.exe -tJ -O2 -ffast-math -Xdriver -mavx -c cpptest.cpp |
or for the 64bits,
1 |
bcc64.exe -tJ -O2 -ffast-math -c cpptest.cpp |
How can project build settings help with C++ program optimization?
RAD Studio 11 allows you to change Debug and Release options. Note that a debug version is also slower than a release version. Thus your final tests should be on the release version, and the deployment application must be a Release version.
You can also change your project from a Windows 32bits application to a Windows 64bits application. I highly recommend you to use 64bits. The difference between math on integers, fixed points, 32bits floats, and 64bits doubles is not as big as you might think. We highly use 64bits machines, thereby double-precision floating-point operations are not slower than single-precision floats. Our modern CPUs and GPUs are modernized for the 64bits and using double precision, which means there is no speed change between floats and doubles. Thus, in most cases, ray tracers run faster using all doubles than all floats on the same machine.
Can testing improve speed of your functions, methods and arithmetic operations?
The best way to understand which method or function is faster in which option is to test your code and measure the durations elapsed time in million times of operations in a single thread. Here is an example to check the difference between the no operation loop and loop with your function. This will exactly give you the elapsed time of your functions or methods.
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 |
//--------------------------------------------------------------------------- #include <vcl.h> #include <chrono> #pragma hdrstop #include "Optimization_Arithmetics_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; #define MAXIT 10000000000 //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { UInt64 i; double x = M_PI, y = M_PI, z; std::chrono::duration<double> diff; SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); //-------------------------------------------- auto start = std::chrono::steady_clock::now(); for( i = 0; i<MAXIT; i++ ) { // } auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff0 = end-start; Memo1->Lines->Add("Empty:"+ FloatToStr(diff0.count()) ); //-------------------------------------------- start = std::chrono::steady_clock::now(); for( i = 0; i<MAXIT; i++ ) { z += j*x; } end = std::chrono::steady_clock::now(); diff = end-start; Memo1->Lines->Add( L"Elapsed: "+ FloatToStr(diff.count() - diff0.count() ) ); Application->ProcessMessages(); } //--------------------------------------------------------------------------- |
Please check 5 Ways To Optimize C++ Arithmetic Operations posts to see how you can optimize your arithmetic operations in general.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version 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 for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.