27 Replies - 6703 Views - Last Post: 04 November 2011 - 10:57 PM
#16
Re: Do Java applications run slower than C++ applications?
Posted 24 October 2011 - 04:18 PM
#17
Re: Do Java applications run slower than C++ applications?
Posted 24 October 2011 - 05:25 PM
In Java, the JVM might take advantage of OS and hardware optimizations; the but the Java programmer doesn't really concern themselves with it.
In any modern language, you're better of letting the compiler make the call. If you really want some insight into C, compile the same program at different levels optimization and look at the assembly generated.
Most programmers should program with an eye to ease of readability and maintenance. Let the compiler writers look at the bark and worry about counting bits with messy, cryptic code.
#18
Re: Do Java applications run slower than C++ applications?
Posted 24 October 2011 - 05:54 PM
In Java a Vector of objects is always a Vector of pointers to object9despite whatever the programmer will try to do). All slots in a Java Vector are 4 bytes long.
It is a frequent error in C++ to bulid Vector of objects, rather than object pointers, inserting an Object in the middle of the pointer may imply a lot of memory buffers to move.
#19
Re: Do Java applications run slower than C++ applications?
Posted 29 October 2011 - 11:16 PM
#20
Re: Do Java applications run slower than C++ applications?
Posted 30 October 2011 - 03:44 AM
hulla, on 30 October 2011 - 02:16 AM, said:
Cite your sources.
Calculations are best done by the CPU as directly as possible. A program that compiles to machine code would do that automatically. A Java program might do the same, or do it in the JVM, depending on the level of abstraction.
If you're writing calculation heavy code, and you care, you write it maximize efficiency. It's worth noting that maximum efficiency is rarely the same as maximum clarity. Java programmers tend to value their clarity. For C programmers, inherent obfuscation seems to be part of the game.
#21
Re: Do Java applications run slower than C++ applications?
Posted 30 October 2011 - 08:43 PM
hulla, on 30 October 2011 - 02:16 AM, said:
Where did you get that ? www.urbanlegens.com ? Please quote your source
And why trigonometric functions and not the others Math ones ?
#22
Re: Do Java applications run slower than C++ applications?
Posted 03 November 2011 - 10:49 AM
Found it: My topic on arc tangents.
He said that...
Quote
#23
Re: Do Java applications run slower than C++ applications?
Posted 03 November 2011 - 10:58 AM
#24
Re: Do Java applications run slower than C++ applications?
Posted 03 November 2011 - 10:58 AM
Also, it's talking about how some math functions, due their complexity, use lookup tables. This actually levels the field, since you're intentionally avoiding expensive lower level calls. In such a content, you'd expect a similarly implemented Java code to preform equivalently.
#25
Re: Do Java applications run slower than C++ applications?
Posted 03 November 2011 - 12:02 PM
The C++:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
struct Timer {
struct timespec ts;
int runRepetitions;
void startTimer() {
ts.tv_sec = ts.tv_nsec = 0;
clock_settime(CLOCK_PROCESS_CPUTIME_ID, &ts);
}
void stopTimer() {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
}
void showResult() const {
printf("%s(%d) time elapsed: %ld.%06ld\n", getName(), runRepetitions, ts.tv_sec, ts.tv_nsec);
}
void runTest(int repeat = 1000) {
runRepetitions = repeat;
startTimer();
while(repeat-- > 0) { runJob(); }
stopTimer();
showResult();
}
virtual const char *getName() const = 0;
virtual void runJob() const = 0;
};
struct AtanTest : public Timer {
const char *getName() const { return "AtanTest"; }
void runJob() const {
const double pi = 3.14159265;
for(double n = 0, step = pi/1000; n<pi; n+=step) { double result = atan(n); }
}
};
int main () {
AtanTest t;
t.runTest(100000);
return 0;
}
Results:
> g++ -lrt speedTests.cc > ./a.out AtanTest(100000) time elapsed: 6.21959041 > ./a.out AtanTest(100000) time elapsed: 5.975034260 > ./a.out AtanTest(100000) time elapsed: 6.20611574
The Java:
import java.util.*;
abstract class Timer {
long startTime;
double elapsed;
int runRepetitions;
public abstract String getName();
public abstract void runJob();
protected void startTimer() { startTime = System.currentTimeMillis(); }
protected void stopTimer() {
long end = System.currentTimeMillis();
elapsed = (end-startTime)/1000.0;
}
public void showResult() {
System.out.println(getName() + "(" + runRepetitions + ") time elapsed: " + elapsed);
}
public void runTest(int repeat) {
runRepetitions = repeat;
startTimer();
while(repeat-- > 0) { runJob(); }
stopTimer();
showResult();
}
public void runTest() { runTest(1000); }
}
class AtanTest extends Timer {
public String getName() { return "AtanTest"; }
public void runJob() {
final double pi = 3.14159265;
for(double n = 0, step = pi/1000; n<pi; n+=step) { double result = Math.atan(n); }
}
}
public class SpeedTests {
public static void main(String[] args) {
AtanTest t = new AtanTest();
t.runTest(100000);
}
}
Results:
>javac SpeedTests.java AtanTest(100000) time elapsed: 10.053 >javac SpeedTests.java AtanTest(100000) time elapsed: 10.054 >javac SpeedTests.java AtanTest(100000) time elapsed: 10.064
Surprise, in this particular test, C++ appear to be about 40% faster. Of course, C++ is faster than most things. Are you likely to call atan 100,000,000 times in your program? Then consider C++. It really depends on where your program spends most of it's time.
#26
Re: Do Java applications run slower than C++ applications?
Posted 04 November 2011 - 03:01 PM
This post has been edited by nick2price: 04 November 2011 - 05:07 PM
#27
Re: Do Java applications run slower than C++ applications?
Posted 04 November 2011 - 05:46 PM
Timing is always tricky. Regardless of the promises, you're usually limited to the precision of the system clock. For Java, you could measure it from the outside, but is wouldn't be an honest test. There is overhead in loading a Java program, due to marshaling the JVM. Once a Java program is running, that's no longer an issue.
Because of the clock resolution, timing simple jobs yields quirky results. You need to extend the job, to help mitigate this effect. Unfortunately, this brings other elements into the test that you'd probably prefer not to.
Short answer, all timing tests at inaccurate, but they're the only reasonable metric we have.
#28
Re: Do Java applications run slower than C++ applications?
Posted 04 November 2011 - 10:57 PM
So when the Java code is being run, it optimizes the code as it is being run. Why can't the C++ compiler optimize the code while it is being compiled?
Also, in response to pbl's code with the strlen, C++ uses std::strings. Doesn't the length method of an std::string simply return the length of the string?
|
|

New Topic/Question
Reply



MultiQuote





|