I have heard a lot of guys here saying that C++ is as fast or faster than C in everything, but cleaner and nicer.
While I do not contradict the fact that C++ is very elegant, and quite fast, I did not find any replacement for critical memory access or processor-bound applications.
Question: is there an equivalent in C++ for C-style arrays in terms of performance?
The example below is contrived, but I am interested in the solution for real-life problems: I develop image processing apps, and the amount of pixel processing there is huge.
double t;
// C++
std::vector<int> v;
v.resize(1000000,1);
int i, j, count = 0, size = v.size();
t = (double)getTickCount();
for(j=0;j<1000;j++)
{
count = 0;
for(i=0;i<size;i++)
count += v[i];
}
t = ((double)getTickCount() - t)/getTickFrequency();
std::cout << "(C++) For loop time [s]: " << t/1.0 << std::endl;
std::cout << count << std::endl;
// C-style
#define ARR_SIZE 1000000
int* arr = (int*)malloc( ARR_SIZE * sizeof(int) );
int ci, cj, ccount = 0, csize = ARR_SIZE;
for(ci=0;ci<csize;ci++)
arr[ci] = 1;
t = (double)getTickCount();
for(cj=0;cj<1000;cj++)
{
ccount = 0;
for(ci=0;ci<csize;ci++)
ccount += arr[ci];
}
free(arr);
t = ((double)getTickCount() - t)/getTickFrequency();
std::cout << "(C) For loop time [s]: " << t/1.0 << std::endl;
std::cout << ccount << std::endl;
Here is the result:
(C++) For loop time [s]: 0.329069
(C) For loop time [s]: 0.229961
Note: getTickCount()
comes from a third-party lib. If you want to test, just replace with your favourite clock measurement
Update:
I am using VS 2010, Release mode, everything else default
A C-style array is nothing more than a block of memory that can be interpreted as an array; it is not a defined data type.
The STL algorithms also provide support for c-style arrays. If you have c-style arrays with known size, you can directly use them together with the algorithms.
A typical declaration for an array in C++ is: type name [elements]; where type is a valid type (such as int , float ...), name is a valid identifier and the elements field (which is always enclosed in square brackets [] ), specifies the length of the array in terms of the number of elements. int foo [5];
In a C++ array declaration, the array size is specified after the variable name, not after the type name as in some other languages. The following example declares an array of 1000 doubles to be allocated on the stack. The number of elements must be supplied as an integer literal or else as a constant expression.
Question: is there an equivalent in C++ for C-style arrays in terms of performance?
Answer: Write C++ code! Know your language, know your standard library and use it. Standard algorithms are correct, readable and fast (They know the best how to implement it to be fast on the current compiler).
void testC()
{
// unchanged
}
void testCpp()
{
// unchanged initialization
for(j=0;j<1000;j++)
{
// how a C++ programmer accumulates:
count = std::accumulate(begin(v), end(v), 0);
}
// unchanged output
}
int main()
{
testC();
testCpp();
}
Output:
(C) For loop time [ms]: 434.373
1000000
(C++) For loop time [ms]: 419.79
1000000
Compiled with g++ -O3 -std=c++0x
Version 4.6.3 on Ubuntu.
For your code my output is similiar to yours. user1202136 gives a good answer about the differences...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With