fwrite slowdown problem

ganeshkumar_1989's Avatar, Join Date: Jan 2011
Newbie Member
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 shabbir; 2Jan2011 at 12:41.. Reason: Code blocks
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Is your stack big enough to contain a 10MB array?
0
rameshb's Avatar, Join Date: Dec 2010
Go4Expert Member
great i will try this