Benchmarks

The benchmarks are done with this script using CMake. There are 3 benchmarking scenarios:

Compilers used:

  • WINDOWS: Microsoft Visual Studio Community 2017 - Version 15.1.26403.7
  • WINDOWS: gcc 7.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
  • LINUX: gcc 6.3.0 20170406 (Ubuntu 6.3.0-12ubuntu2)
  • LINUX: clang 4.0.0-1 (tags/RELEASE_400/rc1) Target: x86_64-pc-linux-gnu

Environment used (Intel i7 3770k, 16g RAM):

  • Windows 7 - on an SSD
  • Ubuntu 17.04 in a VirtualBox VM - on a HDD

doctest version: 1.2.0 (released on 2017.05.16)

Catch version: 1.9.3 (released on 2017.04.25)

Compile time benchmarks

Cost of including the header

This is a benchmark that is relevant only to single header and header only frameworks - like doctest and Catch.

The script generates 201 source files and in 200 of them makes a function in the form of int f135() { return 135; } and in main.cpp it forward declares all the 200 such dummy functions and accumulates their result to return from the main() function. This is done to ensure that all source files are built and that the linker doesn't remove/optimize anything.

  • baseline - how much time the source files need for a single threaded build with msbuild/make
  • + implement - only in main.cpp the header is included with a #define before it so the test runner gets implemented:
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"
  • + header everywhere - the framework header is also included in all the other source files
  • + disabled - doctest specific - only this framework can remove everything related to it from the binary
doctestbaseline+ implement+ header everywhere+ disabled
MSVC Debug7.178.5012.509.42
MSVC Release6.728.4712.518.97
MinGW GCC Debug10.6513.5618.5913.34
MinGW GCC Release10.8114.5319.2114.11
Linux GCC Debug4.986.5511.226.94
Linux GCC Release4.497.2711.927.87
Linux Clang Debug8.529.4215.2511.14
Linux Clang Release9.0111.8518.1811.75
Catchbaseline+ implement+ header everywhere
MSVC Debug7.1710.60128.02
MSVC Release6.7210.90119.25
MinGW GCC Debug10.6526.12127.00
MinGW GCC Release10.8119.95114.15
Linux GCC Debug4.989.37105.66
Linux GCC Release4.4913.04105.57
Linux Clang Debug8.5211.3370.57
Linux Clang Release9.0116.5975.85

Conclusion

doctest

  • instantiating the test runner in one source file costs ~1.5-3 seconds implement - baseline
  • the inclusion of doctest.h in one source file costs between 20ms - 30ms (header_everywhere - implement) / 200
  • including the library everywhere but everything disabled costs less than 3 seconds disabled - baseline for 200 files

Catch

  • instantiating the test runner in one source file costs ~4-8 seconds implement - baseline
  • the inclusion of catch.hpp in one source file costs between 300ms - 575ms (header_everywhere - implement) / 200

So if doctest.h costs 20ms and catch.hpp costs 560ms on MSVC - then the doctest header is >> 28 << times lighter (for MSVC)!


The results are in seconds and are in no way intended to bash Catch - the doctest framework wouldn't exist without it.

The reason the doctest header is so light on compile times is because it forward declares everything and doesn't drag any headers in the source files (except for the source file where the test runner gets implemented). This was a key design decision.

Cost of an assertion macro

The script generates 11 .cpp files and in 10 of them makes 50 test cases with 100 asserts in them (of the form CHECK(a==b) where a and b are always the same int variables) - 50k asserts! The testing framework gets implemented in main.cpp.

  • baseline - how much time a single threaded build takes with the header included everywhere - no test cases or asserts!
  • CHECK(a==b) - will add CHECK() asserts which decompose the expression with template machinery

doctest specific:

  • CHECK_EQ(a,b) - will use CHECK_EQ(a,b) instead of the expression decomposing ones
  • FAST_CHECK_EQ(a,b) - will use FAST_CHECK_EQ(a,b) instead of the expression decomposing ones
  • +faster - will add DOCTEST_CONFIG_SUPER_FAST_ASSERTS which speeds up FAST_CHECK_EQ(a,b) even more
  • +disabled - all test case and assert macros will be disabled with DOCTEST_CONFIG_DISABLE

Catch specific:

doctestbaselineCHECK(a==b)CHECK_EQ(a,b)FAST_CHECK_EQ(a,b)+faster+disabled
MSVC Debug3.2224.1718.548.325.752.34
MSVC Release3.6341.1023.2010.896.872.31
MinGW GCC Debug4.0991.9860.1925.5112.611.82
MinGW GCC Release4.74240.58156.7950.1619.722.53
Linux GCC Debug2.0681.3252.1418.0710.151.16
Linux GCC Release3.28207.21126.8933.1719.922.03
Linux Clang Debug1.7579.4151.2017.787.651.20
Linux Clang Release3.73140.7982.6121.1912.641.46

And here is Catch which only has normal CHECK(a==b) asserts:

CatchbaselineCHECK(a==b)+faster
MSVC Debug9.9440.1436.66
MSVC Release10.66231.6081.90
MinGW GCC Debug21.20129.26110.95
MinGW GCC Release14.59297.04207.75
Linux GCC Debug10.05115.5398.84
Linux GCC Release13.29294.26218.37
Linux Clang Debug6.38103.0685.02
Linux Clang Release11.15195.62156.04

Conclusion

doctest:

  • is around 30% faster than Catch when using normal expression decomposing CHECK(a==b) asserts
  • asserts of the form CHECK_EQ(a,b) with no expression decomposition - around 25%-45% faster than CHECK(a==b)
  • fast asserts like FAST_CHECK_EQ(a,b) with no try/catch blocks - around 60-80% faster than CHECK_EQ(a,b)
  • the DOCTEST_CONFIG_SUPER_FAST_ASSERTS identifier which makes the fast assertions even faster by another 50-80%
  • using the DOCTEST_CONFIG_DISABLE identifier the assertions just disappear as if they were never written

Catch:

Runtime benchmarks

The runtime benchmarks consist of a single test case with a loop of 10 million iterations performing the task - a single normal assert (using expression decomposition) or the assert + the logging of the loop iterator i:

for(int i = 0; i < 10000000; ++i)
    CHECK(i == i);

or

for(int i = 0; i < 10000000; ++i) {
    INFO(i);
    CHECK(i == i);
}

Note that the assert always passes - the goal should be to optimize for the common case - lots of passing test cases and a few that maybe fail.

doctestassert+ info
MSVC Debug5.2914.32
MSVC Release0.771.62
MinGW GCC Debug2.254.71
MinGW GCC Release0.390.90
Linux GCC Debug2.845.13
Linux GCC Release0.300.69
Linux Clang Debug2.475.02
Linux Clang Release0.410.75
Catchassert+ info
MSVC Debug365.37621.78
MSVC Release7.0418.64
MinGW GCC Debug9.2221.89
MinGW GCC Release7.2913.95
Linux GCC Debug11.1724.79
Linux GCC Release6.4512.68
Linux Clang Debug10.4022.64
Linux Clang Release5.8113.83

Conclusion

doctest is significantly faster - between 4 and 40 times.

In these particular cases doctest makes 0 allocations when the assert doesn't fail - it uses lazy stringification (meaning it stringifies the expression or the logged loop counter only if it has to) and a small-buffer optimized string class to achieve these results.


If you want a benchmark that is not synthetic - check out this blog post of Baptiste Wicht who tested the compile times of the asserts in the 1.1 release with his Expression Templates Library!

While reading the post - keep in mind that if a part of a process takes 50% of the time and is made 10000 times faster - the overall process would still be only roughly 50% faster.


Home