hi why cerr make my program so slow?

ok i pratice my programming in uva. When I submit this code (uva 10284)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<bits/stdc++.h>
using namespace std;
#define sync ios_base::sync_with_stdio(0);cin.tie(0)
#ifndef ONLINE_JUDGE
	#define gc getchar
	#define pc putchar
#else
	#define gc getchar_unlocked
	#define pc putchar_unlocked
#endif
#define sc(a) cin>>a
//math function
#define up(x) int(double(x)/3+0.9)
#define round(x) int(double(x)/3+0.5)
#define maxi(a,b) ( (a) > (b) ? (a) : (b))
#define mini(a,b) ( (a) < (b) ? (a) : (b))
//repetition function
#define rep(i,a,b) for(int i = int(a); i <= int(b); i++ )
#define repd(i,a,b) for(int i = int(a); i >= int(b); i-- )
#define foreach(v, c) for( typeof((c).begin()) v = (c).begin(); v != (c).end(); ++v)
#define all(a) a.begin(), a.end()
#define in(a,b) ( (b).find(a) != (b).end())
#define pb push_back
#define fill(a,v) memset(a, v, sizeof a)
#define sz(a) ((int)(a.size()))
#define mp make_pair
#define range(x,a,b) ((x)>=(a) && (x)<=(b))
// Useful constants
#define INF (int)1e9
#define EPS 1e-9
// Useful hardware instructions
#define bitcount __builtin_popcount
#define gcd __gcd // greatest cmmon divisor in hardware fast
// Useful container manipulation / traversal macros
// Some common useful functions
#define checkbit(n,b) ( (n >> b) & 1)
#define UNIQUE(a) sort(all(a)); a.erase(unique(all(a)),a.end())
#define INDEX(arr,ind) (lower_bound(all(arr),ind)-arr.begin())
//debug
#ifdef DEBUG
#define debug(args...) {dbg,args; cerr<<endl;}
#else
#define debug(args...) // Just strip off all debug tokens
#endif
struct debugger
{
template<typename T> debugger& operator , (const T& v)
{
	cerr<<v<<endl;
return *this;
}
} dbg;

