Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Java vs C++ speed (IO & Sorting) (http://www.velocityreviews.com/forums/t600301-java-vs-c-speed-io-and-sorting.html)

Razii 03-20-2008 06:10 AM

Java vs C++ speed (IO & Sorting)
 
This topic was on these newsgroups 7 years ago :)

http://groups.google.com/group/comp....5ebf877e25b287

I said then: "How about reading the whole Bible, sorting by lines, and
writing the sorted book to a file?"

Who remember that from 7 years ago, one of the longest thread on this
newsgroup :)

The text file used for the bible is here
ftp://ftp.cs.princeton.edu/pub/cs126...iles/bible.txt

Back to see if anything has changed

(downloaded whatever is latest version from sun.java.com)

Time for reading, sorting, writing: 359 ms (Java)
Time for reading, sorting, writing: 375 ms (Java)
Time for reading, sorting, writing: 375 ms (Java)

Visual C++ express and command I used was cl IOSort.cpp /O2

Time for reading, sorting, writing: 375 ms (c++)
Time for reading, sorting, writing: 390 ms (c++)
Time for reading, sorting, writing: 359 ms (c++)

The question still is (7 years later), where is great speed advantage
you guys were claiming for c++?

------------------- Java Code -------------- (same as 7 years ago :)

import java.io.*;
import java.util.*;
public class IOSort
{
public static void main(String[] arg) throws Exception
{
ArrayList ar = new ArrayList(5000);


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;
if (line.length() == 0)
continue;
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");
}
}

--------- C++ Code ---------------

#include <fstream>
#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace ::std;


int main()
{
vector<string> buf;
string linBuf;
ifstream inFile("bible.txt");
clock_t start=clock();
buf.reserve(50000);


while(getline(inFile,linBuf)) buf.insert(buf.end(), linBuf);
sort(buf.begin(), buf.end());
ofstream outFile("output.txt");
copy(buf.begin(),buf.end(),ostream_iterator<string >(outFile,"\n"));
clock_t endt=clock();
cout <<"Time for reading, sorting, writing: " << endt-start << "
ms\n";
return 0;

}

Tim H 03-20-2008 06:39 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Mar 19, 11:10 pm, Razii <fdgl...@hotmails.com> wrote:
> This topic was on these newsgroups 7 years ago :)
>
> http://groups.google.com/group/comp....5ebf877e25b287
>
> I said then: "How about reading the whole Bible, sorting by lines, and
> writing the sorted book to a file?"
>
> Who remember that from 7 years ago, one of the longest thread on this
> newsgroup :)
>
> The text file used for the bible is hereftp://ftp.cs.princeton.edu/pub/cs126/markov/textfiles/bible.txt
>
> Back to see if anything has changed
>
> (downloaded whatever is latest version from sun.java.com)
>
> Time for reading, sorting, writing: 359 ms (Java)
> Time for reading, sorting, writing: 375 ms (Java)
> Time for reading, sorting, writing: 375 ms (Java)
>
> Visual C++ express and command I used was cl IOSort.cpp /O2
>
> Time for reading, sorting, writing: 375 ms (c++)
> Time for reading, sorting, writing: 390 ms (c++)
> Time for reading, sorting, writing: 359 ms (c++)
>
> The question still is (7 years later), where is great speed advantage
> you guys were claiming for c++?
>
> ------------------- Java Code -------------- (same as 7 years ago :)
>
> import java.io.*;
> import java.util.*;
> public class IOSort
> {
> public static void main(String[] arg) throws Exception
> {
> ArrayList ar = new ArrayList(5000);
>
> 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;
> if (line.length() == 0)
> continue;
> 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");
> }
>
> }
>
> --------- C++ Code ---------------
>
> #include <fstream>
> #include<iostream>
> #include <string>
> #include <vector>
> #include <algorithm>
> #include <ctime>
> using namespace ::std;
>
> int main()
> {
> vector<string> buf;
> string linBuf;
> ifstream inFile("bible.txt");
> clock_t start=clock();
> buf.reserve(50000);
>
> while(getline(inFile,linBuf)) buf.insert(buf.end(), linBuf);
> sort(buf.begin(), buf.end());
> ofstream outFile("output.txt");
> copy(buf.begin(),buf.end(),ostream_iterator<string >(outFile,"\n"));
> clock_t endt=clock();
> cout <<"Time for reading, sorting, writing: " << endt-start << "
> ms\n";
> return 0;
>
> }


Did this include JVM startup time? What were the memory footprints?

Razii 03-20-2008 06:53 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Wed, 19 Mar 2008 23:39:01 -0700 (PDT), Tim H <thockin@gmail.com>
wrote:

>Did this include JVM startup time? What were the memory footprints?



Read the code.. you will see where the time comes from. What does
start time of virtual machine has to do with the time for reading,
sorting and writing file?



jason.cipriani@gmail.com 03-20-2008 06:57 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Mar 20, 2:10 am, Razii <fdgl...@hotmails.com> wrote:
> The question still is (7 years later), where is great speed advantage
> you guys were claiming for c++?


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 (not on your test platform, but you'll
find these on devices like PDAs and mobile phones).

