Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   fwrite slowdown problem (http://www.go4expert.com/forums/fwrite-slowdown-problem-t24339/)

ganeshkumar_1989 2Jan2011 11:53

fwrite slowdown problem
 
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

xpi0t0s 3Jan2011 18:59

Re: fwrite slowdown problem
 
Is your stack big enough to contain a 10MB array?

rameshb 4Jan2011 12:31

Re: fwrite slowdown problem
 
great i will try this


All times are GMT +5.5. The time now is 01:06.