Copy-On-Write Base Class Template

This tip presents a really simple Copy-On-Write(COW) class template (to be used as a base class) to ease developer effort to implement COW class. Copy-On-Write as its name implies, is a technique to duplicate(copy) shared resource upon write: the only exception to this write operation is when there is only one existing copy and notContinue reading “Copy-On-Write Base Class Template”

Optimization Turns Out to Be Pessimization!

Benchmark is hosted at Github I have doing this optimization in my hobby OpenGL projects for years. My only sin is I did not measure their performance. I just assumed the one with less operation must be the faster one. Whenever a programmer wants to do get a fractional value of a maximum integer value,Continue reading “Optimization Turns Out to Be Pessimization!”

C++17: codecvt_utf8 is deprecated

Last week, I got this compilation error after upgrading my Visual C++ project to C++17 for using codecvt_utf8 to convert UTF-8 string back and fro wchar_t string. error C4996: ‘std::codecvt_utf8<wchar_t,1114111,0>’: warning STL4017: std::wbuffer_convert, std::wstring_convert, and the <codecvt> header (containing std::codecvt_mode, std::codecvt_utf8, std::codecvt_utf16, and std::codecvt_utf8_utf16) are deprecated in C++17. (The std::codecvt class template is NOT deprecated.)Continue reading “C++17: codecvt_utf8 is deprecated”

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 possibleContinue reading “Real Case of Premature Micro-Optimization”

Intel Should Implement Conditional SIMD

Auto-vectorizer can be a great help in vectorizing simple instructions with SIMD instruction calls. int a[COUNT]; int b[COUNT]; int c[COUNT]; for(int i=0; i<COUNT; ++i) { a[i] = b[i] + c[i]; } But a simple conditional check is enough to break the auto-vectorizer. for(int i=0; i<COUNT; ++i) { // a conditional check breaks the auto-vectorizer if(b[i]Continue reading “Intel Should Implement Conditional SIMD”

C++: Quick and Dirty Log Extraction and Replay

New Text Stream library is hosted at Github. Log Extraction Hackers can stay infiltrated and undetected up to 200 days! Nowadays, hackers do not even bother to cover their tracks because most logs are not checked for unusual activities. This tip-trick is to help to change all that with the New Text Stream library. Unfortunately,Continue reading “C++: Quick and Dirty Log Extraction and Replay”

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);Continue reading “C++17: Benchmark of Singular Min/Max and Iterator Versions”

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 ourContinue reading “C#: Never Test For NaN With Equality Test”

Shh…Your interviewer didn’t have the answers to his technical questions!

Advice for suitable type of technical question to ask in an interview   Write code to sum up from 1 to 100 One has to especially careful when the question is deceptively simple, for instance, sum up all the numbers from 1 to 100. A simple summing loop wouldn’t suffice to satisfy your interviewer. HeContinue reading “Shh…Your interviewer didn’t have the answers to his technical questions!”

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 operationsContinue reading “No Code is the Fastest Code”