fwrite slowdown problem

Discussion in 'C' started by ganeshkumar_1989, Jan 2, 2011.

  1. ganeshkumar_1989

    ganeshkumar_1989 New Member

    Joined:
    Jan 2, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I have a probelm with fwrite function. I use it to write large chunk of binary data to 24-bit bitmap(.bmp) files of resolution 2400x1500.
    In the program below, I read an image 1700.bmp and write the same to 1.bmp. I perform this infinite number of times. At the beginning, the writting is fast. But after about 25 images, the writting becomes too slow. When I remove the fwrite statement, the program executes very fast. So fwrite is the culprit. But I have to make the program run fast and at the same speed. Why is this problem caused and how to rectify it? I have copied the program below:
    Code:
    #include <stdio.h>
    #include<conio.h>
    #include<stdint.h>
    struct MagicNumber
    {
        unsigned char type[2]; 
    }BmpMN;
    
    struct INFOHEADER
    {
        unsigned int sizeByte; 
        unsigned short reserved1; 
        unsigned short reserved2; 
        unsigned int offsetbits; 
        unsigned int sizeHeader; 
        int width;
        int height;
        unsigned short planes; 
        unsigned short bitcount; 
        unsigned int compression; 
        unsigned int sizeImage; 
        int xpelspermeter;
        int ypelspermeter;
        unsigned int colorsused;
        unsigned int colorsimportant;
    } BitmapInfoHeader; 
    
    int main()
    {
        FILE *ip,*op;
        int i=1;    
        uint8_t buff[1500][2400*3];
        while(1)
        {
            printf("Reading %d\n",i);
            ip=fopen("op/1700.bmp","rb");
            op=fopen("op/1.bmp","wb");
            fread(&BmpMN,sizeof(BmpMN),1,ip);      
               fread(&BitmapInfoHeader,sizeof(BitmapInfoHeader),1,ip);
            fread(&buff,sizeof(buff),1,ip);
            fwrite(&BmpMN,sizeof(BmpMN),1,op);      
               fwrite(&BitmapInfoHeader,sizeof(BitmapInfoHeader),1,op);
            //This statement is executed slower from 26th image onwards
            fwrite(&buff,sizeof(buff),1,op);        
            fclose(ip);        
            fclose(op);
            i++;
            fflush(&buff);
        }
        return 0;
    }    
    
    Thank you
     
    Last edited by a moderator: Jan 2, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Is your stack big enough to contain a 10MB array?
     
  3. rameshb

    rameshb New Member

    Joined:
    Dec 10, 2010
    Messages:
    35
    Likes Received:
    1
    Trophy Points:
    0
    great i will try this
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice