When it comes to writing & reading data (variables and objects) from & to files, our teacher told us to do it like this:
while(1){
if(fwrite(&object, sizeof object, 1, fp)!= 1){
break;
}
However, it seems rather logical that this should be faster:
int num = sizeof object;
while(1){
if(fwrite(&object, num, 1, fp)!=1){
break;
}
After all, evaluating the size of the object won't be done every iteration. So, I wrote a simple program to test this:
#include <stdio.h>
#include <stdlib.h>
struct block{
int val;
float mal; //the variables are irrelevant here
char *ch;
}object;
int main(void){
FILE *fp;
int f = sizeof object;
if((fp=fopen("file.bin", "wb"))==NULL){
puts("Unable to open file.");
exit(1);
}
int n = 2; //either set n to 1 or 2
switch(n){
case 1:
for(int i = 0; i <101; i++){
if(fwrite(&object, sizeof object, 1, fp)!=1){
puts("I/O error.");
break;
}
break;
}
case 2:
for(int i = 0; i <101; i++){
if(fwrite(&object, f, 1, fp)!=1){
puts("I/O error.");
break;
}
break;
}
}
puts("Fin.");
return 0;
}
However, when I ran this program multiple times in devc++, to my surprise there was basically no difference in time passed.
For n = 1
I got these times: 0.0292
, 0.02757
, 0.02946
, 0.02847
.
For n = 2
: 0.03022
, 0.028
, 0.02954
, 0.02885
.
The second option isn't faster and there appears to be no distinguishable pattern. What gives?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…