Which is better?

keep in mind:
1> that this is a sample, and my_func may have an item parameter which is not shown.
2> int a, and b are true variables in that there values may be other than shown, or may be calculated on the fly beforehand.
3> 10 is a constant...in every example we divide a and b by 10, just different methods are used.

method 1
Code:
int a = 1000;
int b = 100;
my_func(a, a / 10);
my_func(a, a / 10);
my_func(a, a / 10);
my_func(a, a / 10);
my_func(b, b /10);
my_func(b, b /10);
my_func(b, b /10);
my_func(b, b /10);

or

method 2
Code:
int a = 1000;
int b = 100;
int c = a / 10;
int d = b / 10;
my_func(a, c);
my_func(a, c);
my_func(a, c);
my_func(a, c);
my_func(b, d);
my_func(b, d);
my_func(b, d);
my_func(b, d);

or

method 3
Code:
int a = 1000;
int b = 100;
int c = 10;
my_func(a, a / c);
my_func(a, a / c);
my_func(a, a / c);
my_func(a, a / c);
my_func(b, b / c);
my_func(b, b / c);
my_func(b, b / c);
my_func(b, b / c);
 

Veracity

Developer
Staff member
Define "better". If you mean "most efficient", then method 2 is "better". ASH does not detect the common subexpressions; it fetches the variable and divides it by the constant every single time.

After that, method 1 is a teeny tiny bit more efficient than method 3 since getting the value of a constant is a teeny tiny bit faster than doing a variable reference.

If you turn on debug logging, ASH will trace execution and hopefully you'll be able to see the effects of the different methods directly. Although....maybe not. I'm actually not sure if ASH traces expression evaluation. I seem to recall noticing that it did not this summer, when I was adding Records and trying to soup up the tracing. I'm not sure I fixed that. Hmm.
 
Top