- #1
yungman
- 5,718
- 241
I have two questions here. In the program, I have two different boolean expressions, one using array A[10], the other just using individual variablesB(0..9). They both do the same thing as in the program. My question which one is faster in real life hardware? My guess is B(0..9) is faster even though it looks stupid. I think each variable use individual address, so it's one step access. The one using array has to take the starting address, add the index to get the address before accessing the value. I put a lot more emphasis on efficiency than make the code looks "nice".
Also, in the process of declaring the bool A[10], I found out I had to initialize with {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} INSTEAD of {1} for all 10 elements. Why?
Thanks
Also, in the process of declaring the bool A[10], I found out I had to initialize with {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} INSTEAD of {1} for all 10 elements. Why?
C++:
//Efficiency
#include <iostream>
using namespace std;
int main()
{
bool A[10] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
bool B0 = true, B1 = true, B2 = true, B3 = true, B4 = true, B5 = true,
B6 = true, B7 = true, B8 = true, B9 = true;
bool An, Bn; // result of AND
An = A[0] && A[1] && A[2] && A[3] && A[4] && A[5] && A[6] && A[7] && A[8] && A[9];
Bn = B0 && B1 && B2 && B3 && B4 && B5 && B6 && B7 && B8 && B9;
cout << " An = " << An << " Bn = " << Bn << "\n\n";
return 0;
}
Thanks