C++程序如何做优化-创新互联
这期内容当中小编将会给大家带来有关C++ 程序如何做优化,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
第一步,找出耗时的点。原来的程序输出日志用的cout,没有附带时间,不能通过日志发现耗时的点。为了找出性能关键点,第一步是改进log,在输出中加上时间。写了一个Log类,替换掉cout,程序的输出中就带上时间了:
#include "../include/Log.hpp" #include #include #include #include using namespace std; namespace tlanyan { string Log::datetimeFormat = "%F %T"; Log::Log() { } void Log::info(const char* message) { cout << getCurrentTime() << " [info] " << message << endl; } void Log::debug(const char* message) { #if DEBUG cout << getCurrentTime() << " [debug] " << message; #endif; } const char* Log::getCurrentTime() { //locale::global(locale("zh_CN.utf8")); time_t t = time(NULL); char mbstr[512]; if (strftime(mbstr, sizeof(mbstr), Log::datetimeFormat.c_str(), localtime(&t))) { return mbstr; } cerr << "获取或格式化时间错误!" << endl; exit(1); } Log::~Log() { } } // 调用示例: Log::info("program begins...");
网站标题:C++程序如何做优化-创新互联
标题URL:http://myzitong.com/article/ceephj.html