其实只是把 之前的文章 搬过来了。
注意:
- 由于在
int范围内,-2147483648 == -(-2147483648),所以在输出这个数的时候会出现问题,需要进行特判,或者转换成uint32_t后再输出(为方便理解,以下采用前者)。 - 部分非递归快写还需要特判
0。
该部分的评测结果均以 U588066 Fast Write 为例。
1. C++ 风格输出的优化
见「一、1、cin / cout 的优化(关闭同步 和 解除绑定)」
评测结果:输出 个长度为 的数字需要 ms。
2. C 风格的输出优化
先看 printf 函数的 测试结果:输出 个长度为 的数字需要 ms。
可见 printf 的输出性能不是特别理想,以下是代替 printf 的方法。
putchar(x)/putchar_unlocked(x)
与 getchar 类似,putchar 是用来输出 1 byte 的数据并将其转换为 char 类型进行输出(但实际上的接收值的类型是 int),且速度很快,故可以用逐位输出来代替 printf。
我们可以每次对一个正整数除以 10,然后记录余数,存放在数组里,最后逆序输出(也可以逆序存放,顺序输出),同时不要忘记要特判 0 和 -2147483648。
puts(_String)
puts 是一个用于快速输出 C 风格字符串(字符数组)的函数,接收值为 const char*,当输出到 \0 时停止。
考虑到我们要进行多次 putchar 操作,则我们可以把这些操作合并成一个 puts 操作。
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef int i32;
uint8_t fr[17];
inline char* to_str(int x) {
if (x == 0) return "0";
if (x == -2147483648) return "-2147483648";
uint8_t *tot = fr + 17;
if (x < 0) putchar(45), x = -x;
while(x) *(-- tot) = x % 10 | 48, x /= 10;
return (char*)tot;
}
main() {
int n, x; scanf("%d%d", &n, &x);
fputs(to_str(n), stdout); putchar(32); puts(to_str(x));
for (int i = 1; i <= n; ++ i) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
puts(to_str(x));
}
}测试结果:输出 个长度为 的数字需要 ms。
循环展开优化
考虑到一个 int 类型的变量最大为 10 位数,所以我们可以使用 10 个 if 语句代替 while 循环。
inline char* to_str(int x) { // Like this.
if (x == 0) return "0";
uint8_t *tot = fr + 17;
(x < 0) && (putchar(45), x = -x);
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10);
return (char*)tot;
}但实际上可能是因为分支预测,这段代码和上一段代码的运行时间基本一致,但下文的优化需要使用到循环展开。
3. fwrite 在快写上的应用
与 fread 相似,用于实现更快的快写。
函数原型:
size_t fwrite(const void * buffer, size_t size, size_t count, FILE * stream);fwrite 函数将 count 个对象写入给定的输出流,每个对象的大小为 size 个字节。
与 puts 不同的其中一点,puts 在输出到 \0 的时候就会停止输出,而 fwrite 可以指定输出范围,即使遇到 \0 也可以继续输出,并且不会像 puts 一样输出完成后还会加一个多余的 \n。
在上个版本的快写上进行修改,并结合 fread 快读的思想,我们同样需要创建一个 obuf 数组,用于存储输出内容。当存储到达一定量时,一次性输出并清空缓冲区。
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define BUF (1 << 22)
#define putchar(x) (now ++[0] = (x)) // 重定义 putchar
typedef int i32;
uint8_t obuf[BUF + 17], // 需要多预留一点空间,防止缓冲区溢出引起错误
*now = obuf;
uint8_t fr[17];
inline void write(int x) {
if (!x) return(void)(putchar(48));
if (x == -2147483648) return(void)(memmove(now, "-2147483648", 11), now += 11);
uint8_t* tot = fr + 17;
(x < 0) && (now ++[0] = 45, x = -x);
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10),
(x) && ((-- tot)[0] = x % 10 | 48, x /= 10);
memmove(now, tot, fr + 17 - tot); // 内存复制
now += fr + 17 - tot;
}
main() {
int n, x; scanf("%d%d", &n, &x);
write(n), putchar(32), write(x), putchar(10);
for (int i = 1; i <= n; ++ i) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
write(x), putchar(10);
if (now - obuf > BUF) { // 存储到达一定量,清空缓冲区
fwrite(obuf, 1, now - obuf, stdout);
now = obuf;
}
}
fwrite(obuf, 1, now - obuf, stdout); // 在程序结束时也要输出
}测试结果:输出 个长度为 的数字需要 ms。
4. 更多优化(包括 打表优化 与 宏定义封装)
- 与上文的快读中一样可以采用打表优化,不同的是,这次我们可以一次打四个数字。
- 使用
__builtin_expect(...,0/1)对第一个(伪)if语句进行优化。 - 考虑到
inline的内联不够彻底,我们可以直接使用宏定义。
根据 优化 1,我们可以写出以下打表代码:
int fnd[10000];
for (int n = 0000; n <= 9999; ++ n)
fnd[n] = (n/1000%10+'0')|((n/100%10+'0')<<8)|((n/10%10+'0')<<16)|((n%10+'0')<<24)然后使用宏定义初始化列表:
#define D(n) ((n)/1000%10+'0')|(((n)/100%10+'0')<<8)|(((n)/10%10+'0')<<16)|(((n)%10+'0')<<24)
#define D10(s) D((s)+0),D((s)+1),D((s)+2),D((s)+3),D((s)+4),D((s)+5),D((s)+6),D((s)+7),D((s)+8),D((s)+9)
#define D100(s) D10((s)+0),D10((s)+10),D10((s)+20),D10((s)+30),D10((s)+40),D10((s)+50),D10((s)+60),D10((s)+70),D10((s)+80),D10((s)+90)
#define D1000(s) D100((s)+0),D100((s)+100),D100((s)+200),D100((s)+300),D100((s)+400),D100((s)+500),D100((s)+600),D100((s)+700),D100((s)+800),D100((s)+900)
const unsigned fnd[] = {
D1000(0),D1000(1000),D1000(2000),D1000(3000),D1000(4000),
D1000(5000),D1000(6000),D1000(7000),D1000(8000),D1000(9000)
};左右个字符就完成初始化了。
最终代码如下:
#include <stdio.h>
#include <string.h>
#include <stdint.h>
typedef int i32;
#define K 11
// Fnd list
#define D(n) ((n)/1000%10+'0')|(((n)/100%10+'0')<<8)|(((n)/10%10+'0')<<16)|(((n)%10+'0')<<24)
#define D10(s) D((s)+0),D((s)+1),D((s)+2),D((s)+3),D((s)+4),D((s)+5),D((s)+6),D((s)+7),D((s)+8),D((s)+9)
#define D100(s) D10((s)+0),D10((s)+10),D10((s)+20),D10((s)+30),D10((s)+40),D10((s)+50),D10((s)+60),D10((s)+70),D10((s)+80),D10((s)+90)
#define D1000(s) D100((s)+0),D100((s)+100),D100((s)+200),D100((s)+300),D100((s)+400),D100((s)+500),D100((s)+600),D100((s)+700),D100((s)+800),D100((s)+900)
// End
#define _Write(y) {\
int $ = (y);\
uint8_t* tot = fr + K;\
($ < 0) && (now ++[0] = 45, $ = -$);\
($ > 999) && (*(int*)(tot-=4) = fnd[$%10000], $ /= 10000),\
($ > 999) && (*(int*)(tot-=4) = fnd[$%10000], $ /= 10000),\
($ > 9) && (*(uint16_t*)(tot-=2) = fnd[$%100] >> 16, $ /= 100),\
($) && ((-- tot)[0] = $ % 10 | 48, $ /= 10);\
memmove(now, tot, fr + K - tot);\
now += fr + K - tot;\
}
#define write(x) \
if (__builtin_expect(x == 0, 0)) now++[0] = 48;\
else if (__builtin_expect(x == -2147483648, 0)) memmove(now, "-2147483648", 11), now += 11;\
else _Write(x)
#define putchar(x) (now++[0] = (x));
int main() {
uint8_t fr[K];
uint8_t obuf[(1 << 22) + 17];
const int fnd[] = {
D1000(0),D1000(1000),D1000(2000),D1000(3000),D1000(4000),
D1000(5000),D1000(6000),D1000(7000),D1000(8000),D1000(9000)
};
uint8_t* now = obuf;
int n, x;
scanf("%d%d", &n, &x);
printf("%d %d\n", n, x);
for (int i = 1; i <= n; ++ i) {
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
write(x) putchar(10)
(now - obuf > (1 << 22)) && (fwrite(obuf, 1, now - obuf, stdout), now = obuf);
}
fwrite(obuf, 1, now - obuf, stdout);
}测试结果:输出 个长度为 的数字需要 ms。