|
|
|
@ -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 <algorithm>
|
|
|
|
|
#include <algorithm> // 包含标准算法库
|
|
|
|
|
|
|
|
|
|
#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<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; \
|
|
|
|
|
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<int>(static_cast<double>(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<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)
|
|
|
|
|
#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<int>(static_cast<double>(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_
|