第四章,效率
23-考虑使用其他程序库
在检索程序性能瓶颈时,要把引用的程序库这一因素考虑进来,可以思考是否可以替换程序库或修改程序库来提升性能
举例,在以前,iostream库明显慢于stdio,即使iostream有类型安全特性,并且可以扩充(采用面向对象设计,允许用户自定义类型无缝接入流操作);如今可以使用下面的语句设置来得到一个高效的iostream
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
|
引申: iostream扩充
1、运算符重载(最常用)
2、自定义流缓冲区: 通过继承std::streambuf实现自定义底层I/O
#include <streambuf> #include <ostream>
class LogStreamBuf : public std::streambuf { protected: std::streambuf* dest; bool at_line_start = true; virtual int overflow(int c) override { if (at_line_start && c != '\n') { const char* prefix = "[2026-03-19 14:00] "; dest->sputn(prefix, strlen(prefix)); } at_line_start = (c == '\n'); return dest->sputc(c); } public: LogStreamBuf(std::streambuf* d) : dest(d) {} };
class LogStream : public std::ostream { LogStreamBuf buf; public: LogStream(std::ostream& dest) : std::ostream(&buf), buf(dest.rdbuf()) {} };
LogStream log(std::cout); log << "Hello World" << std::endl;
|
3、自定义locale/facet
#include <locale> #include <iomanip>
struct MoneyPunct : public std::numpunct<char> { protected: virtual char do_thousands_sep() const override { return ','; } virtual std::string do_grouping() const override { return "\3"; } };
std::locale loc(std::locale::classic(), new MoneyPunct); std::cout.imbue(loc); std::cout << std::fixed << 12345678.90;
|
4、自定义操纵器(Manipulators)
#include <iostream> #include <iomanip>
struct Color { int code; Color(int c) : code(c) {} };
std::ostream& operator<<(std::ostream& os, const Color& c) { return os << "\033[" << c.code << "m"; }
Color red(31), green(32), reset(0);
std::cout << red << "Error: " << reset << "File not found\n";
|
这些东西,也许在制作一个调试系统的时候会有用…