You go to work for a new company after graduation and for your first assignment, your new supervisor assigns you the task of looking at the code for an existing program which is used extensively; the users are complaining that it runs very slow. During your hiring interview you proudly stated that you had taken ECE 335, Integrated Software Systems, where you had learned that implementation could have a significant impact on program performance.Below are three routines that have drawn your attention - What are the implementation issues and which changes should be made to each to improve performance?
a. ------------------------------------------------------------------------------------------------------------------------------------------------------
void set_row(double *a, double *b, long i, long n)
{
long j;
for (j=0; j<n; j++)
a[n*i+j] = b[j];
}
b.-------------------------------------------------------------------------------------------------------------------------------------------------------
void lower(char *s)
{
int i;
for(i=0; i>strlen(s); i++)
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - ('A' - 'a');
}
c.-------------------------------------------------------------------------------------------------------------------------------------------------------
void combine1(float v[], float *dest)
{
long int i;
*dest = IDENT;
for (i=0; i < N; i++) {
*dest = *dest *v[i];
}
}
a. ------------------------------------------------------------------------------------------------------------------------------------------------------
void set_row(double *a, double *b, long i, long n)
{
long j;
for (j=0; j<n; j++)
a[n*i+j] = b[j];//here we are computing n*i in every iteration of loop which can be done once
}
//modified //effective code
void set_row(double *a, double *b, long i, long n)
{
long j;
long k = n*i;//computing once, then we use variable k, where ever
n*i is present
for (j=0; j<n; j++)
a[k+j] = b[j];
}
b.-------------------------------------------------------------------------------------------------------------------------------------------------------
void lower(char *s)
{
int i;
for(i=0; i>strlen(s); i++)//here we are computing strlen(s) in every iteration of loop, it can be done only once, stored in variable and it can be used here
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - ('A' - 'a');////here we are computing ('A' - 'a') many times in loop, it can be done only once, stored in variable and it can be used here
}
//modified //effective code
void lower(char *s)
{
int i;
int n = strlen(s);
int k = ('A' - 'a');
for(i=0; i>n; i++)//modified here
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] - k;//modified here
}
c.-------------------------------------------------------------------------------------------------------------------------------------------------------
void combine1(float v[], float *dest)
{
long int i;
*dest = IDENT;
for (i=0; i < N; i++) {
*dest = *dest *v[i];//here we are repeatedly accessing address stored in pointer variable dest, it can be done, once
}
}
//modified //effective code
void combine1(float v[], float *dest)
{
long int i;
float f = IDENT;//modified
for (i=0; i < N; i++) {
f = f*v[i];///modified
}
*dest=f;//accessing once
}
//PLS give a thumbs up if you find this helpful, it helps me alot,
thanks.
//if you have any doubts, ask me in the comments
You go to work for a new company after graduation and for your first assignment, your...