Java vs C++ speed (IO & Sorting)

J

Jerry Coffin

You can't have your cake and eat it too. If they can perceive 500 ms,
then the can perceive the difference between downloading 1.5 kb file
and 90 kb file, especially on the dial up.

If they're on a sufficiently slow connection, of course they can
perceive the difference. The number of people on slow dial-up
connections, however is fairly small and steadily shrinking.

For people who have such slow connections, the Java Virtual Machine is
1) much less likely to already be installed and 2) almost certainly not
reasonable to install either.
 
J

Jerry Coffin

[ ... ]
That's exactly the point! If it was easy to check and factor the
number as composite, you won't be able to use it in encryption!
However, the probability that the number is prime is good enough that
you can use it to generate asymmetric keys.

Your knowledge of number theory appears to be somewhat shallow and
inaccurate. A deterministic proof that a number is prime is NOT at all
the same as a factoring an RSA key. The Miller-Rabin test (for one
example) can prove that a number is composite, but does NOT tell you the
prime factors of that number.
 
S

stan

Razii said:
Do you know who is being whining. You and some of your fellow nuts. My
post was just right on the topic. What have you and some of your
fellow nuts have contributed instead of whining? If you have a problem
with the a topic, move on to the next thread moron. This is
unmoderated USENET newsgroup -- do you know what that means?

Aparently you think it means yo can be rude, off topic, juvenile, and
call people names. With behavior like yours, it's no wonder you feel
compelled to troll other newsgroups with off topic nonsense. If there
were enough medication on the planet to cause me to act like you, I
wouldn't hang around my favorite language's newsgroup either. I'd
certainly go somewhere else so as not to annoy people who had
information I might need.

You haven't actually demonstrated any knowledge of usenet etiquite, so
it's impossible to know for sure if you are simply ignorant of such
matters or if you have such knowledge and chose to be a troll. Either
way, like all children you'll get tired sooner or later and the internet
will be a little better place.
 
P

pelio

(e-mail address removed) dixit:
Well I was not involved in that original topic, but I can tell you
that Java has improved a lot over the years. VM startup times aside,
there are VM's that will compile to native code on the fly, the byte
code optimizers have been greatly improved, there are even CPU's that
execute Java byte code directly

Really ? does this exist ? Someone spend millions to make a CPU java
specific ? Any pointers ?

(not on your test platform, but you'll
 
M

Mark Thornton

Razii said:
Do you know who is being whining. You and some of your fellow nuts. My
post was just right on the topic. What have you and some of your
fellow nuts have contributed instead of whining? If you have a problem
with the a topic, move on to the next thread moron. This is
unmoderated USENET newsgroup -- do you know what that means?

Comparisons with C++ are off topic in c.l.j.p and comparisons with Java
are off topic in c.l.c++

Your topic would be on topic in c.l.j.advocacy but presumably you don't
like the (lack of) audience in there.

Mark
 
R

Razii

This time I used the new java.nio package for reading and writing.
Result (on my comp) are really pathetic for c++ :)

Also, since the program spends most of the time in reading and writing
the file, I removed sorting from both java and C++ version (it's
irrelevant to IO test anyway).

(for one bible.txt)

Time for reading and writing files: 94 ms (java)
Time for reading and writing files: 78 ms (java)
Time for reading and writing files: 63 ms (java)

Time for reading and writing file: 156 ms (c++)
Time for reading and writing file: 156 ms (c++)
Time for reading and writing file: 156 ms (++)


Ten bibles (43 meg)

Time for reading and writing files: 766 ms (java)
Time for reading and writing files: 766 ms (java)
Time for reading and writing files: 782 ms (java)

Time for reading and writing file: 1828 ms (c++)
Time for reading and writing file: 1734 ms (c++)
Time for reading and writing file: 1703 ms (c++)

bible.txt is here
ftp://ftp.cs.princeton.edu/pub/cs126/markov/textfiles/bible.txt


========= Java version ===========
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class CopyFile
{
public static void main(String[] arg) throws Exception
{
FileChannel in = new FileInputStream("bible.txt").getChannel();
FileChannel out = new FileOutputStream("output.txt").getChannel();

long start = System.currentTimeMillis();
in.transferTo (0, in.size(), out);
out.close(); in.close();
long end = System.currentTimeMillis();

System.out.println("Time for reading and writing files: " +
(end - start) + " ms");

}
}

