0

I wrote a program to read a 5MB file and perform some operation on it. I did it in two ways, first one is to read 1MB at a time, perform opeartion and read next 1MB

char line[1024*1024];
    while(!feof(file1))
    {
    k = fread(line,1024*1024,1,file1);

    for(i=0;i<strlen(line)-1;i++)
    line[i] = line[i+1];
    }

the next method is to read the 5MB at a time and perform operation.

char line[5*1024*1024];
    while(!feof(file1))
        {
        k = fread(line,5*1024*1024,1,file1);

        for(i=0;i<strlen(line)-1;i++)
        line[i] = line[i+1];
        }

the first one got completed in 3 minutes where as the second one took 26 minutes. My cache size is 3MB. Is there any significance of cache in making this difference? When I tried reading 3MB and doing operation and further reading remaining, it took 15 minutes. So I am totally clueless why this is happening. Plese tell how this is getting executed and why am I getting such a huge difference in execution time.

mit234
  • 11
  • Please see [this](http://stackoverflow.com/q/5431941/2173917). – Sourav Ghosh Mar 10 '15 at 10:05
  • I have modified the code. Plese have a look now. Though I do the separate strlen, there is no difference in execution time. – mit234 Mar 10 '15 at 10:08
  • @MichaelFoukarakis, both will do same work. The first one will iterate 5 times and the second one will read 5MB at a time and work. Basically file there is of 5MB size – mit234 Mar 10 '15 at 10:11
  • What compiler do you use? Which compilation flags, which operating system, which file system? – Basile Starynkevitch Mar 10 '15 at 10:12
  • 2
    The way you are using `strlen` is a bug: `line` is not guaranteed to be NUL-terminated - perhaps you meant `sizeof line` instead. As such, the second snippet is probably doing more work _while overrunning the buffer_. – Michael Foukarakis Mar 10 '15 at 10:12
  • 2
    Plus: the `feof()` is completely wrong. – joop Mar 10 '15 at 10:18
  • 2
    Not to mention the given snippets cannot possibly take 3 minutes to process a 5MB file. @mit234: You need to give a [complete reproducible example](http://sscce.org/). – Michael Foukarakis Mar 10 '15 at 10:27
  • I have removed strlen inside loop. Now, first code is getting executed in 24ms where second one in 7ms. Is this because we are accesing the file 5 times in fist one where it is only accessed once in second case that we are getting more time for first? – mit234 Mar 10 '15 at 11:35

0 Answers0