I have a function which will be called many times throughout my code. This function updates 2 values(passed as argument) in an array. Since it is called too many times, I want this function optimized.
Yes I do have a simplest code to do that,
int global_array[MAX];
int * ptr_to_my_global_array = &global_array[0];
main()
{
int a=0,b=0;
my_func(a,b);
a++;b++;
my_func(a,b);
a++;b++;
my_func(a,b);
a++;b++;
my_func(a,b);
a++;b++;
my_func(a,b);
a++;b++;
//and so on
}
void my_func(int c,int d)
{
*ptr_to_my_global_array = c;
ptr_to_my_global_array++;
*ptr_to_my_global_array = d;
ptr_to_my_global_array++;
}
Please do not suggest any mem_copy,mem_set solutions. There may be no other solution, but was just curious if I could make this faster.
If you’re testing out different things, you could compare it against:
Probably the optimizer will sort things out so that there’s no difference between the two, but you might as well see what difference (if any) it makes to the emitted code.
Basically, your function is so simple that it’s difficult to see how a human could do anything better than the optimizer does. So any real improvement to your program would need to be in
mainrather than inmy_func.