快写竟然比cout关同步流慢这么多??!!
  • 板块灌水区
  • 楼主Lin_On_Work
  • 当前回复18
  • 已保存回复18
  • 发布时间2023/10/13 19:38
  • 上次更新2023/11/2 14:05:17
查看原帖
快写竟然比cout关同步流慢这么多??!!
309988
Lin_On_Work楼主2023/10/13 19:38

上午模拟赛,被输出卡了好久,结果把快写换成cout,就从8秒跑进半秒!

我的快写:

#include <bits/stdc++.h>
#define gc getchar
#define pc putchar

template <typename T>
void write(T x) {
	if (x < 0) pc('-'), x = -x;
	static int st[40]; int tp = 0;
	do {st[tp++] = x % 10; x /= 10;} while (x);
	while (tp) pc(st[--tp] + '0');
}

int main() {
	freopen("test.out", "w", stdout);
	for (int i = 1; i <= 800000; i++) write(rand()), pc('\n');
	return 0;
}

等效的cout关同步流:

#include <bits/stdc++.h>

int main() {
	freopen("test.out", "w", stdout); 
	std::ios::sync_with_stdio(0); std::cout.tie(0);
	for (int i = 1; i <= 800000; i++) std::cout << rand() << '\n';
	return 0;
}

是因为我的快写写的太丑了吗???

2023/10/13 19:38
加载中...