Skip to content

Commit cdc05df

Browse files
committed
1.2: optimize io, time gain 50-70%
1 parent bfa783a commit cdc05df

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

hammingdec.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ int main(int argc, char *argv[])
8080
if(c == 8)
8181
{
8282
int x, kod[12], g = 0;
83-
while(fgetc(f) != EOF)
83+
x = fgetc(f);
84+
while(x != EOF)
8485
{
85-
fseek(f, -1, SEEK_CUR);
86-
x = fgetc(f);
8786
tobit(x, byte);
8887
for(int i = 0; i < 8; i++)
8988
{
@@ -104,15 +103,15 @@ int main(int argc, char *argv[])
104103
fputc(todec(byte), t);
105104
g = 0;
106105
}
106+
x = fgetc(f);
107107
}
108108
}
109109
else if(c == 12)
110110
{
111111
int tmp[4], x, kod[17], g = 0, flag = 0, schet = 0;
112-
while(fgetc(f) != EOF)
112+
x = fgetc(f);
113+
while(x != EOF)
113114
{
114-
fseek(f, -1, SEEK_CUR);
115-
x = fgetc(f);
116115
tobit(x, byte);
117116
kod[g] = byte[7];
118117
g++;
@@ -154,15 +153,15 @@ int main(int argc, char *argv[])
154153
g = 0;
155154
}
156155
schet++; //костыль 3
156+
x = fgetc(f);
157157
}
158158
}
159159
else if(c == 16)
160160
{
161161
int x, kod[21], g = 0;
162-
while(fgetc(f) != EOF)
162+
x = fgetc(f);
163+
while(x != EOF)
163164
{
164-
fseek(f, -1, SEEK_CUR);
165-
x = fgetc(f);
166165
tobit(x, byte);
167166
for(int i = 0; i < 8; i++)
168167
{
@@ -184,15 +183,15 @@ int main(int argc, char *argv[])
184183
}
185184
}
186185
}
186+
x = fgetc(f);
187187
}
188188
}
189189
else if(c == 32)
190190
{
191191
int x, kod[38], g = 0;
192-
while(fgetc(f) != EOF)
192+
x = fgetc(f);
193+
while(x != EOF)
193194
{
194-
fseek(f, -1, SEEK_CUR);
195-
x = fgetc(f);
196195
tobit(x, byte);
197196
for(int i = 0; i < 8; i++)
198197
{
@@ -214,15 +213,15 @@ int main(int argc, char *argv[])
214213
}
215214
}
216215
}
216+
x = fgetc(f);
217217
}
218218
}
219219
else if(c == 64)
220220
{
221221
int x, kod[71], g = 0;
222-
while(fgetc(f) != EOF)
222+
x = fgetc(f);
223+
while(x != EOF)
223224
{
224-
fseek(f, -1, SEEK_CUR);
225-
x = fgetc(f);
226225
tobit(x, byte);
227226
for(int i = 0; i < 8; i++)
228227
{
@@ -244,6 +243,7 @@ int main(int argc, char *argv[])
244243
}
245244
}
246245
}
246+
x = fgetc(f);
247247
}
248248
}
249249
else

hammingenc.c

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ int main(int argc, char *argv[])
7676
if(c == 8)
7777
{
7878
int x, kod[12];
79-
while(fgetc(f) != EOF)
79+
x = fgetc(f);
80+
while(x != EOF)
8081
{
81-
fseek(f, -1, SEEK_CUR);
82-
x = fgetc(f);
8382
tobit(x, byte);
8483
int g = 0;
8584
for(int i = 0; i < 12; i++)
@@ -102,18 +101,18 @@ int main(int argc, char *argv[])
102101
}
103102
fputc(todec(byte), t);
104103
}
104+
x = fgetc(f);
105105
}
106106
}
107107
else if(c == 12)
108108
{
109109
int x, kod[17], g = 0, flag = 0;
110-
while(fgetc(f) != EOF)
110+
x = fgetc(f);
111+
while(x != EOF)
111112
{
113+
tobit(x, byte);
112114
if(flag == 0)
113115
{
114-
fseek(f, -1, SEEK_CUR);
115-
x = fgetc(f);
116-
tobit(x, byte);
117116
for(int i = 0; i < 8; i++)
118117
{
119118
if((g != 0)&&(g != 1)&&(g != 3)&&(g != 7)&&(g != 15)&&(g != 31)&&(g != 63))
@@ -143,9 +142,6 @@ int main(int argc, char *argv[])
143142
}
144143
else if(flag == 1)
145144
{
146-
fseek(f, -1, SEEK_CUR);
147-
x = fgetc(f);
148-
tobit(x, byte);
149145
for(int i = 4; i < 8; i++)
150146
{
151147
if((g != 0)&&(g != 1)&&(g != 3)&&(g != 7)&&(g != 15)&&(g != 31)&&(g != 63))
@@ -189,15 +185,15 @@ int main(int argc, char *argv[])
189185
}
190186
g = 0;
191187
}
188+
x = fgetc(f);
192189
}
193190
}
194191
else if(c == 16)
195192
{
196193
int x, kod[21], g = 0;
197-
while(fgetc(f) != EOF)
194+
x = fgetc(f);
195+
while(x != EOF)
198196
{
199-
fseek(f, -1, SEEK_CUR);
200-
x = fgetc(f);
201197
tobit(x, byte);
202198
for(int i = 0; i < 8; i++)
203199
{
@@ -227,15 +223,15 @@ int main(int argc, char *argv[])
227223
}
228224
g = 0;
229225
}
226+
x = fgetc(f);
230227
}
231228
}
232229
else if(c == 32)
233230
{
234231
int x, kod[38], g = 0;
235-
while(fgetc(f) != EOF)
232+
x = fgetc(f);
233+
while(x != EOF)
236234
{
237-
fseek(f, -1, SEEK_CUR);
238-
x = fgetc(f);
239235
tobit(x, byte);
240236
for(int i = 0; i < 8; i++)
241237
{
@@ -265,15 +261,15 @@ int main(int argc, char *argv[])
265261
}
266262
g = 0;
267263
}
264+
x = fgetc(f);
268265
}
269266
}
270267
else if(c == 64)
271268
{
272269
int x, kod[71], g = 0;
273-
while(fgetc(f) != EOF)
270+
x = fgetc(f);
271+
while(x != EOF)
274272
{
275-
fseek(f, -1, SEEK_CUR);
276-
x = fgetc(f);
277273
tobit(x, byte);
278274
for(int i = 0; i < 8; i++)
279275
{
@@ -303,6 +299,7 @@ int main(int argc, char *argv[])
303299
}
304300
g = 0;
305301
}
302+
x = fgetc(f);
306303
}
307304
}
308305
else

0 commit comments

Comments
 (0)