Re: Java vs C++ speed (IO & Sorting)
On 20 Mar., 11:46, Razii <DONTwhatever...@hotmail.com> wrote:
On Thu, 20 Mar 2008 03:01:28 -0700 (PDT), peter koch
<peter.koch.lar...@gmail.com> wrote:
I also
notice that the time included does not involve releasing memory used
by the Java-program which is unfair as this time was measured in the C+
+ version.
You are not making sense. Where on earth is c+ releasing memory in the
code that I posted?
Be that as it is, I notice that the C++ version is fifty percent
shorter which suggests that developing with C++ will be quite a lot
faster.
No, it's generally accepted that developing in C++ is much slower and
difficult due to pathetic c++ library, no thread support, no network
library.
This is weird. If the C++ library is so bad I do not understand why
the C++ code in your example is so much clearer than the Java
equivalent with an "endless" loop that is exited in the middle.
Apart from that, the C++ philosophy is very different from the Java
one: Java has an "everything in one package" whereas in C++ you
typically use add-on packages. So if you use threading and networking,
just use e.g. Posix or Corba or boost which gives you everything.
As for the length of code I posted, I can jumble everything
together and make Java code look short :)
import java.io.*; import java.util.*; public class IOSort
{public static void main(String[] arg) throws Exception {
ArrayList<String> ar = new ArrayList<String>(50000); String line = "";=
BufferedReader in = new BufferedReader( new FileReader("bible.txt"));
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("output.txt"))); long start = System.currentTimeMillis();
while (true) { line = in.readLine(); if (line == null) break;
ar.add(line); } Collections.sort(ar); int size = ar.size();
for (int i = 0; i < size; i++) { out.println(ar.get(i));}
out.close(); long end = System.currentTimeMillis();
System.out.println("Time for reading, sorting, writing: "+ (end -
start) + " ms"); } }
I hope you are satisfied :))
Right. But count the number of statements: they are the same. And
still the same half time longer.
On a serious note, I also removed an unneeded line, (if (line.length()
==0) continue;) that was in the loop. That probably helped in speed=
..
It did? That would give you more lines to sort, wouldn't it?
So all in all, the above benchmark could never make me consider
switching languages.
Yawn. I really care what language you use (NOT).
I do not know your purpose of that test, but to me it confirms that
you should use C++ and not Java. I guess that must be of relevance
somewhere?
/Peter