Ben Chuanlong Du's Blog

And let it direct your passion with reason.

Performance Tips for C++

Performance

  1. If there is some block of useless code, the compile is smart enough to ignore it and thus speed up the program.

  2. Use the option -O2 to generate optimized code when you are ready to publish your code.

  3. Define variables only when needed to avoid the overhead of creating …

Lambda Function in C++11

Lambda Function

Check [here[(https://github.com/dclong/cearn/tree/master/lambda) for illustrative examples for the following discussions.

  1. When capture variables, you can define new variables in the [] of a lambda expression. For example, if a lambda function need the sum of two double variable x and y, you …

Initializing Variables in C++

  1. {} is more powerful than () and thus is preferred over (). You should use always use {} except for a few cases where () is necessary. For example, if you want to create a vector of length 1000, you have to use
    vector<int>(1000);
    

instead of

    vector<int> {1000};

which create a vector …

Install the GSL Library

GSL is an advance C/C++ library that is widely used. To install GSL, you can download the source file and following instruction in the included README and INSTALL document. For Unix/Linux users, the GSL library is often availabe in the repository. For example, you can use the following …

Lvalue Reference and Rvalue Reference

Difference betwen Lvalue and Rvalue Reference

  1. Lvalue and Rvalue are terrible names. They are due to historical reasonal but people stuck with these names.

  2. A lvalue reference has a name while a rvalue reference has no name, in other words, a lvalue reference is persistent while a rvalue reference is …

Copy Arrays in Java

There are several different ways to copy arrays of object (primitive types) in Java. However, the fastest way seems to be System.arraycopy. This methed is implemented using native code and only performs a shallow copy. Acutally most methods for copying arrays in Java perform shallow copy.

int arr1[] = {0 …