I am writing a console program which will take a set of data and compare it to the database and reports if each parameter has changed or not.
The problem is to get it to work faster(comparison)..
1) The structure has around 20 members.
2) Also to hold multiple record , have defined array of structures..
3) Now for comparison I have nested for loop and check each members with if condition. Now this looks to be expensive method. any suggestion on how this can be made better?
an idea of the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
int a; bool b; string c; .. so on for 20 parameters };
struct data employe_data ;
for (i =0 ; i < 50; i++)
for (j=0 ; j < n ; j++) //n == new set of records entered.
if ( employe_data[i].a == new[j].a)
if( employe_data[i].b == new[j].b)
printf("same value of a\n");
printf(" not same value of b\n");
// ... so on for 20 different parameters..
The above comparison for 20 parameters for 50 records take quite long.Can this be reduce with a better algo or design.
P.S: Mentioned only the core problem of the code here as code does much more after this comparison, like delete or add or modify a record.
Please assume "a" to be serial & unique number for the whole database
When I say expensive for a user its blank screen for couple of secs(secs not milli or micro :) )..
just to figure out which register is changed don't i have to check all the 20 parameters of all the records?? (which is what i am currently doing)
Any way you can think of that would tell differences would work, turning the struct into a class would make a lot more sense, given the fact that you could have a variety of member functions that could tell you when the last change was made, what the difference is, and so on. You could even just add a group of data, keep a running total and compare the sum to tell if they are not equal.