Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
679 views
in Technique[技术] by (71.8m points)

fastest way to write integer to file in C

I am doing a homework to programming in C. Bonus points are for quick writing to the file in there upload test system.

I am trying to write a number of lines each comprising three space delimited decimal integer strings and then ' ' in file. The problem is, that fprintf is too slow (their reference time is more or less 1/3 faster).

I have tried a lots of possibilities (everything is in one for loop). fprintf (too slow):

fprintf(f, "%d %d %d
", a[i], b[i], c[i]);

converting to string and then put the string into it - even worse:

sprintf(buffer, "%d", a[i]); //or: _itoa(_itoa(a[i], buffer, 10);
fputs(buffer, f);
fputc(' ', f);

is there any quick way to write integer numbers to simple text file (.txt) (the last solution has time 220ms, reference is 140ms for you to picture the time)? I have been trying and googling as hell, but nothing is working. But if the time is this short, there has to be some way!

PS: The numbers are integers all the time, size is 4 bytes, all the time in format:

a0 b0 c0
a1 b1 c1
a2 b2 c2
a3 b3 c3
etc...

More info: When I send the solution, I send only two files: file.h and file.c. No main etc... so everything is in their optimization. The solution should be in commands/algorithm (even in the description of the problem is statement, that fprintf is too slow and we should try something else to speed things up).

Thank you for everything!

Edit: since you want the whole code, here it is:

void save(const str_t * const str, const char *name)
{
  FILE* f;
  int i;

  if(str->cnt == 0)
      return;

  f = fopen(name, "w");
  if(f == NULL)
      return;

  for(i = 0; i < str->cnt; i++)
  {
      fprintf(f, "%d %d %d
", str->a[i], str->b[i], str->c[i]);
  }
  fclose(f);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using any variation of printf, the function will have to scan the format string to find %d, and parse it for any extra options (such as %-03d), and work accordingly. That is a lot of processing time. printf is awesome because it is super-flexible, not because it is fast.

If you use an itoa type function to write each number, you're still going to turn your integer into a string, then copy that string to the file. You will spend all your processing time moving between string-buffers and file writes.

I think your fastest approach will be to make an in-memory, really big buffer, write everything to that, then do ONE AND ONLY ONE write to dump that entire buffer to the file.

Outline:

char buffer[10000];
for(i = 0; i < str->cnt; i++)
{
    /* write to buffer */
}

fwrite(buffer, buffer_size, 1, my_file);  // One fast write.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...