Friday, May 29, 2009

Java is making me dumb

Sorry everyone who loves Java. But if you're a regular reader, you know about my love/hate relationship with Java. I hate it because I can't do the things I can do in C++ with it. But I love it because I'm making a pretty good living using it on Eclipse-based projects. So I deal.

Now, in my playing with Android and doing JNI development with it and OpenGL ES, I found myself immersed again in C++ for the first time in a very long time and loving it. One of the things you do with graphic programming is matrix multiplication. So I thought it would be cool if I created a Matrix class and an operator overload for the multiplication operator. So I could write code like this:

M4 myTransform = baseTransform * currTransform;

with all of these things as 4x4 matrices. Much less typing, no? :). So I coded it up and for the life of me I couldn't figure out why it wasn't working. Well, after mucking with it and trying different combinations I remembered something Bjorn Stoustrup mentioned in the C++ "bible". If you aren't doing anything special in your constructors, don't provide them. The compiler will take care of it for you and will do a better job of it than you could. So I removed them, and bingo, it worked!

Man I felt dumb, and at bit of a loss as to how I forgot how to do intense C++ programming. Clearly doing Java almost full time like I do, I'm out of practice. Now, you could argue that that's why people should stay away from C++, because it's too complicated. And, sure, it's not for the meek.

But I was trying to show the incredible benefits the gcc compiler gives you with performance over Java, especially in mobile where the JIT's aren't up to snuff (and Dalvik in particular doesn't even have one) and gcc can optimize using extended hardware capabilities like SIMD instructions. So there is a method to my madness and you put up with the pain to reap the rewards. Anyway, more on that after the Ottawa demo camp. Don't want to spoil the show :).

20 comments:

  1. This article is making me dumb. :-)

    ReplyDelete
  2. Ah, but aren't the assembler guys still moaning about C++ making them dumb?

    Higher languages bring the ability to quickly bring lots of functionality, but at a price...

    Happy coding!

    Rick

    P.S. For those of us just learning assembly language, check out the excellent tutorials on SecurityTube.net. Knowledge for nothing!

    ReplyDelete
  3. And somebody said that one should use the right tool for the job.

    And someone else said, that if you have a hammer everything else looks like a nail.

    And so forth.

    Personally, I would not call myself, dumb, yet simply out of practice. Also, I would not blame Java but myself for not keeping in touch.

    So, you regret that all cars don't have stick any more? Or that you don't have to personally tune the carburetor? Etc, etc.

    Needless to say, I dislike the conclusion that it is Java that makes one dumb. It didn't my wife and she know nothing about. Wow.

    ReplyDelete
  4. Sounds like you're just out of practice. Kind of like playing too much basketball and you've forgotten how to play football.

    ReplyDelete
  5. Java is not making you dumb. You're using a failed construct (operator overloading) which caused you grief. Java does not implement operator overloading for good reasons - one of which you just discovered.

    ReplyDelete
  6. Bonus points if you can implement your overloaded operator with ASM blocks that use hardware matrix math instructions.

    ReplyDelete
  7. Could be true Doug, perhaps you are getting "dumber" (heh)... but you afford some loss

    Imagine the poor souls who start from a lower rung on the ladder.

    ReplyDelete
  8. You really does not know in which world you live right?, You want to do Native programming in Java? ROFL!, It is easy, stop using Java and go back to your C++ messy and frankenstein language and stop bloggin stupid things. Unbelievable!.

    Im agree with the comment that assembly folks moaning of C++ made them dumb hahah.

    By the way I will use C instead of C++ any day.

    ReplyDelete
  9. Java isn't making you dumb. You're just getting older... :-)

    ReplyDelete
  10. Hey, you guys need to stop picking on Doug!
    So Doug, since you're doing Android development, why not use the javax.microedition.khronos.opengles package instead of JNI? Or you use JNI for something else?

    BTW... You are not alone. I too miss things like operator overloading, structs, and compiler directives. :)

    ReplyDelete
  11. How can we resist when Doug starts his post with "Java is making me dumb"? ;-)

    ReplyDelete
  12. I'm loving following the comments here. I can't argue with any of them, especially the first one :).

    @Barry - Basically the JNI call is per frame. All the animation and drawing is done in C++. I'm using Android's GLSurfaceView which deals with EGL et al and calls a Renderer for each frame. Works great.

    ReplyDelete
  13. Cool!
    Do you notice a big performance improvement doing it that way versus making it pure Java? I imagine if you're doing a lot of math calculations it could make a difference?

    ReplyDelete
  14. Yes I did. The Java was about 40 fps, and the C++ was around 57 fps. And that's on the emulator with a software renderor emulating OpenGL ES on an ARM CPU emulator. I imagine on a real device with real GL acceleration it'll be even more dramatic.

    ReplyDelete
  15. Hey, Doug, I think that your current project on C++/Java development on the Android platform is *really* interesting. Please keep us informed ;)

    OTOH, you java fanboys should stop being so picky. Java makes a great platform for programing, but you must admit that it lacks some essential features as operator overloading, compiler directives, structs, const-correctness, etc

    ReplyDelete
  16. Doug, maybe you should try Scala? Not only does it have operator overloading (well, not exactly because every operator is actually a method), performance matching Java, but also concise syntax, features from functional programming etc.

    In my ScalaBox2D physics engine port ( http://github.com/Villane/scalabox2d/tree/master ), I can use unciode operators, for example dot and cross products:

    def ∙(v: Vector2f) = x * v.x + y * v.y

    def ×(v: Vector2f) = x * v.y - y * v.x

    ReplyDelete
  17. @Villane I think you miss the point. I don't want performance that matches Java. I want performance that far exceeds Java, in particular, on Android.

    ReplyDelete
  18. Use an existing, expertly designed linear algebra library, such as Boost uBLAS (http://www.boost.org/doc/libs/1_39_0/libs/numeric/ublas/doc/index.htm) and you'll very quickly feel smart again (provided the compiler you're using is reasonably modern).

    ReplyDelete
  19. There is nothing smart or dumb about mentally caching a particular language's incidental complexity.

    The idea that a fairly sophisticated developer can't implement a naive matrix class with an overloaded multiplication operator fairly trivially says more about the language than the developer. The fact that all of your time was devoted to fighting the language and not actually addressing domain problems also speaks poorly of it. As a final warning against C++ we see that your development process degenerated to experimentation and repeating the mindless incantation of the high priest. Do you actually know what you were doing wrong?

    And despite all this you have the temerity to claim that Java is making you dumb. No, what is happening is you are yearning for the golden productivity of yesteryear, a time when you had just achieved a level of C++ sophistication which raised your level of productivity (relative to your prior self) an order of magnitude.

    And now you've moved with the industry onto VM languages, but you've never felt as productive as you did then. When a return to C++ didn't rekindle it, you blamed Java.

    Here's the bad news. It's probably never coming back. You only get a bump like that very rarely in a career. Anyway, the way to achieve it again is to move forward, not back. Try OCaml.

    ReplyDelete
  20. The good news is that it didn't take long to get used to the syntax of C++ again and now I'm as productive there as I am with Java and my code runs faster. Java helps you by hiding the complexities of the underlying hardware. But there are times when you need to leverage the hardware to meet certain requirements and C++ allows that. Android allows me the best of both worlds.

    ReplyDelete