I did a quick test to see if there is a significant difference. The code is slightly different from yours, but it still shows a difference in performance. In addition, I did not bother to take into account caching, etc.
You can see for yourself if this is significant.
Testing program:
#include <stdio.h>
#include <stdlib.h>
#ifdef TEST1
void test(char *filename, int n) {
int i;
FILE *fp;
for (i=0; i<n; i++) {
fp = fopen(filename, "a");
if (fp) {
fprintf(fp, "%d\n", i);
fclose(fp);
}
}
}
#else
void test(char *filename, int n) {
int i;
FILE *fp;
fp = fopen(filename, "a");
if (!fp)
return;
for (i=0; i<n; i++) {
fprintf(fp, "%d\n", i);
}
fclose(fp);
}
#endif
int main(int argc, char *argv[]) {
char *filename;
int n;
if (argc < 3)
return -1;
filename = argv[1];
n = atoi(argv[2]);
test(filename, n);
return 0;
}
Compilation and labeling commands:
gcc -DTEST1 -Wall -O3 -o test1 test.c
gcc -DTEST2 -Wall -O3 -o test2 test.c
time ./test1 test.bin n; rm test.bin
time ./test2 test.bin n; rm test.bin
The machine has a 2.6 GHz Core i7, 8 GB of RAM, running under OS X.
Results:
n | test1 | test2
-------+---------+---------
10 | 0.009s | 0.006s
100 | 0.036s | 0.006s
1000 | 0.340s | 0.007s
10000 | 2.535s | 0.011s
100000 | 24.509s | 0.041s
, , ? .
? , () .
? . ? 1000 . - , .
, , ?