|
|
|
@ -1,108 +1,107 @@
|
|
|
|
|
#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_
|
|
|
|
|
#define MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_
|
|
|
|
|
|
|
|
|
|
// 仅仅针对 sort, binary_search 做了性能测试
|
|
|
|
|
#ifndef MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ // 预处理指令,防止头文件被重复包含
|
|
|
|
|
#define MYTINYSTL_ALGORITHM_PERFORMANCE_TEST_H_ // 定义宏,用于防止头文件被重复包含
|
|
|
|
|
|
|
|
|
|
// 包含标准库中的算法头文件
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
// 包含自定义的算法头文件
|
|
|
|
|
#include "../MyTinySTL/algorithm.h"
|
|
|
|
|
// 包含测试相关的头文件
|
|
|
|
|
#include "test.h"
|
|
|
|
|
|
|
|
|
|
namespace mystl
|
|
|
|
|
{
|
|
|
|
|
namespace test
|
|
|
|
|
namespace mystl // 命名空间mystl
|
|
|
|
|
{
|
|
|
|
|
namespace algorithm_performance_test
|
|
|
|
|
namespace test // 命名空间test
|
|
|
|
|
{
|
|
|
|
|
namespace algorithm_performance_test // 命名空间algorithm_performance_test,用于算法性能测试
|
|
|
|
|
|
|
|
|
|
// 函数性能测试宏定义
|
|
|
|
|
{
|
|
|
|
|
// 函数性能测试宏定义,用于测试单个函数的性能
|
|
|
|
|
#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<int>(static_cast<double>(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)
|
|
|
|
|
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<int>(static_cast<double>(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; // 输出结果,WIDE为预定义的宽度 \
|
|
|
|
|
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<int>(static_cast<double>(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)
|
|
|
|
|
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<int>(static_cast<double>(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; // 输出结果,WIDE为预定义的宽度 \
|
|
|
|
|
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_
|
|
|
|
|
|
|
|
|
|