S
shuisheng
Dear All,
I have the following example and try to improve its efficiency.
#include<iostream>
using namespace std;
double a[] = {1.1, 2, 3, 4, 5, 6, 7, 8, 9.2};
template<double p[], int n>
inline
double Sum()
{
double result = 0;
for (int i = 0; i < n; ++i)
{
result += a;
}
return result;
}
int main()
{
cout << Sum<a, 9>();
return 0;
}
I hope the sum result can be calculated at compilation. But it isn't.
I look into the diassembly code, I see several fadd operations (the
loop is unrolled by the compiler). Any way can make the final sum
result is calculated at the compilation.
Thank you very much for your kind help!
Shuisheng
I have the following example and try to improve its efficiency.
#include<iostream>
using namespace std;
double a[] = {1.1, 2, 3, 4, 5, 6, 7, 8, 9.2};
template<double p[], int n>
inline
double Sum()
{
double result = 0;
for (int i = 0; i < n; ++i)
{
result += a;
}
return result;
}
int main()
{
cout << Sum<a, 9>();
return 0;
}
I hope the sum result can be calculated at compilation. But it isn't.
I look into the diassembly code, I see several fadd operations (the
loop is unrolled by the compiler). Any way can make the final sum
result is calculated at the compilation.
Thank you very much for your kind help!
Shuisheng