patterncMinor
Writing strings to a file
Viewed 0 times
filewritingstrings
Problem
This opens a file and writes 5120 times a string to it, and if the file doesn't exist it creates a new file. The name of the file and the string are chosen randomly. I would like to know if there is a way to speed up this code. It takes about 10 seconds at the moment. I was wondering if I did a recursion in the
escreve() function or if I create some space in memory with malloc() on the main and free it before each cycle. If it will speed the thing up.#define linhas 1024
#define vezes 5120
char *ficheiros[5]={"SO2014-0.txt","SO2014-1.txt","SO2014-2.txt","SO2014-3.txt","SO2014-4.txt"};
char *cadeias[10]={"aaaaaaaaa\n","bbbbbbbbb\n","ccccccccc\n","ddddddddd\n","eeeeeeeee\n","fffffffff\n","ggggggggg\n","hhhhhhhhh\n","iiiiiiiii\n","jjjjjjjjj\n"};
int random(int range)
{
int num;
num = rand() % range;
return num;
}
void escreve(char *fi,char *cad){
int l;
int file = open(fi, O_WRONLY | O_CREAT, S_IRWXU | S_IROTH ) ;
for(l=0;l<linhas;l++)
write(file,cad, strlen(cad));
close(file);
}
int main(){
int x,y,l,i;
srand(time(NULL));
for(i=0; i<vezes; i++){
x= random(5);
y=random(10);
escreve(ficheiros[x],cadeias[y]);
}
return 0;
}Solution
This is a very instructive example of wrong judgement. I understand (correct me if I am wrong) that you went for a low-level
The net effect is that your code invokes a system call for each 10 characters.
System call is an extremely expensive operation. It involves switching memory maps from a user to a kernel space, and on top of that it faces who knows how many cache misses. This is exactly the problem
This is how your original code behaves:
I changed opening a file to
and writing to it to
and the new timigs are
PS To make it a code review:
You shall
write to avoid seemingly unnecessary overhead of FILE * functions offered by a stdio library. Not only it made your code less portable, it in fact made id slower!The net effect is that your code invokes a system call for each 10 characters.
System call is an extremely expensive operation. It involves switching memory maps from a user to a kernel space, and on top of that it faces who knows how many cache misses. This is exactly the problem
stdio solves by internal buffering.This is how your original code behaves:
~/projects/play/cr/pedro $ time ./original
real 0m7.929s
user 0m0.484s
sys 0m7.432sI changed opening a file to
FILE * file = fopen(fi, "w");and writing to it to
fputs(cad, file);and the new timigs are
~/projects/play/cr/pedro $ time ./with-stdio
real 0m0.908s
user 0m0.276s
sys 0m0.584sPS To make it a code review:
You shall
#include at least , , and ``.Code Snippets
~/projects/play/cr/pedro $ time ./original
real 0m7.929s
user 0m0.484s
sys 0m7.432sFILE * file = fopen(fi, "w");fputs(cad, file);~/projects/play/cr/pedro $ time ./with-stdio
real 0m0.908s
user 0m0.276s
sys 0m0.584sContext
StackExchange Code Review Q#64820, answer score: 4
Revisions (0)
No revisions yet.