Which language is better to learn? C++ or Java?

Moderator
Posts: 3,573
Joined: 2003.06
Post: #46
bronxbomber92 Wrote:There is D.

I don't know if D is actually any better (I've seen arguments going both ways), but I was talking about "wide-spread", which D is not Rasp

I should mention that I think LLVM might be the key to bringing forward a better bare-metal language.
Moderator
Posts: 373
Joined: 2006.08
Post: #47
Sauerbraten was written with the Cube engine, too, which was an engine intentionally designed to be weird.

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Member
Posts: 260
Joined: 2005.05
Post: #48
Najdorf Wrote:You may like the languages or not, still C & C++ compilers make the fastest executables around, and by far.

The "speed doesn't matter nowadays" thing may be true for desktop apps, but for games you often have to push things to the edge to stand out from the crowd, and for embedded games you need all the speed you can get.

Sure python or java may be more elegant, but until they get comparable speeds they're not an option I will consider.
That is not really true. It should read "native compilers make the fastest executables around, and by far", but C/C++ does not outperform other native compiling languages significantly, if at all.

But they do outperform Python, Java and Ruby, easily, and by far. (Java is the fastest of these if it has a decent JIT but it still is slow.)
Luminary
Posts: 5,143
Joined: 2002.04
Post: #49
Java is not slow. At certain tasks certain designs of Java code will be slower than certain designs of C/C++ code, and vice versa. You can google up plenty of benchmarks which "prove" Java is faster, or C/C++ is faster, which only goes to prove that it's not clear-cut by any means.
Moderator
Posts: 373
Joined: 2006.08
Post: #50
While it's debatable whether Java is slower than C++ or not, I would say that most Java programs are noticeably slower than C++, due to the fact that many people using the Java language seem to have an incomplete grasp of how to use it efficiently.

*EDIT*
That came out sounding way more like flame bait than I meant it to. I know that there are a lot of good Java programmers out there; I've been lucky enough to work by some of them in the past Smile

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Moderator
Posts: 3,573
Joined: 2003.06
Post: #51
Fair warning: I'm talking out my arse here Rasp

It's not flamebait as much as it is simply debatable. C/C++ largely rely only upon the proficiency of the programmer and the capabilities of the hardware. Java relies on the efficiency of the runtime environment as well as the proficiency of the programmer, on top of the capabilities of the hardware. Yes, theoretically C/C++ is faster, but in practice, those three things overlap greatly. So, as OSC is pointing out, it's pretty fuzzy to call it one way or another.

I would argue that Java, as it is, is generally "slower" from the viewpoint of it not being generally well-implemented, both in runtime environments (e.g. crappy gui kits and buggy VMs), as well as many programmers making a mess of what could otherwise be well-done. Being as how the "hardcore" programmers out there don't generally gravitate toward Java, and it seems like the "newbie" programmers do, in part from universities "teaching" Java, there is a lot of crappy Java code out there as well. None of this works in Java's favor.

IOW, it's not that Java can't be as fast or even faster than C/C++, just that there's a long track record of suckage that precedes it Wink
Member
Posts: 144
Joined: 2009.11
Post: #52
AnotherJake Wrote:I don't know if D is actually any better (I've seen arguments going both ways), but I was talking about "wide-spread", which D is not Rasp

I should mention that I think LLVM might be the key to bringing forward a better bare-metal language.

D recently nearly died when someone forked the specification and went of and wrote another compiler. They're already in some major trouble with two competing standard libraries (Tango and Phobos), with two active versions of the original language (D 1.x and D 2.x).

If you want to get something done, and perhaps get paid along the way, don't spend much more than a weekend with D. It's an interesting and fun language, but it's not a full-stack.

Everyone's favourite forum lurker!
https://github.com/NSError
Moderator
Posts: 133
Joined: 2008.05
Post: #53
I think it is important, especially as a moderator, to either know what you are talking about, research what you plan on talking about before talking about it (research does not include reading programming forums), or don't say anything at all.

I also think it is important, especially as a moderator, to not link to websites in signatures that may hinder the credibility of this website and spread false and misleading information.
Moderator
Posts: 3,573
Joined: 2003.06
Post: #54
longjumper Wrote:I think it is important, especially as a moderator, to either know what you are talking about, research what you plan on talking about before talking about it (research does not include reading programming forums), or don't say anything at all.

I also think it is important, especially as a moderator, to not link to websites in signatures that may hinder the credibility of this website and spread false and misleading information.

