R
Razii
From an old post by James Kanze
Out of curiosity, I tried to test if the above is true. It didn't make
any difference. In fact, C++ was a bit faster (not by much, just 6%).
Probably due to array bound check in Java, if there is in indeed an
issue with C++ arrays, overall there is no difference.
==c++==
#include <iostream>
#include <cstdlib>
#include <ctime>
void fill (double* src, int len);
void smooth (double* dest,
double const* src,
int len );
int main()
{
const int len = 50000;
double src_array [len] = {0};
double dest_array [len] = {0};
fill(src_array, len);
clock_t start=clock();
for (int i = 0; i < 10000; i++)
smooth (dest_array, src_array, len);
clock_t endt=clock();
std::cout <<"Time smooth(): " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
// doesn't work without the following cout on vc++
std::cout << dest_array [0] ;
return 0;
}
void smooth (double* dest, double const* src, int len )
{
for (int i = 1 ; i < len - 1 ; i++ ) {
dest[ i - 1 ] = (src[ i - 1 ] + src[ i ] + src[ i + 1 ]) / 3 ;
}
}
void fill (double* src, int len)
{
srand((unsigned)std::time(0));
for (int i = 0 ; i < len ; ++ i ) {
src = rand();
}
}
==== java ========
import java.util.*;
public class Arrays{
public static void main (String[] arg)
{
final int LEN = 50000;
double[] src_array = new double [LEN];
fill(src_array, LEN);
double[] dest_array = new double [LEN];
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
smooth(dest_array, src_array, LEN);
long end = System.currentTimeMillis();
System.out.println("Time smooth(): " + (end - start) + " ms");
}
static void smooth (double[] dest, double[] src, int len )
{
for (int i = 1 ; i < len - 1 ; i++ ) {
dest[ i - 1 ] = (src[ i - 1 ] + src[ i ] + src[ i + 1 ]) / 3 ;
}
}
static void fill (double[] src, int len)
{
for (int i = 0 ; i < len; i++)
src = Math.random();
}
}
When I pass an "array" to a function in C/C++, I actually pass
a pointer to the first element. And the compiler, when it compiles the
function, only sees pointers -- it has no way of determining any
relationship between them. Consider, for example, a smoothing function
(in C or earlier C++):
void
smooth( double* dest,
double const* src,
size_t len )
{
for ( size_t i = 1 ; i < len - 1 ; ++ i ) {
dest[ i - 1 ] = (src[ i - 1 ] + src[ i ] + src[ i + 1 ]) / 3 ;
}
}
The compiler cannot arrange to use the src[ i + 1 ] value from the
preceding pass through the loop as the src[ i ] value in the current
pass, since the write to dest[ i - 1 ] might have changed it. In Java,
it can, since two arrays are either identical or disjoint.
This sort of code shows up frequently. In benchmarks from Java
suppliers comparing Java to C++, of course. But also in any number
of applications dealing with physical measures: meteorology, geophysical
research (oil prospection), etc.
Out of curiosity, I tried to test if the above is true. It didn't make
any difference. In fact, C++ was a bit faster (not by much, just 6%).
Probably due to array bound check in Java, if there is in indeed an
issue with C++ arrays, overall there is no difference.
==c++==
#include <iostream>
#include <cstdlib>
#include <ctime>
void fill (double* src, int len);
void smooth (double* dest,
double const* src,
int len );
int main()
{
const int len = 50000;
double src_array [len] = {0};
double dest_array [len] = {0};
fill(src_array, len);
clock_t start=clock();
for (int i = 0; i < 10000; i++)
smooth (dest_array, src_array, len);
clock_t endt=clock();
std::cout <<"Time smooth(): " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
// doesn't work without the following cout on vc++
std::cout << dest_array [0] ;
return 0;
}
void smooth (double* dest, double const* src, int len )
{
for (int i = 1 ; i < len - 1 ; i++ ) {
dest[ i - 1 ] = (src[ i - 1 ] + src[ i ] + src[ i + 1 ]) / 3 ;
}
}
void fill (double* src, int len)
{
srand((unsigned)std::time(0));
for (int i = 0 ; i < len ; ++ i ) {
src = rand();
}
}
==== java ========
import java.util.*;
public class Arrays{
public static void main (String[] arg)
{
final int LEN = 50000;
double[] src_array = new double [LEN];
fill(src_array, LEN);
double[] dest_array = new double [LEN];
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++)
smooth(dest_array, src_array, LEN);
long end = System.currentTimeMillis();
System.out.println("Time smooth(): " + (end - start) + " ms");
}
static void smooth (double[] dest, double[] src, int len )
{
for (int i = 1 ; i < len - 1 ; i++ ) {
dest[ i - 1 ] = (src[ i - 1 ] + src[ i ] + src[ i + 1 ]) / 3 ;
}
}
static void fill (double[] src, int len)
{
for (int i = 0 ; i < len; i++)
src = Math.random();
}
}