Java vs C++ speed (IO & Sorting)

A

Andrei Zavidei

I wanted to create a string with 50 million chars (small letters from
a to z), but the program crashed...

//the function returns a string of length len
// with chars from a to z (small letters).
const len = 50000000;
void randomString(string &s)
{
srand((unsigned)std::time(0));
char cr [len+1];
for( int i = 0 ; i < len ; ++i )
{
int iNumber;
iNumber = rand() % 26 + 97;
cr = iNumber;
}

cr [len] ='\0';
s.assign(cr);

}

The above crashed when the length, len = 50000000 (50 million). How
come? Since I was trying to test something (something else this time,
as I have nothing else to do:)), an hour earlier I was doing the same
thing in java without any problem.

//returns a String of length len with lowercase letters from a to z
static String randomString(int len)
{
char[] cr = new char[len];
Random rd = new Random();
for (int i = 0; i < len; i++){
int num = rd.nextInt(26) + 97;
cr = (char) num;
}

return new String(cr);

}

They both look the same ... why the c++ version crashes?


Funny.
You say that "char[] cr = new char[len];" is the same as "char cr [len
+1];" ...
Please refresh your memories about heap and stack differencies. That
will guide you in the right direction.

Regards,
Andrei
 
R

Razii

n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

Java version also uses random function from library that returns
double.
 
I

Ian Collins

Razii said:
n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

Java version also uses random function from library that returns
double.
So, you can apply the same optimisation, can't you?
 
B

Bo Persson

Razii said:
n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

Java version also uses random function from library that returns
double.

So you want to benchmark string handling, and end up benchmarking
random number generation. Great!

Benchmarking is hard!



Bo Persson
 
A

Alf P. Steinbach

* Razii:
n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

Java version also uses random function from library that returns
double.

Why does it matter?

If you want fast you have been given fast.

If you want slow instead, no problem, that can also be provided. :)


Cheers, & hth.,

- Alf
 
C

Christopher

n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

Java version also uses random function from library that returns
double.

Razii, you've succeeded. We will all stop using C++ and promptly
switch to Java. I promise. Now don't you have a hobby or something to
get to?
 
R

Razii

n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;


I had trouble compiling this.... right complete function with loop and
function name and everything like I posted.
 
Z

zionztp

n = rand();
memcpy(&n, nt, 4);
b[i+0] = nt[0] % 26 + 97;
b[i+1] = nt[1] % 26 + 97;
b[i+2] = nt[2] % 26 + 97;
b[i+3] = nt[3] % 26 + 97;

I had trouble compiling this.... right complete function with loop and
function name and everything like I posted.

There is a bug in that code, the memcpy should be:
memcpy(nt, &n, 4);
nt is char*, n is int

Btw generating random numbers is not so simple, depending on "how
random" the numbers are it will take more time, and since we have no
idea (at least i don't) how C++/Java implement random number
generation we may be even comparing different things.
 
R

Razii

If you want fast you have been given fast.

He posted something that didn't compile. What is nt in his example?

I always post the complete thing that people can cut and paste. What's
with posting snippets?

Anyway, his example didn't compile so until he does it, c++ was
slower.
 
A

Alf P. Steinbach

* Razii:
He posted something that didn't compile. What is nt in his example?

I always post the complete thing that people can cut and paste. What's
with posting snippets?

Anyway, his example didn't compile so until he does it, c++ was
slower.

:)


Cheers,

- Alf
 
R

Razii

There is a bug in that code, the memcpy should be:

Can you save people some trouble and cut and paste the complete
function as you have it in your IDE (or text file, whatever). I wasted
needless time figuring what you meant by nt.

I don't feel like trying it again.
 
C

Christopher

If you have nothing to add to a thread, why post such nonsensical
posts?

Because, I really wonder what it is exactly you are trying to
accomplish with these posts.
 
I

Ian Collins

Razii said:
Can you save people some trouble and cut and paste the complete
function as you have it in your IDE (or text file, whatever). I wasted
needless time figuring what you meant by nt.

I don't feel like trying it again.
Then don't. Just show us the equivalent Java optimisation.

Sensible programmers use the standard libraries and look for
optimisations if and only if the library performance isn't good enough.
A C or C++ programmer always has access to the "metal", be that
embedded hardware of operating system APIs, so he or she will always be
able to optimise to the best solution the platform offers.

I assume Java also offer access to the native APIs?

For your file copy example, I could use the system page mapping API to
simply map the input and output files and memcpy from one to the other.
This was significantly faster than iostreams and quite possibly what
the Java library transferTo function was doing.
 
R

Razii



His opatamizing doesn't work ...

This crashes

void getRandomString( std::string& s, size_t len )
{
srand((unsigned)std::time(0));
std::string cr( len, '\0' );
for(int i = 0 ; i < len ; ++i )
{
char* nt;
int n = rand();
memcpy(&n, nt, 4);
cr[i+0] = nt[0] % 26 + 97;
cr[i+1] = nt[1] % 26 + 97;
cr[i+2] = nt[2] % 26 + 97;
cr[i+3] = nt[3] % 26 + 97;
}

s.swap( cr );
}
 
R

Razii

So, you can apply the same optimisation, can't you?


That is when you first give me the version that works, his version
crashed

//This crashes

void getRandomString( std::string& s, size_t len )
{
srand((unsigned)std::time(0));
std::string cr( len, '\0' );
for(int i = 0 ; i < len ; ++i )
{
char* nt;
int n = rand();
memcpy(&n, nt, 4);
cr[i+0] = nt[0] % 26 + 97;
cr[i+1] = nt[1] % 26 + 97;
cr[i+2] = nt[2] % 26 + 97;
cr[i+3] = nt[3] % 26 + 97;
}

s.swap( cr );
}
 
I

Ian Collins

Razii said:
Huh? You never posted an example for C++ that worked and was faster.
The code that he gave me crashed
Why don't you address the points in the rest of my post?

Anyway, I must be bored, this should be quicker:

void getRandomString( std::string& s, size_t len )
{
srand(std::time(0));

std::string cr( len, '\0' );

// The following code is based on the requirement that RAND_MAX
// shall be at least 32767.
//
const size_t byThree = (len/3)*3;

size_t i(0);

while( i < byThree )
{
const int iNumber( rand() );

cr[i++] = iNumber % 26 + 97;
cr[i++] = (iNumber>>5) % 26 + 97;
cr[i++] = (iNumber>>10) % 26 + 97;
}

while( i < len )
{
const int iNumber( rand() );

cr[i++] = iNumber % 26 + 97;
}

s.swap( cr );
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top