Points taken! Although I realize your message isn't directed at anyone in particular, it's a good reminder for all of us mods not to go nuts with the BS. It is actually important for mods to help maintain the "signal" level over the "noise".

Also, I would like to second your point on the sig links. I would encourage all moderators to review links in their signatures and make sure they reflect positively on iDevGames. If in any doubt, it'd be best to leave the links out of your sig. Smile While we know who everyone is around here, and it's not that big of a deal to us, visitors and web searches turn those links up, and with a moderator label under your name, it does matter to some degree in terms of this site's image.
Member
Posts: 749
Joined: 2003.01
Post: #55
Look I dont know what voodoo you must make to make java run faster than C++ and how people manage to change numbers to prove any point they want but running the Redneck Testâ„¢

int a=0;
int i,j;
for (j=0;j<10;j++)
{
for (i=0;i<1000000000;i++)
{
a=a+1;
}
}

I get Java running in 20 seconds vs C++ running 5 seconds on my Mac. 4x slower and that's just summing integers, if you just start using a 3d math class I'm afraid it's going to get much worse than that.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Moderator
Posts: 3,573
Joined: 2003.06
Post: #56
Najdorf Wrote:Redneck Testâ„¢

LOL

Your example helps to illustrate OSC's point. If all we had to do was run a Redneck Testâ„¢ on every language, we'd have only one language left: C. Rasp The point is that there is a *LOT* more which determines the relative efficiency of one language over another than simple number crunching. You have to take design patterns into account as well, not to mention the runtime environment. When taking all factors into account, the results sometimes wind up being subjective in nature and hence the endless flamewars, but they can also reveal some surprising speeds where you might not expect them.

[edit] Here, check this out: Language Benchmarks

And don't forget to read this page on that: The Computer Language Benchmarks Game
Member
Posts: 144
Joined: 2009.11
Post: #57
wyrmmage Wrote:While it's debatable whether Java is slower than C++ or not, I would say that most Java programs are noticeably slower than C++, due to the fact that many people using the Java language seem to have an incomplete grasp of how to use it efficiently.

*EDIT*
That came out sounding way more like flame bait than I meant it to. I know that there are a lot of good Java programmers out there; I've been lucky enough to work by some of them in the past Smile

As a battle-hardened and zestfully seasoned Java programmer I have to affirm this. Java prior to version 1.5 was bona-fide slow. 1.5 sped things up significantly. If you see slow 1.5+ Java, it's probably because the programmer is doing something silly.

Treating strings as mutable types is a big one I see quite often. This isn't the only problem, but it's a salient example. Too often Java programmers will += a string type, which is 100% wrong. A string in Java is an immutable type. Using a StringBuilder is the sign of a good Java programmer (or at least one who has been around the block enough to have used the profiler to figure out that java.lang.String causes problems when it's used like an accumulator).

So, no, Java itself isn't extraordinarily slow. There are, however, Java programmers who create extraordinarily slow code. I think that's a problem no matter what language you use, but it seems highly exacerbated with Java.

Everyone's favourite forum lurker!
https://github.com/NSError
Luminary
Posts: 5,143
Joined: 2002.04
Post: #58
Let's give that "redneck" benchmark a go:

redneck.c
Code:
int main() {
int a=0;
int i,j;
for (j=0;j<10;j++)
{
for (i=0;i<1000000000;i++)
{
a=a+1;
}
}
return 0;
}

Code:
/Developer/usr/bin/llvm-gcc-4.2 -O4 redneck.c
OneSadMBP13:Desktop keith$ time ./a.out

real    0m0.010s
user    0m0.000s
sys    0m0.002s
OneSadMBP13:Desktop keith$ time ./a.out

real    0m0.003s
user    0m0.000s
sys    0m0.002s

OK, so I have a 2.26GHz machine, so at one cycle per add it could be done with ten billion adds in 4.425 seconds. Clearly it's not doing ten billion adds. Hmm. Let's have a look at the assembly:

Code:
otool -toV a.out
a.out:
(__TEXT,__text) section
[...]
_main:
0000000100000f80    xorl    %eax,%eax
0000000100000f82    ret

Oh, look at that, it's optimized the entire benchmark away. It's turned that code into int main() { return 0; }. Clever. Let's see if we can short-circuit that:

Code:
OneSadMBP13:Desktop keith$ /Developer/usr/bin/gcc-4.2 -O3 redneck.c
OneSadMBP13:Desktop keith$ time ./a.out

real    0m0.011s
user    0m0.000s
sys    0m0.002s
Nope!
Code:
OneSadMBP13:Desktop keith$ /Developer/usr/bin/gcc-4.2 -O2 redneck.c
OneSadMBP13:Desktop keith$ time ./a.out

real    0m0.004s
user    0m0.000s
sys    0m0.002s
Nope!
Code:
OneSadMBP13:Desktop keith$ /Developer/usr/bin/gcc-4.2 -O redneck.c
OneSadMBP13:Desktop keith$ time ./a.out

real    0m0.004s
user    0m0.000s
sys    0m0.002s
Nope!
Code:
OneSadMBP13:Desktop keith$ /Developer/usr/bin/gcc-4.2 -O0 redneck.c
OneSadMBP13:Desktop keith$ time ./a.out

real    0m32.561s
user    0m32.453s
sys    0m0.042s

yay! OK, now let's try java:

redneck.java:
Code:
public class redneck {
public static void main(String[] args) {
int a=0;
int i,j;
for (j=0;j<10;j++)
{
for (i=0;i<1000000000;i++)
{
a=a+1;
}
}
}
}

Code:
javac redneck.java
OneSadMBP13:Desktop keith$ time java redneck

real    0m0.378s
user    0m0.279s
sys    0m0.061s
OneSadMBP13:Desktop keith$ time java redneck

real    0m0.309s
user    0m0.274s
sys    0m0.055s

OMG! Java is 0.309s where C is 32s! Java is 100x faster than C!

Oh wait, Java's clearly optimized away the entire benchmark too. The 0.309s time is comparable only to the 0.003s time of the C benchmark. Java is 100x slower to start up than C. Well, that's going to be a critical part of the performance of your game.

Compilers for both languages are smarter than the Redneck benchmark, though.
Member
Posts: 749
Joined: 2003.01
Post: #59
Hmm, that's weird, on OSX 10.5 , Xcode 3.1.2 and GCC 4.0, "Command Line Utility"->"C++ Tool" template, Release Build, Os optimization, it's running it all even if I didn't ask him to print a (java too...).

Whatever, you can use all the tricks you want to prove me wrong, but if you have him print a (ok, it overflows in this example, make a a float), you'll see it runs 4 times slower, and it's not for the startup time.

©h€ck øut µy stuƒƒ åt ragdollsoft.com
New game in development Rubber Ninjas - Mac Games Downloads
Luminary
Posts: 5,143
Joined: 2002.04
Post: #60
OK, Let's devise a benchmark that the compilers can't fold. Say, the fibonacci sequence. Let's include timing in the code so we don't measure startup time either:

redneck.c:
Code:
#include <stdio.h>
#include <sys/time.h>

int main()
{
    struct timeval t;
    gettimeofday(&t, NULL);
    long start = t.tv_sec * 1000 + t.tv_usec / 1000;
    
    long i, l = 1, m = 1;
    for (i = 0; i < 1000000000; ++i)
    {
        long temp = l;
        l = m;
        m = temp + m;
    }
    
    gettimeofday(&t, NULL);
    long end = t.tv_sec * 1000 + t.tv_usec / 1000;
    
    long duration = end - start;
    
    printf("Result: %ld\n", m);
    printf("Duration: %ld\n", duration);
    return 0;
}

And it takes about a second to run:

Code:
keith$ /Developer/usr/bin/llvm-gcc-4.2 -O4 redneck.c
keith$ ./a.out
Result: 1601461754847896408
Duration: 1087
Redneck keith$ ./a.out
Result: 1601461754847896408
Duration: 1086

Now Java's turn:

redneck.java:
Code:
public class redneck
{
    public static void main(String[] args)
    {
        long start = System.currentTimeMillis();
        long i, l = 1, m = 1;
        for (i = 0; i < 1000000000; ++i)
        {
            long temp = l;
            l = m;
            m = temp + m;
        }
        long duration = System.currentTimeMillis() - start;
        System.out.println("Result: " + m);
        System.out.println("Duration: " + duration);
    }
}

And it takes a little longer to run:
Code:
keith$ javac redneck.java
keith$ java redneck
Result: 1601461754847896408
Duration: 1412
keith$ java redneck
Result: 1601461754847896408
Duration: 1410

That's a little less than 1.3x slower. Certainly nowhere near 4x. Not something I'd be worrying about for a game, even.
Thread Closed 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Learn java ? icross 6 6,037 Apr 22, 2009 11:11 PM
Last Post: AnotherJake
  What to learn next? stevejohnson 3 3,133 Nov 19, 2006 04:08 PM
Last Post: unknown