1

I'm gating more then 1000 page faults in this program. can i reduce them to some smaller value or even to zero ? or even any other changes can speed up the execution

#include <stdio.h>
#include<stdlib.h>
int main(int argc, char* argv[])
{
  register unsigned  int u, v,i;
  register unsigned int arr_size=0;
  register unsigned int b_size=0;
  register unsigned  int c;
  register unsigned  int *b;
  FILE *file;
  register unsigned int *arr;
  file=fopen(argv[1],"r");
  arr=(unsigned int *)malloc(4*10000000);
  while(!feof(file)){
    ++arr_size;
    fscanf(file,"%u\n",&arr[arr_size-1]);
  }
  fclose(file);
  b=(unsigned int *)malloc(arr_size*4);
  if (arr_size!=0)
  {
    ++b_size;
    b[b_size-1]=0;

    for (i = 1; i < arr_size; ++i)
    {
      if (arr[b[b_size-1]] < arr[i])
      {
        ++b_size;
        b[b_size-1]=i;
        continue;
      }
      for (u = 0, v = b_size-1; u < v;)
      {
        c = (u + v) / 2;
        if (arr[b[c]] < arr[i]) u=c+1; else v=c;
      }
      if (arr[i] < arr[b[u]])
      {
        b[u] = i;
      }
      if(i>arr_size)break;
    }
  }
  free(arr);
  free(b);
  printf("%u\n", b_size);
  return 0;
}
schnaader
  • 49,103
  • 10
  • 104
  • 136

1 Answers1

0

The line:

arr=(unsigned int *)malloc(4*10000000);

is not a good programming style. Are you sure that your file is as big as 40MBs? try not to allocate the whole memory in the first lines of your program.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
  • i tried to use realloc() but it slowing down the program. i made but thank you my file may cost 15MB so i changed it to 4*3750000 – R Krishna Sagar Aug 19 '11 at 19:25