0

I'm writing chucks of chat *ptr into file, but it takes me so long to finish. I don't know why.

for(int i = 1; i < 64048; i++){
    if(ptr[i] != NULL){
        fwrite(ptr[i], 1, strlen(ptr[i])-6, file);
        free(ptr[i]);
    }
}

there is array of char pointers storing 8186 bytes, and I want to write into file, but the time is so long.

I have 64048 chunks of string need to write into file.

What could cause the slowing fwrite() ??

thank you

lurker
  • 56,987
  • 9
  • 69
  • 103
Max Lin
  • 79
  • 1
  • 8
  • 2
    How long is it taking? You're writing a file that is 8186 x 64048, or over 500MB. I could see why it would take a considerable amount of time. – lurker Sep 15 '13 at 01:47
  • Is there any way you could avoid calling strlen? – Adam Burry Sep 15 '13 at 02:10
  • Took about 10 mins to finish, I need to transfer over 1GB file over UDP. For now, I only test 500MB. – Max Lin Sep 15 '13 at 03:33
  • But in my localhost, it takes only several seconds to finish, but on Emulab the writing is considerable slow! – Max Lin Sep 15 '13 at 03:53

1 Answers1

3

OP is using wrong method to size fwrite(), amongst other small errors.

If "array of char pointers storing 8186 bytes" means
1 each char array has 8186 significant bytes of data, use size 8186.
2 each char array is a NUL terminated string, then a test needs to occur to insure each is at least length 6. Not sure what this 6 is all about. Note: size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream )

I suspect that the OP's - 6 is causing a problem when strlen(ptr[i]) is less than 6. This would make the negative difference a very large size_t, which is usually unsigned for count in fwrite().

for(int i = 0; i < 64048; i++){  // start at 0
    if(ptr[i] != NULL){
        int result; 

        result = fwrite(ptr[i], 1, 8186, file);
        if (result != 8186) ; handle error;

        // or

        size_t len = strlen(ptr[i]) + 1; /// Add 1 for the NUL byte
        if (len < 6) ; handle error of whatever the OP's 6 is about.
        result = fwrite(ptr[i], 1, len, file);
        if (result != len) ; handle error;

        free(ptr[i]);
    }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256