idealvin / gitalk

comments for Alvin's blog
0 stars 0 forks source link

字符流(fastream) | Book for CO #31

Open idealvin opened 3 years ago

idealvin commented 3 years ago

https://idealvin.github.io/cn/co/fastream/

include: co/fastream.h.

fastream fastream 用于取代 C++ 标准库中的 std::ostringstream。std::ostringstream 性能较差,实测比 snprintf 慢好几倍,fastream 在不同平台测试比 snprintf 快 10~30 倍左右。

fastream::fastream fastream() noexcept; explicit fastream(size_t cap); fastream(fastream&& s) noexcept; 第 1 个是默认构造函数,创建一个空的 fastream 对象,内部不会分配任何内存。

第 2 个构造函数用参数 cap 指定 fastream 的初始容量,即预分配 cap 字节的内存。 第 3 个是 move 构造函数,不会进行内存拷贝。 示例 fastream s; // 状态为空的 fastream, 未分配内存 fastream s(1024); // 预分配 1k 内存 fastream x(std::move(s); // move 构造函数, s 变成空对象 #fastream::operator= fastream& operator=(fastream&& s) noexcept; fastream 只支持 move 赋值操作,s 的内容被转移到 fastream 中,s 自身变成空对象。

lanqsh commented 2 years ago

fastream x(std::move(s); // move 构造函数, s 变成空对象 这里少一个右括号,我没眼花了吧。。。

idealvin commented 2 years ago

文档都在github上,可以直接修改、提交pr