0%

Cpp-三方库

阅读更多

1 gtest

github-googletest

安装googletest

1
2
3
4
5
6
7
8
9
git clone https://github.com/google/googletest.git --depth 1
cd googletest

mkdir build
cd build

cmake ..
make
make install

cmake中添加gtest依赖

1
2
3
4
5
6
7
find_package(GTest REQUIRED)
message(STATUS "GTEST_INCLUDE_DIRS: ${GTEST_INCLUDE_DIRS}")
message(STATUS "GTEST_BOTH_LIBRARIES: ${GTEST_BOTH_LIBRARIES}")
message(STATUS "GTEST_LIBRARIES: ${GTEST_LIBRARIES}")
message(STATUS "GTEST_MAIN_LIBRARIES: ${GTEST_MAIN_LIBRARIES}")

target_link_libraries(xxx ${GTEST_LIBRARIES})

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 编写CMakeLists.txt 
cat > CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.20)

project(gtest_demo)

set(CMAKE_CXX_STANDARD 17)

set(EXEC_FILES ./test_main.cpp)

add_executable(gtest_demo ${EXEC_FILES})

find_package(GTest REQUIRED)
message(STATUS "GTEST_INCLUDE_DIRS: ${GTEST_INCLUDE_DIRS}")
message(STATUS "GTEST_BOTH_LIBRARIES: ${GTEST_BOTH_LIBRARIES}")
message(STATUS "GTEST_LIBRARIES: ${GTEST_LIBRARIES}")
message(STATUS "GTEST_MAIN_LIBRARIES: ${GTEST_MAIN_LIBRARIES}")

target_link_libraries(gtest_demo ${GTEST_LIBRARIES})
EOF

# 编写test_main.cpp
cat > test_main.cpp << 'EOF'
#include <gtest/gtest.h>

TEST(TestDemo, case_right) {
ASSERT_EQ(1, 1);
}

TEST(TestDemo, case_wrong) {
ASSERT_EQ(1, 0);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
EOF

mkdir build
cd build

cmake ..
make

./gtest_demo

1.1 Tips

  1. 假设编译得到的二进制是test,通过执行./test --help就可以看到所有gtest支持的参数,包括执行特定case等等

2 phmap

全称:parallel-hashmap,提供了一组高性能、并发安全的map,用于替换std以及boost中的map

3 benchmark

google-benchmark

安装benchmark

1
2
3
4
5
6
7
8
9
10
git clone https://github.com/google/benchmark.git --depth 1
cd benchmark

mkdir build
cd build

# 这里指定googletest的工程路径(不加任何参数会有提示)
cmake -DGOOGLETEST_PATH=~/googletest/ -DCMAKE_BUILD_TYPE=Release ..
make
make install

cmake中添加benchmark依赖

1
2
3
find_package(benchmark REQUIRED)

target_link_libraries(xxx benchmark::benchmark)

完整示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 编写CMakeLists.txt 
cat > CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.20)

project(benchmark_demo)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall -fopt-info-vec")

set(EXEC_FILES ./main.cpp)

add_executable(benchmark_demo ${EXEC_FILES})

find_package(benchmark REQUIRED)

target_link_libraries(benchmark_demo benchmark::benchmark)
EOF

# 编写main.cpp
cat > main.cpp << 'EOF'
#include <string>
#include <benchmark/benchmark.h>

static void BM_StringCreation(benchmark::State& state) {
for (auto _ : state)
std::string empty_string;
}
// Register the function as a benchmark
BENCHMARK(BM_StringCreation);

// Define another benchmark
static void BM_StringCopy(benchmark::State& state) {
std::string x = "hello";
for (auto _ : state)
std::string copy(x);
}
BENCHMARK(BM_StringCopy);

BENCHMARK_MAIN();
EOF

mkdir build
cd build

cmake ..
make

./benchmark_demo

输出如下:

  • Time:每次迭代消耗的总时间,包括cpu时间+等待时间
  • CPU:每次迭代真正占用cpu的时间
  • Iterations:迭代次数
1
2
3
4
5
------------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------------
BM_StringCreation 5.12 ns 5.12 ns 136772962
BM_StringCopy 21.0 ns 21.0 ns 33441350

3.1 quick-benchmark

quick-bench(在线)

3.2 Tips

3.2.1 benchmark::DoNotOptimize

避免优化本不应该优化的代码

3.3 参考