C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Simple Ways To Optimize C++ Arithmetic Operations

What is optimization in arithmetic operations? How can I optimize my equations or functions? Which arithmetic operation is faster than another one? Where should I optimize?

Optimization is one of the important part of programming but we should note that it is not as important in C++ in most cases for modern CPUs and GPUs today. Do not spend too much time to optimize if it is not needed. In this post, we will explain where you should optimize and 5 ways to optimize C++ arithmetic operations.

What is Optimization ?

Mathematical optimization is really important in programming. Mathematical Optimization also called Mathematical Programming is the selection of the best mathematical elements or functions about some criterion, from a set of available alternatives. In programming, optimization problems of all sorts arise in all quantitative disciplines from computer science and engineering to operations research and economics, and the development of optimal solutions to problems has been of interest in mathematics for centuries. Sometimes it’s not enough to just get an answer – sometimes it’s desirable to get the best and most efficient answer.

Where and how can I optimize in programming ?

Today we have very high-speed computers and many new mathematical and logical instruction sets in our CPUs and GPUs. In general, if you have single operations, in this modern world of powerful compilers running on advanced modern hardware it’s not as critical as it used to be to spend excessive time optimizing your applications. Don’t forget that efficient coding takes time. Optimization may take at least twice your coding time. For example, most accounting applications don’t require optimizations when doing sum add calculating taxes and other expenses. Most of the operations are done by a single click in a single method or function which ends less than in milliseconds. These kinds of applications may not require much optimization, if any.

If you have a lot of datapoints – pixels, videos, data – to handle with heavy iterating, repeating computations, heavy loops or do-whiles(), recursive functions then you should focus on optimization in those areas instead. This will make your process faster and more robust for future extensions.

1. Simplify your equations

Try to simplify your equations. There are simplification methods in some of the mathematical applications. You can use equation editors like Matlab, Mathematica, Wolfram Alpha, Mapple etc. At least try to simplify your equations on paper.

Sometimes terms in math can be faster than you expected. for example division by 2 is faster than multiplication by 0.5. In many equations, these kinds of terms are canceled out. 

Note that the compiler cannot find these simplifications, but you can. Eliminating a few expensive operations inside an inner loop can speed your program more than days working on other parts.

2. Focus on Modern CPU and GPU Architectures

Optimization methods are also evolving because of many advances in CPU and GPU architectures. Optimization for CPU architectures requires a good assembler programming language. You should know well about how C++ transforms your codes to these machine codes. This will help you in understanding optimization in that architecture.

On modern CPUs and GPUs, floating-point operations have almost the same throughput as integer operations. In compute-intensive programs like ray tracing. This leads to a negligible difference between integer and floating-point costs. This means you should not go out of your way to use integer operations.

The difference between math on integers, fixed points, 32-bit floats, and 64-bit doubles is not as big as you might think. Double-precision floating-point operations may not be slower than single-precision floats, particularly on 64-bit machines. 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.

3. You should know your compiler options

Another thing is you should know well about optimization options -O0, -O1 -O2, and other necessary options required for the new microchip architectures. These options may improve the performance of your methods or functions. 

Also, note that a debug version is also noticeably slower than a release version. Thus your final tests should be on the release version.

4. Use operators wisely

Operators are the important part. You should think of operators with a focused CPU/GPU architecture. In general,

  • For basic data types, use the operators + , – , * , and / instead of the operators += , -= , *= , and /=
  • For the most classes, use the operators += , -= , *= , and /= , instead of the operators + , – , * , and /
  • If there is integer multiplication and division and there is a possibility to use bit shifting operations such as >> and <<, use bit shifting.

5. Test and improve your code speed

The best way to understand which method or function is faster is to test this code and measure the duration elapsed. You can use SetPriorityClass() method to set priority to REALTIME_PRIORITY_CLASS.

For example, this VCL example below (with a Memo and Button) can be used to test the speed of your methods.

These kind of tests can be done in FMX applications and in Console applications too.

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.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

C++Introduction to C++Language FeatureLearn C++

How to Create a New Windows FMX Static Library In C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish