Do Java applications run slower than C++ applications?

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 7773 Views - Last Post: 04 November 2011 - 10:57 PM

#16 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Do Java applications run slower than C++ applications?

Posted 24 October 2011 - 04:18 PM

Thank you, baavgai! I understand now. Although, wouldn't C be able to declare the loop variable i as a register variable? How much faster would that make the loop? Does java do this as well?
Was This Post Helpful? 0
  • +
  • -

#17 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Do Java applications run slower than C++ applications?

Posted 24 October 2011 - 05:25 PM

The register thing comes from a time of simpler, less robust, hardware. Today, most operating systems will take your request under consideration; but don't count on it. It's also not really a guarantee of speed, anyway.

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. ;)
Was This Post Helpful? 2
  • +
  • -

#18 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Do Java applications run slower than C++ applications?

Posted 24 October 2011 - 05:54 PM

Another good example is the use of Vector of Objects
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.
Was This Post Helpful? 0
  • +
  • -

#19 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Do Java applications run slower than C++ applications?

Posted 29 October 2011 - 11:16 PM

Trigonometric functions are slow in Java? Isn't trigonometry the one thing in high-speed programs that has to be as fast as possible?
Was This Post Helpful? 0
  • +
  • -

#20 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Do Java applications run slower than C++ applications?

Posted 30 October 2011 - 03:44 AM

View Posthulla, on 30 October 2011 - 02:16 AM, said:

Trigonometric functions are slow in Java?


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.
Was This Post Helpful? 1
  • +
  • -

#21 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Do Java applications run slower than C++ applications?

Posted 30 October 2011 - 08:43 PM

View Posthulla, on 30 October 2011 - 02:16 AM, said:

Trigonometric functions are slow in Java? Isn't trigonometry the one thing in high-speed programs that has to be as fast as possible?

Where did you get that ? www.urbanlegens.com ? Please quote your source
And why trigonometric functions and not the others Math ones ?
Was This Post Helpful? 0
  • +
  • -

#22 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Do Java applications run slower than C++ applications?

Posted 03 November 2011 - 10:49 AM

PlasticineGuy mentioned in one of my C++ topics that trigonometric functions in C++ are precompiled because they have to be as fast as possible. Let me find the topic.

Found it: My topic on arc tangents.

He said that...

Quote

Because trigonometry is often used in performance-critical graphics programming, these functions must be as fast as they possibly can be.

Was This Post Helpful? 0
  • +
  • -

#23 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Do Java applications run slower than C++ applications?

Posted 03 November 2011 - 10:58 AM

There is no real concrete evidence that trig functions run slower on Java than in C++. There are a couple of topics about it, but they are way back in 2008 or even 2005. I would think significant performance improvements have been gained since then.
Was This Post Helpful? 0
  • +
  • -

#24 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Do Java applications run slower than C++ applications?

Posted 03 November 2011 - 10:58 AM

The comment has nothing to do with Java.

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.
Was This Post Helpful? 0
  • +
  • -

#25 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Do Java applications run slower than C++ applications?

Posted 03 November 2011 - 12:02 PM

A quick and dirty test...

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.
Was This Post Helpful? 1
  • +
  • -

#26 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Do Java applications run slower than C++ applications?

Posted 04 November 2011 - 03:01 PM

I remember reading somewhere before that using System.currentTimeMillis() produces inaccurate results. Not sure how true this is.

This post has been edited by nick2price: 04 November 2011 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#27 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,761
  • Joined: 16-October 07

Re: Do Java applications run slower than C++ applications?

Posted 04 November 2011 - 05:46 PM

It's System.nanoTime() that folks seem to have an issue with. However, if you have a preferred option, I'd be happy to hear about it.

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.
Was This Post Helpful? 2
  • +
  • -

#28 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Do Java applications run slower than C++ applications?

Posted 04 November 2011 - 10:57 PM

Cool. :)
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?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2