C++ will bring you closer to the hardware you are developing for, that
is one of the strengths of the language, but Java can be just as
respectable as far as performance goes. It really just depends on what
you are using the language for. Use the most appropriate tool for the
job.

Also, comparing to your results 7 years ago, it looks like Java has
slowed down a bit, relatively. :-D

Jason

Razii 03-20-2008 07:03 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Wed, 19 Mar 2008 23:57:25 -0700 (PDT), "jason.cipriani@gmail.com"
<jason.cipriani@gmail.com> wrote:


>Also, comparing to your results 7 years ago, it looks like Java has
>slowed down a bit, relatively. :-D


Slowed down? It was 2080 ms in the google link that I posted. It's 359
ms this time (however, the bible.txt file was different back then. So
there can't be any comparison with the old times).


jason.cipriani@gmail.com 03-20-2008 08:16 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Mar 20, 3:03 am, Razii <DONTwhatever...@hotmail.com> wrote:
> On Wed, 19 Mar 2008 23:57:25 -0700 (PDT), "jason.cipri...@gmail.com"
>
> <jason.cipri...@gmail.com> wrote:
> >Also, comparing to your results 7 years ago, it looks like Java has
> >slowed down a bit, relatively. :-D

>
> Slowed down? It was 2080 ms in the google link that I posted. It's 359
> ms this time (however, the bible.txt file was different back then. So
> there can't be any comparison with the old times).


Key word: relatively. I was making a joke that the old C++:Java ratio
was 3400:2080 (1.0:0.6), and the new one is 375:370 (1.0:1.0).

Jason

Ian Collins 03-20-2008 08:20 AM

Re: Java vs C++ speed (IO & Sorting)
 
Razii wrote:
>
> --------- C++ Code ---------------
>
> #include <fstream>
> #include<iostream>
> #include <string>
> #include <vector>
> #include <algorithm>
> #include <ctime>


#include <iterator>

Is required for ostream_iterator.

> using namespace ::std;
>
>
> int main()
> {
> vector<string> buf;
> string linBuf;
> ifstream inFile("bible.txt");
> clock_t start=clock();
> buf.reserve(50000);
>
>
> while(getline(inFile,linBuf)) buf.insert(buf.end(), linBuf);
> sort(buf.begin(), buf.end());


Why not use a sorted container? Your example takes 120mS on my box,
using std::multiset reduces this to 90.

> ofstream outFile("output.txt");
> copy(buf.begin(),buf.end(),ostream_iterator<string >(outFile,"\n"));
> clock_t endt=clock();
> cout <<"Time for reading, sorting, writing: " << endt-start << "
> ms\n";


endt-start is in what ever unit the system returns from clock(), it
should be scaled by CLOCKS_PER_SEC.

--
Ian Collins.

peter koch 03-20-2008 10:01 AM

Re: Java vs C++ speed (IO & Sorting)
 
On 20 Mar., 07:10, Razii <fdgl...@hotmails.com> wrote:
> This topic was on these newsgroups 7 years ago :)
>
> http://groups.google.com/group/comp....5ebf877e25b287
>
> I said then: "How about reading the whole Bible, sorting by lines, and
> writing the sorted book to a file?"
>
> Who remember that from 7 years ago, one of the longest thread on this
> newsgroup :)
>

[snip]

First of all, I believe this is a bad test. A lot of the time will be
involved with I/O which the compilers cant really affect. 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.
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.
I also wonder what happens in the hypothetical case where you were
told that the solution produced was simply to slow. I know that C++
offers you lots of flexibility where you could program towards a
certain environment, using e.g. memory-mapped I/O. (*)
So all in all, the above benchmark could never make me consider
switching languages.

/Peter

(*) Simpler measures such as adjusting the buffers of the streams
could also have an effect.

jason.cipriani@gmail.com 03-20-2008 10:05 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Mar 20, 4:20 am, Ian Collins <ian-n...@hotmail.com> wrote:
> Why not use a sorted container? Your example takes 120mS on my box,
> using std::multiset reduces this to 90.


What kind of super computers are you guys using? I mean my machine is
a little over a year old but... took me 790ms on a 2.16GHz Core Duo,
7200 RPM SATA something or other hard drive, with GCC -O2 (MinGW, GCC
3.4.5), using QueryPerformanceCounter for timings. Multiset reduced it
to about 720; with 380 for read + sort and 340 for write.

Jason

Razii 03-20-2008 10:16 AM

Re: Java vs C++ speed (IO & Sorting)
 
On Thu, 20 Mar 2008 01:16:58 -0700 (PDT), "jason.cipriani@gmail.com"
<jason.cipriani@gmail.com> wrote:

>Key word: relatively. I was making a joke that the old C++:Java ratio
>was 3400:2080 (1.0:0.6), and the new one is 375:370 (1.0:1.0).


Read the whole thread.. (700+ posts .. is that still a record in this
group?) :)

In the end they whined, made me change compilers, then after I got
VC++, claimed there is a bug in VC++ 5.0 library, . I had to fix the
bug. c++ ended up slightly faster after all that. even then there was
nothing to brag about.



All times are GMT. The time now is 04:30 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.