Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C++
Java vs C++ speed (IO & Sorting)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="Michael.Boehnisch, post: 3487411"] Quick check, comment out the std::sort() call: with std::sort(): 375ms w/o : 281ms I seem to have a similar machine in terms of performance, compared to the original poster :-) The program's runtime is dominated by the I/O, executed in both cases by the same OS back-end functions. The rest of the program is mainly comparing and shoving around memory segments, I assume in both cases executed by library *machine* code. Its only natural to me, the execution time is near identical. Memory allocation seems to be no issue, at least not for C++ - if I comment out the buf.reserve() call, no change in runtime is noticeable. However, the Java code pre-allocates 5000 lines, the C++ version 50000. Somebody with a Java environment may check out what happens if the number is adjusted. (The example text is ~31,000 lines). One more thing caught my eye: The bible file contains a single empty line that is processed by the C++ version but not by the Java version. One extra empty line is not much, but induces O(log n) extra steps for the sorting. If I modify the C++ program to disregard the empty line, computing time goes down to 358ms (or 94ms --> 77ms for the sorting only!). Plus, the considerable effort for loading and initialization, and garbage collection of the Java VM is not included. While I agree in part, IMHO you are not referring to the right reason. The physical typing of the programs should not make a big difference - in C++ you can use really nifty constructs that save plenty of source bytes. However, most other developers will have problems reading your code - even you yourself may not be able to explain a code snippet you wrote "ad hoc" only one week later. Just as example, the infamous Ackerman function: int ack( const int m, const int n ) { return m?n?ack(m-1,ack(m,n-1)):ack(m-1,1):n+1; } This *is* valid C++ code - a real space-saver, horrible style. I would prefer the more typing intensive, but better manageable version: int ack( const int m, const int n ) { if ( m == 0 ) return n+1; if ( n == 0 ) return ack( m-1, 1 ); return ack( m-1, ack( m, n-1 ) ); } Both versions are not identical in run-time efficiency: the "nifty" version takes 5,4s on my system for ack(4, 1), the lengthy one 3,6s only. I have no quick explanation for the difference, though. For my feeling, too, the Java version looks clumsy style - it is harder to understand. just my EURO.02, Michael. [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C++
Java vs C++ speed (IO & Sorting)
Top