From 1ef64c15a44ef3042e521f864639c395ffe980fb Mon Sep 17 00:00:00 2001 From: buaiqiufengaisese <1911007196@qq.com> Date: Mon, 16 Dec 2024 21:16:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=99=E6=AE=B5=E4=BB=A3=E7=A0=81=E6=98=AF?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E7=94=A8=E4=BA=8E=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=9A=84=E5=A4=B4=E6=96=87=E4=BB=B6=EF=BC=8C=E5=AE=83?= =?UTF-8?q?=E5=8C=85=E5=90=AB=E4=BA=86=E4=B8=A4=E4=B8=AA=E5=AE=8F=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20`FUN=5FTEST1`=20=E5=92=8C=20`FUN=5FTEST2`=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=B5=8B=E9=87=8F=E5=92=8C=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4=EF=BC=8C?= =?UTF-8?q?=E5=85=B6=E4=B8=AD=20`FUN=5FTEST1`=20=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=97=A0=E9=A2=9D=E5=A4=96=E5=8F=82=E6=95=B0=E7=9A=84=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=20`FUN=5FTEST2`=20=E7=94=A8=E4=BA=8E=E9=9C=80=E8=A6=81?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E9=A2=9D=E5=A4=96=E5=8F=82=E6=95=B0=E7=9A=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=B5=8B=E8=AF=95=E3=80=82=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=B8=AD=E5=AE=9A=E4=B9=89=E4=BA=86=E4=B8=A4=E4=B8=AA=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=87=BD=E6=95=B0=20`binary=5Fsearch=5Ftest`=20?= =?UTF-8?q?=E5=92=8C=20`sort=5Ftest`=EF=BC=8C=E5=88=86=E5=88=AB=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=AF=94=E8=BE=83=E6=A0=87=E5=87=86=E5=BA=93=E5=92=8C?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=BA=93=E4=B8=AD=E4=BA=8C=E5=88=86?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E5=92=8C=E6=8E=92=E5=BA=8F=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E7=9A=84=E6=80=A7=E8=83=BD=E3=80=82=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=9A=84=E5=85=A5=E5=8F=A3=E7=82=B9=E6=98=AF=20`algor?= =?UTF-8?q?ithm=5Fperformance=5Ftest`=20=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E5=AE=83=E5=9C=A8=20`PERFORMANCE=5FTEST=5FON`=20=E5=AE=8F?= =?UTF-8?q?=E8=A2=AB=E5=AE=9A=E4=B9=89=E6=97=B6=E6=89=A7=E8=A1=8C=E3=80=82?= =?UTF-8?q?=E6=95=B4=E4=B8=AA=E6=96=87=E4=BB=B6=E9=80=9A=E8=BF=87=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=20`mystl::test::algorithm=5Fperform?= =?UTF-8?q?ance=5Ftest`=20=E7=BB=84=E7=BB=87=EF=BC=8C=E4=BB=A5=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=91=BD=E5=90=8D=E5=86=B2=E7=AA=81=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=E4=BB=A3=E7=A0=81=E7=9A=84=E6=95=B4=E6=B4=81?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Test/algorithm_performance_test.h | 183 +++++++++--------- 1 file changed, 90 insertions(+), 93 deletions(-) diff --git a/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h b/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h index 675e5b2..d7df544 100755 --- a/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h +++ b/src/MyTinySTL-master/MyTinySTL-master/Test/algorithm_performance_test.h @@ -1,108 +1,105 @@ -#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ +#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ // 防止头文件被重复包含 #define MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ -// 仅仅针对 sort, binary_search 做了性能测试 +// 仅对 sort 和 binary_search 函数进行性能测试 -#include +#include // 包含标准算法库 -#include "../MyTinySTL/algorithm.h" -#include "test.h" +#include "../MyTinySTL/algorithm.h" // 包含自定义的算法库 +#include "test.h" // 包含测试相关的头文件 -namespace mystl -{ -namespace test -{ -namespace algorithm_performance_test +namespace mystl // 命名空间 mystl { + namespace test // 命名空间 test + { + namespace algorithm_performance_test // 命名空间 algorithm_performance_test -// 函数性能测试宏定义 + { + // 函数性能测试宏定义,用于测试函数 fun 并打印执行时间 #define FUN_TEST1(mode, fun, count) do { \ - std::string fun_name = #fun; \ - srand((int)time(0)); \ - char buf[10]; \ - clock_t start, end; \ - int *arr = new int[count]; \ - for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); \ - start = clock(); \ - mode::fun(arr, arr + count); \ - end = clock(); \ - int n = static_cast(static_cast(end - start) \ - / CLOCKS_PER_SEC * 1000); \ - std::snprintf(buf, sizeof(buf), "%d", n); \ - std::string t = buf; \ - t += "ms |"; \ - std::cout << std::setw(WIDE) << t; \ - delete []arr; \ + std::string fun_name = #fun; // 将函数名转为字符串 \ + srand((int)time(0)); // 初始化随机数种子 \ + char buf[10]; // 用于存储时间字符串 \ + clock_t start, end; // 定义开始和结束时间 \ + int *arr = new int[count]; // 分配数组空间 \ + for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); // 填充随机数 \ + start = clock(); // 记录开始时间 \ + mode::fun(arr, arr + count); // 调用函数 fun \ + end = clock(); // 记录结束时间 \ + int n = static_cast(static_cast(end - start) // 计算执行时间 \ + / CLOCKS_PER_SEC * 1000); // 转换为毫秒 \ + std::snprintf(buf, sizeof(buf), "%d", n); // 格式化时间字符串 \ + std::string t = buf; // 将时间字符串赋值给 t \ + t += "ms |"; // 添加单位后缀 \ + std::cout << std::setw(WIDE) << t; // 打印时间 \ + delete []arr; // 释放数组空间 \ } while(0) -#define FUN_TEST2(mode, fun, count) do { \ - std::string fun_name = #fun; \ - srand((int)time(0)); \ - char buf[10]; \ - clock_t start, end; \ - int *arr = new int[count]; \ - for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); \ - start = clock(); \ - for(size_t i = 0; i < count; ++i) \ - mode::fun(arr, arr + count, rand()); \ - end = clock(); \ - int n = static_cast(static_cast(end - start) \ - / CLOCKS_PER_SEC * 1000); \ - std::snprintf(buf, sizeof(buf), "%d", n); \ - std::string t = buf; \ - t += "ms |"; \ - std::cout << std::setw(WIDE) << t; \ - delete []arr; \ -} while(0) +#define FUN_TEST2(mode, fun, count) do { // 与 FUN_TEST1 类似,但适用于需要额外参数的函数 + std::string fun_name = #fun; // 将函数名转为字符串 \ + srand((int)time(0)); // 初始化随机数种子 \ + char buf[10]; // 用于存储时间字符串 \ + clock_t start, end; // 定义开始和结束时间 \ + int *arr = new int[count]; // 分配数组空间 \ + for(size_t i = 0; i < count; ++i) *(arr + i) = rand(); // 填充随机数 \ + start = clock(); // 记录开始时间 \ + for(size_t i = 0; i < count; ++i) \ + mode::fun(arr, arr + count, rand()); // 调用函数 fun,传入额外参数 \ + end = clock(); // 记录结束时间 \ + int n = static_cast(static_cast(end - start) // 计算执行时间 \ + / CLOCKS_PER_SEC * 1000); // 转换为毫秒 \ + std::snprintf(buf, sizeof(buf), "%d", n); // 格式化时间字符串 \ + std::string t = buf; // 将时间字符串赋值给 t \ + t += "ms |"; // 添加单位后缀 \ + std::cout << std::setw(WIDE) << t; // 打印时间 \ + delete []arr; // 释放数组空间 \ + } while(0) -void binary_search_test() -{ - std::cout << "[------------------- function : binary_search ------------------]" << std::endl; - std::cout << "| orders of magnitude |"; - TEST_LEN(LEN1, LEN2, LEN3, WIDE); - std::cout << "| std |"; - FUN_TEST2(std, binary_search, LEN1); - FUN_TEST2(std, binary_search, LEN2); - FUN_TEST2(std, binary_search, LEN3); - std::cout << std::endl << "| mystl |"; - FUN_TEST2(mystl, binary_search, LEN1); - FUN_TEST2(mystl, binary_search, LEN2); - FUN_TEST2(mystl, binary_search, LEN3); - std::cout << std::endl; -} + void binary_search_test() // 二分查找性能测试函数 + { + std::cout << "[------------------- function : binary_search ------------------]" << std::endl; + std::cout << "| orders of magnitude |"; + TEST_LEN(LEN1, LEN2, LEN3, WIDE); // 打印测试长度 + std::cout << "| std |"; + FUN_TEST2(std, binary_search, LEN1); // 测试标准库二分查找 + FUN_TEST2(std, binary_search, LEN2); + FUN_TEST2(std, binary_search, LEN3); + std::cout << std::endl << "| mystl |"; + FUN_TEST2(mystl, binary_search, LEN1); // 测试自定义库二分查找 + FUN_TEST2(mystl, binary_search, LEN2); + FUN_TEST2(mystl, binary_search, LEN3); + std::cout << std::endl; + } -void sort_test() -{ - std::cout << "[----------------------- function : sort -----------------------]" << std::endl; - std::cout << "| orders of magnitude |"; - TEST_LEN(LEN1, LEN2, LEN3, WIDE); - std::cout << "| std |"; - FUN_TEST1(std, sort, LEN1); - FUN_TEST1(std, sort, LEN2); - FUN_TEST1(std, sort, LEN3); - std::cout << std::endl << "| mystl |"; - FUN_TEST1(mystl, sort, LEN1); - FUN_TEST1(mystl, sort, LEN2); - FUN_TEST1(mystl, sort, LEN3); - std::cout << std::endl; -} + void sort_test() // 排序性能测试函数 + { + std::cout << "[----------------------- function : sort -----------------------]" << std::endl; + std::cout << "| orders of magnitude |"; + TEST_LEN(LEN1, LEN2, LEN3, WIDE); // 打印测试长度 + std::cout << "| std |"; + FUN_TEST1(std, sort, LEN1); // 测试标准库排序 + FUN_TEST1(std, sort, LEN2); + FUN_TEST1(std, sort, LEN3); + std::cout << std::endl << "| mystl |"; + FUN_TEST1(mystl, sort, LEN1); // 测试自定义库排序 + FUN_TEST1(mystl, sort, LEN2); + FUN_TEST1(mystl, sort, LEN3); + std::cout << std::endl; + } -void algorithm_performance_test() -{ - -#if PERFORMANCE_TEST_ON - std::cout << "[===============================================================]" << std::endl; - std::cout << "[--------------- Run algorithm performance test ----------------]" << std::endl; - sort_test(); - binary_search_test(); - std::cout << "[--------------- End algorithm performance test ----------------]" << std::endl; - std::cout << "[===============================================================]" << std::endl; + void algorithm_performance_test() // 算法性能测试入口函数 + { +#if PERFORMANCE_TEST_ON // 如果定义了性能测试宏,则执行测试 + std::cout << "[===============================================================]" << std::endl; + std::cout << "[--------------- Run algorithm performance test ----------------]" << std::endl; + sort_test(); // 运行排序性能测试 + binary_search_test(); // 运行二分查找性能测试 + std::cout << "[--------------- End algorithm performance test ----------------]" << std::endl; + std::cout << "[===============================================================]" << std::endl; #endif // PERFORMANCE_TEST_ON + } -} - -} // namespace algorithm_performance_test -} // namespace test -} // namespace mystl -#endif // !MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ - + } // namespace algorithm_performance_test + } // namespace test + } // namespace mystl +#endif // !MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ \ No newline at end of file