template<typename T> inline const T abs(T const& x)
{
	return (x<0) ? -x : x;
}
void attack(char board[8][8],char type,int x,int y)
{
	switch(type)
	{
		case 'P':
		if(y-1>=0)
		{
			if(x-1>=0 && board[y-1][x-1]=='.')board[y-1][x-1]='a';
			if(x+1<=7 && board[y-1][x+1]=='.')board[y-1][x+1]='a';
		}
		break;
		case 'p':
		if(y+1<=7)
		{
			if(x-1>=0 && board[y+1][x-1]=='.')board[y+1][x-1]='a';
			if(x+1<=7 && board[y+1][x+1]=='.')board[y+1][x+1]='a';
		}
		break;
		case 'r':
		case 'R':
		rep(i,x+1,7)
			if(board[y][i] == '.'||board[y][i] == 'a')board[y][i]='a';
			else break;
		repd(i,x-1,0)
			if(board[y][i] == '.' || board[y][i] == 'a')board[y][i]='a';
			else break;
		repd(i,y-1,0)
			if(board[i][x] == '.'||board[i][x] == 'a')board[i][x]='a';
			else break;
		rep(i,y+1,7)
			if(board[i][x] == '.'||board[i][x] == 'a')board[i][x]='a';
			else break;
		break;
		case 'n':
		case 'N':
		if(y-2>=0)
		{
			if(x-1>=0 && board[y-2][x-1]=='.')board[y-2][x-1]='a';
			if(x+1<=7 && board[y-2][x+1]=='.')board[y-2][x+1]='a';
		}
		if(y+2<=7)
		{
			if(x-1>=0 && board[y+2][x-1]=='.')board[y+2][x-1]='a';
			if(x+1<=7 && board[y+2][x+1]=='.')board[y+2][x+1]='a';
		}
		if(x-2>=0)
		{
			if(y-1>=0 && board[y-1][x-2]=='.')board[y-1][x-2]='a';
			if(y+1<=7 && board[y+1][x-2]=='.')board[y+1][x-2]='a';
		}
		if(x+2<=7)
		{
			if(y-1>=0 && board[y-1][x+2]=='.')board[y-1][x+2]='a';
			if(y+1<=7 && board[y+1][x+2]=='.')board[y+1][x+2]='a';
		}
		break;
		case 'b':
		case 'B':
		for(int i=x+1,j=y+1;i<=7 && j<=7;i++,j++)
			if(board[j][i] == '.' ||board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x-1,j=y+1;i>=0 && j<=7;i--,j++)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x+1,j=y-1;i<=7 && j>=0;i++,j--)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x-1,j=y-1;i>=0 && j>=0;i--,j--)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;
		break;
		case 'k':
		case 'K':
		if(y-1>=0)
		{
			if(board[y-1][x]=='.')board[y-1][x]='a';
			if(x-1>=0 && board[y-1][x-1]=='.')board[y-1][x-1]='a';
			if(x+1<=7 && board[y-1][x+1]=='.')board[y-1][x+1]='a';
		}
		if(y+1<=7)
		{
			if(board[y+1][x]=='.')board[y+1][x]='a';
			if(x-1>=0 && board[y+1][x-1]=='.')board[y+1][x-1]='a';
			if(x+1<=7 && board[y+1][x+1]=='.')board[y+1][x+1]='a';
		}
		if(x-1>=0 && board[y][x-1]=='.')board[y][x-1]='a';
		if(x+1<=7 && board[y][x+1]=='.')board[y][x+1]='a';
		break;
		case 'q':
		case 'Q':
		for(int i=x+1,j=y+1;i<=7 && j<=7;i++,j++)
			if(board[j][i] == '.' ||board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x-1,j=y+1;i>=0 && j<=7;i--,j++)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x+1,j=y-1;i<=7 && j>=0;i++,j--)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;
		for(int i=x-1,j=y-1;i>=0 && j>=0;i--,j--)
			if(board[j][i] == '.' || board[j][i] == 'a')board[j][i]='a';
			else break;

		rep(i,x+1,7)
			if(board[y][i] == '.'||board[y][i] == 'a')board[y][i]='a';
			else break;
		repd(i,x-1,0)
			if(board[y][i] == '.' || board[y][i] == 'a')board[y][i]='a';
			else break;
		repd(i,y-1,0)
			if(board[i][x] == '.'||board[i][x] == 'a')board[i][x]='a';
			else break;
		rep(i,y+1,7)
			if(board[i][x] == '.'||board[i][x] == 'a')board[i][x]='a';
			else break;
		break;
	}
}
int main()
{
	sync;
	#ifndef ONLINE_JUDGE
	freopen("input", "r", stdin);
	freopen("out", "w", stdout);
	freopen("out_err", "w", stderr);
	#else
	// online submission
	#endif
	string t;
	char board[8][8];
	while(getline(cin,t))
	{
		fill(board,'.');
		int x(0),y(0);
		int len = t.length()-1;
		rep(I,0,len)
			if(isdigit(t[I]))x+=t[I]-'0';
			else if(t[I] == '/'){y++;x=0;}
			else 
				{board[y][x] = t[I];x++;}
		rep(y,0,7)
			rep(x,0,7)
				if(board[y][x]!='.' && board[y][x]!='a')
					{attack(board,board[y][x],x,y);
				cerr<<x<<" "<<y<<' '<<board[y][x]<<'\n';					// this line
			rep(y,0,7)                    // this line
			{rep(x,0,7)                    // this line
				cerr<<board[y][x]; // this line
			cerr<<'\n';			// this line
			}                                      // this line
			cerr<<'\n';					// this line
					}	              // this line
		
		int ans=0;		
		rep(y,0,7)
			rep(x,0,7)
			if(board[y][x]=='.')ans++;
		cout<<ans<<'\n';
	}
	return 0;
}


the time is 1.396 second but when i remove the cerr(for debugging)the time show 0.003 second why this happen did cerr is very slow due to having no buffer?
Last edited on
> cerr is very slow due to having no buffer?

std::cerr does have a stream buffer; but for every output sent to it, the buffer is immediately flushed.

std::clog also writes to stderr, but in contrast to std::cerr, the buffer is not flushed for every write (or before every input from std::cin).

Also see std::nounitbuf http://en.cppreference.com/w/cpp/io/manip/unitbuf
Topic archived. No new replies allowed.