===== C++ Version =======
#include <ctime>
#include <fstream>
#include <iostream>
int main(int argc,char *argv[])
{

std::ifstream src("bible.txt");
std::eek:fstream dst("output.txt");
clock_t start=clock();
dst << src.rdbuf();
clock_t endt=clock();

std::cout <<"Time for reading and writing file: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
return 0;
}
 
R

Razii

Im not sure if i used the right optimization flags to compile the java
program so if its wrong please tell me how i should compile it, since
i expected to obtain similar results.

Try these verions (only IO test) with g++ (in this version java is
using nio package for reading and writing).

Let me know what time you get for Java and c++ with g++


=== java ===
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class CopyFile
{
public static void main(String[] arg) throws Exception
{
FileChannel in = new FileInputStream("bible.txt").getChannel();
FileChannel out = new FileOutputStream("output.txt").getChannel();

long start = System.currentTimeMillis();
in.transferTo (0, in.size(), out);
out.close(); in.close();
long end = System.currentTimeMillis();

System.out.println("Time for reading and writing files: " +
(end - start) + " ms");

}
}

===== C++ Version =======
#include <ctime>
#include <fstream>
#include <iostream>
int main(int argc,char *argv[])
{

std::ifstream src("bible.txt");
std::eek:fstream dst("output.txt");
clock_t start=clock();
dst << src.rdbuf();
clock_t endt=clock();

std::cout <<"Time for reading and writing file: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
return 0;
}
 
R

Razii

Your knowledge of number theory appears to be somewhat shallow and
inaccurate. A deterministic proof that a number is prime is NOT at all
the same as a factoring an RSA key. The Miller-Rabin test (for one
example) can prove that a number is composite, but does NOT tell you the
prime factors of that number.

Your reading skills appear to be shallow. That was exactly my point.
It's easier to generate prime numbers using various algorithms than do
factoring.
 
R

Razii

If they're on a sufficiently slow connection, of course they can
perceive the difference. The number of people on slow dial-up
connections, however is fairly small and steadily shrinking.

Even on fastest connection, the time difference between downloading
1.5 KB and 90 KB file would be larger than 500 ms. So you are stuck
with a foot in mouth :) You can't have it both ways.
 
R

Razii

Either
way, like all children you'll get tired sooner or later and the internet
will be a little better place.

Since you have a very thin skin, you will always find something that
bothers you and gets under your skin, whether I am here or not. I
suggest you learn the filers that come with your newsreader client.
Add me to ignore and live happy ever after. How about that?
 
Z

zionztp

Im not sure if i used the right optimization flags to compile the java
program so if its wrong please tell me how i should compile it, since
i expected to obtain similar results.

Try these verions (only IO test) with g++ (in this version java is
using nio package for reading and writing).

Let me know what time you get for Java and c++ with g++

=== java ===
import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class CopyFile
{
public static void main(String[] arg) throws Exception
{
FileChannel in = new FileInputStream("bible.txt").getChannel();
FileChannel out = new FileOutputStream("output.txt").getChannel();

long start = System.currentTimeMillis();
in.transferTo (0, in.size(), out);
out.close(); in.close();
long end = System.currentTimeMillis();

System.out.println("Time for reading and writing files: " +
(end - start) + " ms");

}

}

===== C++ Version =======
#include <ctime>
#include <fstream>
#include <iostream>
int main(int argc,char *argv[])
{

std::ifstream src("bible.txt");
std::eek:fstream dst("output.txt");
clock_t start=clock();
dst << src.rdbuf();
clock_t endt=clock();

std::cout <<"Time for reading and writing file: " <<
double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";
return 0;

}

I've tried with that codes and the results changed a bit:

c++: 211 ms avg
java: 142 ms avg
Both tests using the 10x bible.

I suspected this test is not good to measure file IO speeds since in
my system (linux) after the first run the kernel seems to be
performing some kind of memory optimization so the files are actually
not read nor written from/to the HD at all.. so i tried using a 100x
bibles (430mb) file to test and the results where:

c++: 2856 ms avg
java: 16832 ms avg

So its obvious there is some problem with the timing functions because
the c++ program is taking longer than 3 sec but less than 28, after
testing both programs via the linux "time" command the average
complete execution time was:
c++ 15.97 sec.
java 16.13 sec.
 
A

Arved Sandstrom

Razii said:
Even on fastest connection, the time difference between downloading
1.5 KB and 90 KB file would be larger than 500 ms. So you are stuck
with a foot in mouth :) You can't have it both ways.

