-
Real Case of Premature Micro-Optimization
The benchmark is hosted at Github I discovered a real case of premature micro-optimization when you don’t measure. You know it is pretty bad when you read premature and micro on the same sentence. On the page 44 of Optimizing C++ ebook by Agner Fog, it is written … In some cases it is possible…
-
C++17: Benchmark of Singular Min/Max and Iterator Versions
The benchmark code is hosted at GitHub. In this tip, we’ll benchmark native operator with singular min()/max() and iterator versions like min_element()/max_element(). Lastly, we benchmark the combined operations with minmax_element(). This is the benchmark code. const size_t MAX_LOOP = (argc == 2) ? atoi(argv[1]) : 1000; using int_type = uint64_t; std::vector vec(1000000); std::iota(vec.begin(), vec.end(), 1);…
-
C#: Never Test For NaN With Equality Test
Let's write a simple program to test. To get a NaN, we do a 0/0 and store it in num. First of all, the divisor has to be a float-point type because DivideByZeroException shall be thrown for integer or decimal zero divisor. Note: Non-zero number divided by zero (float) gives an infinity number. In our…
-
No Code is the Fastest Code
Optimization of finding a point with shortest distance w.r.t. a point of interest The benchmark source code is hosted at Github. Table of Contents Introduction Shortest Distance Benchmark Results History Introduction When it comes to optimization, majority of developers turn to parallelism but per CPU core isn’t cheap. We could look to eliminate some operations…
-
C++14/20 Heterogeneous Lookup Benchmark
Download the benchmark code on GitHub. C++14 introduced ordered transparency lookup which enables const char* and string_view lookup without string instantiation on map/set objects. C++20 introduced unordered transparency lookup that allows to do same thing with unordered_map/unordered_set. We are officially in 2020 but as of writing time, the C++20 Standard is yet to be ratified…
-
C++: Size Matters in Platform Compatibility
Introduction For file storage and data communication to work interoperably, the width of datatype must stay invariant across platforms. This tip discusses the pitfalls of platform-dependent data width and their solution. Endianess, deserving a tip of its own, is not covered here. time_t time_t stores the number of seconds since 1st January 1970. It is…
-
C++23: fullptr to replace nullptr
Coming in C++ 2023, detailed in the AF0401 proposal, fullptr_t is fully recommended as the invalid pointer type to replace current nullptr_t which first standardized in C++11. nullptr is defined as a pointer value with all its bits set to zeroes while fullptr has its bits set to ones (the address of the last addressable…
-
C++11 std::div() Benchmark
Download source at Github Update: rand() overhead in benchmark has been removed by filling the array with random values beforehand. C++11 standard introduces std::div() and its siblings on the premise of some compiler can take advantage of the available machine code that compute quotient and remainder of division together. The C++ reference noted, and (updated)…
-
Not Every Memory Allocation Failure is OOM
Introduction As with many C++ programmers with C background, bring their C habits to C++ programming as shown in the below code where a massive array is allocated and pointer is then checked for failed allocation in presence of null address. It works this way for C malloc. Unfortunately, C++ new does not work like…
-
Floating Point and Integer Arithmetic Benchmark
Introduction This is not much of a tip, just a posting of benchmark result to compare integer and floating point arithmetic timing. All the integer and floating point types used in Benchmark are 64bit. Timing is based on looping 100 million times. Clarification: SmallInt and SmallDouble refers to small values (10-10000) stored in int64_t and…