Wednesday, July 23, 2014

C programming-- throughput increment -decrement by little take care

"Optimum space maximum throughput"

Suppose you are creating on structure having fixed sized for example:

1.
struct abc {
     int a;
     char c[50];
};

2.
you can also write the same as
struct abc {
    int a;
    char *c;
};   and you will malloc like c= malloc(50);

and you want to do something like
struct abc *a=malloc(sizeof(struct abc));

So which way is better?

Yes it depends on application but if your structure size is fixed means any how your structure have int a + 50 bytes for variable in that case , you should go for 1.(first) implementation.

Why????
Because when you go for first implementation, whole memory will be continues, so OS(operating system) will load all memory in same page.  So there will be very less page-fault.

While in second implementation, it may possible that when you do c=malloc(50), memory of c will be allocated  in other page, so to access c OS may have page-faults.   Even you will also have to do extra system call malloc for allocating memory to variable c. So it would minimize your throughput.


Another thing is that you are saving space of sizeof(char*)  in implementation 1.Because in first implementation you are using char c[50]; that requires exactly 50*sizeof(char) bytes.

But in second implementation, you have bytes of sizeof(char *) + (50*sizeof(char));



<Sumit Kakadiya>



No comments:

Post a Comment