Errrr???? If the total time to download 90 KB is less than 500 ms (roughly a
1.5 Mbps download speed, these days that being provided by any ISP broadband
package except the slowest) then the time difference between downloading 1.5
KB and 90 KB can hardly be greater.

AHS
 
A

Arved Sandstrom

Razii said:
The new version is

C:\>java -version
java version "1.6.0_05"
Java(TM) SE Runtime Environment (build 1.6.0_05-b13)
Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)

For the basic operations carried out in this example code, are you seriously
suggesting that it would be better to have JDK 1.6.x rather than JDK 1.5.x?
Let's see, BufferedReader/BufferedWriter first showed up in JDK 1.1 (also
FileWriter/FileReader/PrintWriter), and ArrayList in 1.2 (the Collections
framework since 1.2). Maybe, just maybe, by 2008 you'd think a Java program
that used these classes shouldn't expect a serious speed bump from using the
latest JVM.

Note that I am not discounting the value of keeping up with new versions of
compilers and VMs...feature additions and modifications, bug fixes etc. But
I'd be pretty disappointed if I needed to move up from, say, JDK 1.4 to try
make _this_ Java program competitive.

AHS
 
R

Razii

Errrr???? If the total time to download 90 KB is less than 500 ms (roughly a
1.5 Mbps download speed, these days that being provided by any ISP broadband
package except the slowest)

You are right there, but if it's a large program that uses many
libraries the size difference between a compiled jar file and c++ exe
would be much bigger.
 
L

Lew

So its obvious there is some problem with the timing functions because
the c++ program is taking longer than 3 sec but less than 28, after
testing both programs via the linux "time" command the average
complete execution time was:
c++ 15.97 sec.
java 16.13 sec.

Interesting, in that it incorporates the JVM load time into the measurement.
 
J

Jerry Coffin

(e-mail address removed) dixit:

[ ... ]
Really ? does this exist ? Someone spend millions to make a CPU java
specific ? Any pointers ?

Yes. Sun created a specification for a processor they called PicoJava. I
don't think you can buy it as a finished chip, but the source is
available from:

http://www.sun.com/software/communitysource/processors/download_picojava
..xml

ARM also has a set of extensions for their microprocessor called
Jazelle. This doesn't attempt to execute _all_ byte-codes directly, just
to provide enough to make the JVM a lot smaller, simpler and faster.

http://www.arm.com/products/esd/jazelle_home.html

I'm pretty sure there are at least a few others, but I doubt any of them
has quite the market presence of Jazelle (most cell phones now use ARM
cores, and I'd guess a fair number of those include the Jazelle
extensions, though I've never tried to verify it).
 
Z

zionztp

Interesting, in that it incorporates the JVM load time into the measurement.

Indeed, the JVM load time seems to be arround 200ms here since thats
the difference between the time command result and the actual program
output.
 
J

Joshua Cranmer

I don't know what the terrain is like in comp.lang.java.programmer,
maybe we're more used to trolls in comp.lang.c++. I wouldn't pay too
much attention to Razii. Also it's like debates between "soda" and
"pop" or "Windows" and "Linux"... to which I say: oh lord, not
*again*.

We've had some recent bad trolls in c.l.j.p: a recent snipe by someone
complaining about Java being slow (you get used to that, fast). I still
have XahLee in my kill list, but I think he hasn't posted in several
months. Then there is He Who Must Not Be Named who (although dieing down
recently) had spammed several threads into very long threads, such as my
own Java 7 Features thread.

Seeing as most of my extended family lives in the Midwest and my
immediate family on the Eastern Seaboard, we will sometimes have long
arguments over whether or not it's "soda" or "pop." Or that catch-all
"drink" :)
FWIW, I'm a registered C++ programmer, however I find Java to be an
equally wonderful tool depending on the task. What I value most (among
many things) about C++ is that I can get close to the hardware using a
nice high-level syntax, and what I value most (also among many things)
about Java is its rich standard component library. I have never had
performance issues with either language, and I also have never had
problems expressing the program I want to write in either language
(especially since the introduction of Java generics).

My lingua franca is still Java, but I wholeheartedly agree with this
paragraph: I work equally well (modulo standard library usage) in Java
and C++, so using either one doesn't really matter to me.
 

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

Forum statistics

Threads
474,177
Messages
2,570,953
Members
47,507
Latest member
codeguru31

Latest Threads

Top