ccache is a utility that caches intermediate files during compilation offering significant speedups. It is especially useful if you are switching between two branches of the same project see below.

To use it, prepend it before the compiler i.e. Use ccache gcc instead of gcc ... . If you are using cmake with a version greater that 3.4 you can use the option -DCMAKE_CXX_COMPILER_LAUNCHER=ccache for older version you can modify the compiler settings or use the variable RULE_LAUNCH_COMPILE for more see here.

Example :

Lets try it on the source code of cmake:

First without ccache

$ git clone https://gitlab.kitware.com/cmake/cmake
$ mkdir build
$ cd build
$ cmake ../cmake
$ time make -j
real	7m29.921s
user	25m56.556s
sys	1m46.428s

We switch to another branch and try again:

$ cd ../cmake
$ git checkout v3.15.0
$ cd ../build
$ cmake ../cmake
$ time make -j
real	5m47.679s
user	20m17.790s
sys	1m23.828s

These are our baselines.

Switching back to master and recompiling requires the same time:

$ cd ../cmake
$ git checkout master
$ cd ../build
$ cmake ../cmake
$ time make -j
real	7m29.399s
user	25m56.187s
sys	1m46.214s

Now we enable ccache on a clean build

$ rm -rf ./build/*
$ cmake -DCMAKE_CXX_COMPILER_LAUNCHER=ccache ../cmake
$ time make -j
real	8m32.952s
user	27m36.442s
sys	2m12.991s

There is a slight increase in compilation time. The same when we switch for first time to another branch:

$ cd ../cmake
$ git checkout v3.15.0
$ cd ../build
$ cmake ../cmake
$ time make -j
real	6m42.900s
user	21m48.924s
sys	1m44.644s

However, now that we have populated our cache, builds are significantly faster !

$ cd ../cmake
$ git checkout master
$ cd ../build
$ cmake ../cmake
$ time make -j
real	0m38.711s
user	1m55.778s
sys	0m21.922s

and

$ cd ../cmake
$ git checkout v3.15.0
$ cd ../build
$ cmake ../cmake
$ time make -j
real	0m31.499s
user	1m36.373s
sys	0m18.004s

ccache also offers some statistics:

$ ccache --stats
cache directory                     /home/user/.cache/ccache
primary config                      /home/user/.config/ccache/ccache.conf
secondary config (readonly)         /etc/ccache.conf
stats updated                       Tue Jun 23 17:34:08 2020
cache hit (direct)                   874
cache hit (preprocessed)               9
cache miss                           882
cache hit rate                     50.03 %
cleanups performed                     0
files in cache                      1754
cache size                          46.5 MB
max cache size                       5.0 GB

Cache size, policies and other configurations are performed by editing ~/.config/ccache/ccache.conf according to the documentation.