Follow Slashdot stories on Twitter

 



Forgot your password?
typodupeerror
×
Java Programming

Java Performance Urban Legends 632

An anonymous reader writes "Urban legends are kind of like mind viruses; even though we know they are probably not true, we often can't resist the urge to retell them (and thus infect other gullible "hosts") because they make for such good storytelling. Most urban legends have some basis in fact, which only makes them harder to stamp out. Unfortunately, many pointers and tips about Java performance tuning are a lot like urban legends -- someone, somewhere, passes on a "tip" that has (or had) some basis in fact, but through its continued retelling, has lost what truth it once contained. This article examines some of these urban performance legends and sets the record straight."
This discussion has been archived. No new comments can be posted.

Java Performance Urban Legends

Comments Filter:
  • I wonder to what extent this exists in other languages? For example, there is an oft-cited tip that says using persistent database applications with LAMP applications increases performance. I've found in actual practice that this depends on a lot of factors such as server load, amount of available memory, etc.

    I remember in my Turbo Pascal programming days (heh) that a lot of people said that using Units would degrade performance. So I tried it both ways and it never really made a difference, for my applications anyways.

    I'd say before taking someone's word for it on a performance enhancing technique, test it out. Because not everything you read is true, and not everything you read will apply to every environment or every application.
  • Source (Score:1, Interesting)

    by Anonymous Coward on Saturday May 17, 2003 @10:11PM (#5983434)
    Post the source code here [sf.net] and all "urban legends" about it will soon dissappear.
  • It doesn't help... (Score:5, Interesting)

    by devphil ( 51341 ) on Saturday May 17, 2003 @10:15PM (#5983457) Homepage


    ...when one of the first issues of Java magazine published an article explaining the Java object runtime model, but made it little more than a FUD-filled advertisement. (What killed it for me: claiming that C++ vtbls are always, in every implementation, done as a huge array of function pointers inside each obvject. It wasn't a typo, either; they had glossy color diagrams that were equally deliberately false.)

    I think Java's a decent language, but it invented nothing new. Every language feature had been done before, and without the need for marketing department bullshit.

  • Times change (Score:4, Interesting)

    by onelin ( 116589 ) on Saturday May 17, 2003 @10:17PM (#5983464)
    I think a lot of the "urban legends" originated before CPUs were at a speed where it didn't matter. Past 500Mhz, as one professor put it, and the CPU starts being fast enough that handling Java doesn't slow it down significantly compared to before then...and now we're dealing with multi-GHz monsters.

    What the article said is true - JVMs have improved a lot. They are getting better and better, even today. My friend likes to fool around with all these little 3d demos in Java and even the latest JDK (1.4.2 beta) suddenly offers big performance boosts over the previous JDK. The fact that I refuse to ever use a beta Java SDK is another story, though...so I won't see those performance gains for a little while.
  • Java's memory usage (Score:2, Interesting)

    by zymano ( 581466 ) on Saturday May 17, 2003 @10:19PM (#5983472)
    Does anyone know why Java isn't more memory efficient? Lack of pointers?

    Isn't the memory usage one of negatives of java?

    While I don't care too much for java's wordy verbose syntax , anything that competes with Microsoft is A-OK in my book.

    If any of you think Sun is making a ton of money on java , check this link out. [com.com]

    I am beginning to feel sorry for SUN. They are also in some economic hard times laying off alot of people.

  • by philovivero ( 321158 ) on Saturday May 17, 2003 @10:21PM (#5983485) Homepage Journal
    Interesting. As a guy who's been a die-hard PostgreSQL for a number of years, and who recently accepted a job doing hardcore MySQL administration, I was dreading it, because everyone knows MySQL has bad transaction management, horrible administration nightmares, and is only good for developers.

    And I'm sure MySQL DBAs all know PostgreSQL is slow, bloated, and is only good for huge database rollouts.

    Except, well. You get the gist. I'm replying to this article because I now know first-hand that both camps are getting a lot of it wrong.

    I've written up what began as a final in-depth studied proof that MySQL wasn't ready for the corporate environment (because I'm a PostgreSQL guy, see?) but ended up reluctantly having to conclude MySQL is slightly more ready for the corporate environment than PostgreSQL!

    The writeup [faemalia.org] is on a wiki, so feel free to register and add your own experience. Please be ready to back up your opinions with facts.
  • Java is slow (Score:3, Interesting)

    by etymxris ( 121288 ) on Saturday May 17, 2003 @10:23PM (#5983502)
    I've programmed in Java before, and love it as a language. Unfortunately, even in the newer iterations of the JVM, you simply cannot trust the virtual machine to get rid of objects efficiently. If you are doing some heavy processing, you simply will exhaust all memory on your machine, even if you explicitly set references to null.

    If there is an "inner loop" of your application that needs performance above all else, and you need to program it in Java for whatever reason, there are two things you should get rid of:
    1. Memory allocations
    2. Function calls
    Of course, if you can do this in C/C++, it will also improve performance, but it is not as critical to be so careful in these lower level languages.

    I've just found that you can't trust the garbage collector, no matter how good people say it is. People have been saying it's great since the beginning of Java, and now they say, "It wasn't good before, but it is now." And they'll be saying the same thing in 3 more years. No matter what, the opportunistic garbage collection of C/C++ simply leads to better performance than any language that tries to do the garbage collection for you.
  • jav vm sucks (Score:2, Interesting)

    by Anonymous Coward on Saturday May 17, 2003 @10:26PM (#5983516)
    Fact: Due to similarities of the MSIL/Java Bytecode, Java bytecode can eb converted into MSIL bytecode. (though not vice versa, due to higher expressiveness of MSIL).

    Fact: Valenz and Leopold did a survey (summarrixed in the most recent issue of Dr Dobbs) whereby Java bytecode programs were converted to MSIL, and the .NET, Rotor, MONO, Sun, Blackdown, etc. VMs were compared. In each case, Microsoft's .NET VM had a 5-20% speed increase over the best java VM, and even the MONO and Rotor VMs had a 2-15% speed increase.

  • Re:Times change (Score:5, Interesting)

    by etymxris ( 121288 ) on Saturday May 17, 2003 @10:30PM (#5983532)
    JVMs are always improving, I'll definitely agree. But efficiency will always matter. I was writing my own regular expression parser (had to homebrew it for various reasons) that got statistical information from multiple megabytes of text data. Whatever performance improvement that could be made was noticed...a lot. Do you want to wait 60 seconds or 30 seconds for the results? It's a big difference.

    No matter what, there will always be applications that strain our machines to the cusp of their abilities. And there will always be things we want to do that our machines cannot handle. It's only by performance tuning that these tasks can go from impossible to possible.

    If John Carmack was forced to program in Java, for example, Doom would only now be possible. And Doom III wouldn't be possible for many more years. Performance matters. Not always, but often.
  • by Kjella ( 173770 ) on Saturday May 17, 2003 @10:34PM (#5983555) Homepage
    Running Freenet and only freenet

    javaw.exe - Mem usage 70,244K
    java.exe - Mem usage 9,808K

    According to task manager. Granted, now I got 512 to take from but it's still eating up much more memory than anything else.

    Kjella
  • by linhux ( 104645 ) on Saturday May 17, 2003 @10:36PM (#5983564) Homepage
    Exactly how does "string require careful attention"? I've seen this statement a couple of times, but only to suspect that many people don't really understand what Java Strings are.

    The first mistake, of course, is that people think that (a == b) == a.equals(b) which is, of course, only true if a and b are constant strings or one have invoked intern() on them.

    The second is to not realize that string concatenation with the "+" operator is a special case and only syntactic sugar for StringBuffer operations. Thus, someone not familiar with Java may accidentally generate huge amount of StringBuffer objects in loops.

    However, both these things are very fundamental Java knowledge and among the first thing you learn when studying Java. It's obvious that you don't start coding serious Java without knowing how try..catch..finally works, and equally obvious that you should the know about the deals with the String class.
  • by BrookHarty ( 9119 ) on Saturday May 17, 2003 @10:39PM (#5983575) Journal
    Really, I cant tell if the java programming language is slow, Synchronization is really slow, Declaring classes or methods final makes them faster or Immutable objects are bad for performance.

    But I can tell you, the that almost every Administration application that runs java, sucks out my soul. Trying to run java applications over X at long distances makes me want to commit suicide. (Lucky theres VNC, so its almost usable...) (I think its a Shared memory problem with the way it works with X windows.)

    Then there is the damn JVM's that each app needs, and how i can have multiple versions loaded, so each application works correctly. Java 1.1/1.2/1.3/1.4 and now 1.5 should take even more disk space. Doesnt seem anything is upgradable in java.

    And lets not forget, about how Java likes to interact with all custom window manager replacements on windows. For some reason the screen flickers every time you run a java app. (Havnt seen any answers, but it messes with lightstep, blackbox, geoshell, and even stardock applications.)

    Humm, and cut/paste sucks, yes you can use key combos, but sometimes in windows, its nice to select all, and copy. (Minor bitch, but still annoying when you have switch ways of doing things...)

    If you cant have command line, and you must have a GUI, for gods sake use a HTML. I now make it a point to go with vendors without Java interfaces, they clearly dont use their own products on a day to day basis.

    BTW, i said java Interfaces, not Java Beans, etc. We have java running on solaris, works fine, other than the memory leaks. Its the Admin applications that use Java that are crap.
  • by Anonymous Coward on Saturday May 17, 2003 @10:50PM (#5983618)
    If it's so bloody obvious, why does it need to be said? You'd be surprised how many times people "shoot themselves in the foot" writing things in C, in areas where Java performance is (now) ok. Or write something that needs the speed in C, but introduce bugs in optimizing something that would be better optimized by the compiler anyway.
  • I assume you know (Score:3, Interesting)

    by Anonymous Coward on Saturday May 17, 2003 @10:59PM (#5983650)
    that the operating system duplicates the amount of memory reported for each thread in a JAVA program?

    For example, should I have a program that has 8 threads and the whole thing uses 28 mb of memory.
    A process listing shows 8 entries each using 28 mb of memory, when in reality only 28 mb and not 224 mb (8 * 28) of memory is being used.

    Before you blame this one on JAVA too, you might want to know that it's a bug in the concept of process memory reporting (ie. the OS) not JAVA. The OS lists 8 scheduleable programs (the java threads) looking up the amount of memory each has access to (28 mb) without ever hinting that they are all using the same 28 mb.
  • by fm6 ( 162816 ) on Saturday May 17, 2003 @10:59PM (#5983652) Homepage Journal
    There are many reason why "urban legends" (why the quotes? explanation shortly) spread the way they do. Unfortunately, this article demonstrates one of the least forgivable reasons: people often get sloppy about their facts. It's inevitable that facts will get distorted as they go from ear to mouth to ear to mouth. But there's no need to hurry the process!

    What's sloppy about the article? Well first of all, Goetz asserts " even though we know they are probably not true, we often can't resist the urge to retell [urban legends]". Where in Hades did he get such a silly idea? Some half-remembered sociology book? Everybody who's ever told me an urban legend really believed in that exploding microwave poodle or the dead construction workers concealed in Hoover Dam. I myself remember feeling rather peeved when I heard the sewer alligator legend debunked.

    Second, perfomance myths are not "urban legends". ULs are third-hand stories that are difficult to debunk because the actual facts are hard to get at. "Facts" that people can check but don't are just myths or folklore.

    Anyway, here's my favorite performance myth: "more is faster". The most common variation of this is "application performance is a function of CPU speed". Ironically enough, I encountered this one when I was working at JavaSoft. Part of my job was to run JavaDoc against the source code to generate the API docs. The guy who did it before me ran it on a workstation, where a run took about 10 hours. Neither of us had the engineering background to really understand why this took so long. He just took for granted that it was a matter of CPU cycles. I knew a little more than him -- not enough to understand what was actually going on, but enough to be skeptical of his explanation.

    Eventually, I put together the relevent facts: (1) JavaDoc loads class files to gain API data, using the object introspection feature; (2) the Java VM we were using visited every loaded class frequently, because of a primitive garbage collection engine; (3) forcing a lot of Java classes into memory is a good way to defeat your computer's virtual memory feature...

    Eureka! I tried doing the JavaDoc run on a machine with enough RAM to prevent swapping. Run went from 10 hours to 10 minutes.

    Another variation of this myth is "interpreted code is slower than native code". That bit of folklore has hurt Java no end. If your application is CPU-bound, you might get a performance boost by using native code. But, with the obvious exception of FPS games, how many apps are CPU bound?

    Here's another variation: "I/O performance is directly proportional to buffer size". At another hardware vendor I worked for, one of our customers actually filed a bug because his I/O-bound application actually got slower when he used buffer sizes greater than 2 meg. It was not, of course, a coincidence that 2 meg was also the CPU cache size!

  • What about numerics (Score:3, Interesting)

    by Phronesis ( 175966 ) on Saturday May 17, 2003 @11:12PM (#5983695)
    The most common thing I believe about Java is that its performance is well below FORTRAN or even C for numerically intensive work, such as linear algebra on gigabyte complex matrices.

    I notice that while the article mentioned deals with a couple of nit-picky optimizations, it doesn't tell us anything useful about how to make Java rock on the numerics, which is the pace performance matters most to me. For instance, how would you write FFTW in Java?

  • by pyrrho ( 167252 ) on Saturday May 17, 2003 @11:17PM (#5983726) Journal
    >My biggest reason for liking JAVA is that it forces people to stop writing bad C code. Which is exactly what it was designed to do.

    this is what I dislike about it. I don't like languages built to force me away from a bad habit, it means I've been forced into (someone else's idea of) the perfect habit. I have yet to find the perfect habit.

    The real solution for bad C/C++ code (I slipped C++ in there on purpose) is to learn what you are doing. If you can't learn the ins and outs of procedural logic than don't create an imperitive language, you still have the the real pitfalls still present, and create a new paradigm altogether at a higher language, like Zope.

    There are good problems that VMs address, but they are niche areas (dynamically distributed code that can benefit by using heterogenous networds, and certain idioms like self modifying code... somehow I don't think the joy of self modifying code is what Java is about, but then, I'm told again and again how Java will be as fast as C++ when the JVM's dynamically recompile and optimize code as it runs... loluck), and not general purpose areas.
  • Re:Times change (Score:5, Interesting)

    by Orne ( 144925 ) on Saturday May 17, 2003 @11:23PM (#5983746) Homepage
    Totally agree. I remember when Java came out, I was still in college using my 486 DX2 66MHz, with all of 64 MB of RAM. That thing had its hands full with normal Windows 95, much less running Windows while browsing with Netscape to stumble across a web page with a Java applet.

    In those days, I hated Java and Macromedia Flash, because even then, they only used it to do the exact things a scripted mouseovers could reproduce. Those two programs accounted for most of the slowest web page loads...

    Now I have a P4 1.5 GHz with gobs of RAM running XP, and I have a hard time running enough tasks to slow it down. With a cable modem, I don't care about huge binary applets. I guess Java just needed some hardware upgrades for it to become useable...
  • by Anonymous Coward on Saturday May 17, 2003 @11:23PM (#5983753)
    Well put - exactly what I was thinking when I read this non-article. You summed it up much better than I could have. It seems this was not a disproof so much as it was a very poorly guised java advocacy article. Ironically though, the article actually makes more points about java's slowness than he supposedly refutes.
  • by Tony-A ( 29931 ) on Saturday May 17, 2003 @11:27PM (#5983763)
    I wonder to what extent this exists in other languages?

    Probably lots. Everywhere.
    As a crude approximation, 90% of the time is due to 10% of the code. Improving the "efficiency" of the 90% of the code that is responsible for only 10% of the time tends to be counter-productive. Of course there are no easy magic rules for how to improve the 10% of the code that is responsible for 90% of the time, or even identify exactly what that 10% really is.
    What does work is to have a sense of how long things should take and find and cure whatever is taking much longer than it should.
  • by Anonymous Coward on Saturday May 17, 2003 @11:33PM (#5983788)
    for example:

    public String indent(int indent)
    {
    String output = "";
    for (int x= 0; x indent; x++)
    {
    output += "\t";
    }
    return output;
    }

    If you use this function you are waisting cpu power like crazy because strings are immutable. This has nothing to do with the jvm, but is inherent in immutable strings, no matter what language they are in. Here's the reason... output starts as an empty string then the first "\t" is added to it. What the compiler MUST do is create a 3rd string of the length of both output and the "\t" string. Then it copies all of output in to the new string then the "\t". On the next loop output = "\t" and we add another "\t". To do this we make a third string again and start copying. BUT we already copied everything in output to a new string once already, so we're copying it for a second time. This is where we waste cpu cycles, and this is why not using immutable strings properly can cause problems. By the end of the function, the first "\t" is copied indent number of times, to indent number of temporary objects. If we change the function to..

    public String indent(int indent)
    {
    StringBuffer output = new StringBuffer();
    for (int x= 0; x indent; x++)
    {
    output.append("\t");
    }
    return output.toString();
    }

    No temp strings are created. No "\t"'s are copied. Thus we don't waste cpu cycles.
  • Re:Java is slow (Score:2, Interesting)

    by the eric conspiracy ( 20178 ) on Saturday May 17, 2003 @11:38PM (#5983803)
    even if you explicitly set references to null.

    Setting references to null in Java is not what I would expect from a professional programmer.

  • by Anonymous Coward on Saturday May 17, 2003 @11:39PM (#5983807)
    Where are the Java spreadsheets, browsers and word processors? They don't exist. Why is that? This is not a flame - if the Java language is as good as C++ then why are all critical feature-filled applications that you use every day written in C or C++?

    Don't moderate - post an example of a useful java desktop application.
  • Re:Antidote (Score:3, Interesting)

    by Daleks ( 226923 ) on Saturday May 17, 2003 @11:41PM (#5983814)
    1. All Objects are Allocated on the Heap

    This issue is debatable. The example the author gives is a bad one.

    What small objects? For me these are iterators. I use a lot of them in my designs. Someone else may use complex numbers. A 3D programmer may use a vector or a point class. People dealing with time series data will use a time class. Anybody using these will definitely hate trading a zero-time stack allocation for a constant-time heap allocation. Put that in a loop and that becomes O (n) vs. zero. Add another loop and you get O (n^2) vs. again, zero.

    Don't create a new object each time through the loop. Reuse the object to sidestep allocation/deallocation. In a tight-loop where performance matters this will help. In a situation where performance doesn't matter, then this doesn't matter at all.

    2. Lots of Casts

    Java 1.5 will have generics.

    3. Increased Memory Use

    Well let's look at the three points the author tries to make.

    3.1. Programs that utilize automatic garbage collection typically use about 50% more memory that programs that do manual memory management.

    What's a typical program? This one can be ignored.

    3.2. Many of the objects that would be allocated on stack in C++ will be allocated on the heap in Java.

    See above. This can be minimized.

    3.3 Java objects will be larger, due to all objects having a virtual table plus support for synchronization primitives.

    I'll admit ignorance on this issue, although my gut reaction is that hard facts need to be presented on the issue. As the original article said, Java has come a long way since 1.0.

    4. Lack of Control over Details

    This is a mixed bag. You could say that a language without pointers, and consequently direct memory access, will never be as powerful as a language without. But we know this isn't true. Functional languages are as powerful (from a programmatically expressive point of view, not computationally expressive), if not moreso considering the other features they offer: closures, anonymous functions, first-class functions, etc.

    5. No High-Level Optimizations

    The whole concept of template-metaprogramming is entirely orthogonal to the intended style of C++ programming. Meshing template-metaprogrammed code with regular code is a daunting task (on the large scale). It's a hack to get features that aren't in the language, such as a more computationally inclined macro system, lazy evaluation, etc. With that said, it is useful. However, I would rather see those features actually put into C++ and/or Java rather than having to resort to the abuse of C++'s generic programming facilities. A counter to this could be that templates allow for unbridled extention to C++, but that is most definitely not the case. Until C++ has a macro system that rivals Common Lisp's that assertion cannot be made.
  • by jasonditz ( 597385 ) on Saturday May 17, 2003 @11:59PM (#5983897) Homepage
    Corel actually had a Java version of Word Perfect Office.

    OpenOffice at the very least uses Java within it.

    Hotjava is a functional web browser.

    Java just isn't popular on the desktop, because you never know what crazy JVM version someone's going to have on their system. But there's definately a place for it.

  • by Anonymous Coward on Sunday May 18, 2003 @12:14AM (#5983945)
    HotJava is dead. I stopped by the website a couple weeks ago and Sun had a notice saying that HotJava was now in "end of life" cycle. Too bad. It would have been nice to have *one* browser that worked the same on *any* platform which supported Java.
  • by jasonditz ( 597385 ) on Sunday May 18, 2003 @12:23AM (#5983983) Homepage
    everything Corel does is a disaster, that doesn't mean it couldn't have worked :) Actually I have a buddy who still uses an old WP Java beta and swears it runs wonderfully now that PCs are fast enough to run it properly.
  • Re:Java is slow (Score:2, Interesting)

    by Curt Cox ( 199406 ) <curtcox@@@gmail...com> on Sunday May 18, 2003 @12:24AM (#5983986)
    Unintentionally retained objects can be quite hard to trace and understand in Java. Better tools are definitely needed. I feel yor pain.
    I can't tell the JVM when I'm done with an object, so I have to give it hints, or arrange it so my backlog of garbage doesn't get so big, or simply avoid allocation altogether.
    What do you mean hints? If there are no references to an object, it is eligible for GC. If there are references to an object, it can't be GC'd. What sort of hint do you mean? If you could dispose of an object explicitly, (and consequently without necessarily eliminating all references to it) the language wouldn't be garbage collected.

    When you say "I'm not complaining about the implementation so much as the architecture." are you suggesting GC'd languages make programming harder? If so, let me suggest that the real problem is that Java doesn't have adequate tools for detecting accidentally retained objects.

  • Re:Java is slow (Score:3, Interesting)

    by WolfWithoutAClause ( 162946 ) on Sunday May 18, 2003 @12:52AM (#5984110) Homepage
    This isn't a valid solution because sometimes your program might actually need that much memory.

    I said you can control the initial size of the memory. Many JVMs will normally not go outside that size unless it has already GCd and it still doesn't have enough memory.

    I should be able to tell the GC that I am done with an object right here and now, rather than waiting for the low priority GC thread to take too long to pick it up.

    Yes, that would certainly be useful; however, you can usually (depending on the VM) kick the GC off at sensible rates or tune the GC to run more often.

    Still, there are alternatives; and I generally still prefer using Java inspite of these kinds of relatively minor shortfalls in the language because it is still a more powerful language than C++ (powerful in the sense of my programs being shorter in Java than C++).

  • by pHDNgell ( 410691 ) on Sunday May 18, 2003 @01:02AM (#5984159)
    leave out a lot of the dubious stuff like operator overloading

    It's there in String, isn't it? They declared it good enough for themselves, but not good enough for us.

    Java's nothing new and exciting if you program in more languages than what the corporate world tells you is popular. They just had a marketing machine behind them.
  • by BitwizeGHC ( 145393 ) on Sunday May 18, 2003 @01:07AM (#5984175) Homepage
    The problem is, for Web applications, other languages (such as Perl, LISP, and perhaps Python) come readily to mind as comparable at least to Java. C++ and its like are verbose and confining in a domain (like Web apps) where agility is paramount. In C++'s case this is somewhat justified since like its ancestor C, C++ is only a step or two above assembly language, and thus can be used to produce VERY tight code. Java, running on a VM, has no such excuse.

    Java offers two main advantages: a beefy class library, and enough of the bondage-and-discipline nature to herd legions of mediocre programmers into typing a lot and doing a lot of important-looking work. Other than that it seems to combine the disadvantages of other languages, producing the old saw "Java: All the power of C++ with the blinding speed of Smalltalk".

    The thing about knowing many languages is that you can evaluate them and choose a better language, even than the conventional default. The idea that programmers should know many languages has been invoked far more often to justify the incumbence of bad languages rather than promote the adoption of good ones.
  • Re:Times change (Score:5, Interesting)

    by delmoi ( 26744 ) on Sunday May 18, 2003 @02:46AM (#5984419) Homepage
    If John Carmack was forced to program in Java, for example, Doom would only now be possible. And Doom III wouldn't be possible for many more years. Performance matters. Not always, but often.

    Actually, carmack considered java for Quake 3, but decided against it because he was worried about the quality of JVMs (something he couldn't control). Not because of their speed. He's said on many occasions that optimization in game code isn't even important anymore, since the vast majority of the work is done by the CPU is code inside the video card driver. He's said that for quake 3, even doubling the speed of the game code would only give a 10% improvement in framerate.
  • by morissm ( 22885 ) <morissmNO@SPAMlexum.umontreal.ca> on Sunday May 18, 2003 @02:53AM (#5984434) Homepage
    Hmmm... while I understand what the author is trying to say, I believe his article is misguiding. The problems he mentions are not urban legends and could conceivably be at the root of a performance bottleneck.

    What I think the author is trying to say is that "Premature optimization is the root of all evil in programming". Most of the stuff enumerated in the article usually has a minor impact on performance and no programmer should worry about them during coding.

    However, when all the coding is over, the system will have to meet some performance criteria. If it crawls like a quadraplegic snail, a programmer will have to get its hands dirty and tweak his code to remove the bottlenecks.

    It is very possible that one of those bottlenecks will be rooted in these so-called "urban legends". Gross over-allocation of immutable objects and synchronized methods may impact performance.

    It happened to me a while ago. I was working on a system that was designed to use lots of threads and message passing. We had completed the development and were ready to move on to testing. The system worked pretty well on the developers' workstations (1 CPU) but when we deployed it on our much more powerful servers, the throughput went down. At first, we thought that it was a thread contention problem but after some testing, we realized that the cost of obtaining a lock on multiprocessor systems is orders of magnitude higher than on uniprocessor systems.

    This is because on uniprocessor machines, thread synchronization simply amounts to doing an atomic if/set. However, on multiprocessor machines, complex mechanisms have to be used so that the lock becomes effective for both processors. It involves a lot more overhead because the required extra-cpu operations cost a lot of cycles.
  • by PizzaFace ( 593587 ) on Sunday May 18, 2003 @03:27AM (#5984485)
    I remember in my Turbo Pascal programming days (heh) that a lot of people said that using Units would degrade performance. So I tried it both ways and it never really made a difference, for my applications anyways.
    That's a good example of optimization tip that is "true" yet useless. Turbo Pascal used a 64K code segment for each unit, so function calls within the main program were "near" calls and function calls to another unit were "far" calls. Each far call made the code one byte bigger than each near call, and used an extra two bytes of stack space while the call was active. (Rubenking, Turbo Pascal 6.0 Techniques and Utilities.) The processor also had to save the CS register before a far call, then restore it afterward, but the performance difference would probably be measured in millionths of a second - possibly measurable in a big tight loop, but certainly not noticeable in a program that did any useful work.

    I don't put much stock in performance tips that are offered without explanation. And in deciding whether to use a tip, I weigh not only the performance trade-offs (near call vs. far call) but also the programming trade-offs (single source file vs. modular code). End-users want reliable functionality, and efficient programming practices often make more difference than code tweaks.
  • by vidnet ( 580068 ) on Sunday May 18, 2003 @05:19AM (#5984696) Homepage
    He's right!

    I tried the simple and stupid

    int fib(int i) {
    if(i<=2) return 1;
    else return fib(i-1)+fib(i-2);
    }

    without optimization on javac and gcc (the latter was slowed down by it so I figured it wouldn't be fair). Calculating up to 45 on my P3 800MHz took, according to 'time', 1m5.554s. Java used 0m51.807s (and that's including the jvm loading).

    Pretty neat.
    java -Xint (no JIT) is still running though.

  • by Anonymous Coward on Sunday May 18, 2003 @06:23AM (#5984811)
    MySQL ACID support is not commercial ready. To say it needs more battle testing and STILL select it for commercial use is quite stupid.

    I can easily corrupt MySQL with concurrent transactions in the same table with at least 2 indexes. Thanks but no thanks. MySQL, let me know when your transactions don't corrupt under real load.
  • It is a trade-off (Score:3, Interesting)

    by Trinition ( 114758 ) on Sunday May 18, 2003 @07:17AM (#5984886) Homepage
    The question of Java performancd can be quite subjective. For example, I run jEdit, a 100% Java text editor. It's fast. Unforunately, its not as fast as native Win32 editors like UltraEdit or TextPad. However, my mind and body can only work so fast. Both jEdit and the other text editors are faster than I need them to be for my day-to-day operations. So, by that measure, Java is fast.

    Of course, some people interpret the statement to be a comparison to C or C++. Now, Java has a lot of behaviors that are slower than C/C++.

    For example consider array access. Java implicitlu checks the bounds of an array whereas in C/C++, that is leftas an exercise for the programmer. Unfirtunately, most pogrammers are lazy and don't exercise that. Hence with C/C++ you have buffer overruns where nasty clients can execute arbitrary code. In Java,you'd have an ArrayIndexOutOfBoundsException which would prevent the malicious data form being pushed into memory. This, it was a trade-off between security and speed.

    Garbage collection is another one of these. Ever seen a C/C++ program with memory leaks (why, I even remember the X11 libraries leaking)? With Garbage collection, your memory consumption is slower and your memory freeing slower (since Java has to determine using an algorithm what isn't used anymore whereas in C/C++ its coded into the logic). Java also seems slower becaus ethat GC overhead is generally experiences as "pauses" whereas n C/C++ the object deletion occurs through the execution of the program. But this was a trade-off. A trade-off between making developers lives easier and the programs more stable versus the speed and risk of developer-coded memory deallocation.

    Java also has immutable Strings With a mutable String class, I know I could eliminate a lot of Object creation. But the String class was made immutable so everything could be final, and thus optimized better for. This was a trade-off between the speed of Strings themselves and the speed of creating a new String everytime you need to concatenate.

    There are many more cases, but I think you get the point. Java does things ways that are slower. But many of these are trad-offs -- trade-offs to make the programs more secure, development faster and syntax/API simpler. Then they go and address the speed in other ways by improving the VM (HotSpot, incremental/concurrent GC, etc.)

    In my opinion, I would've accepted a 100% Java version of Microsoft Outlook, even if it was slower, if I didn't have to worry about the nex buffer overrun exploit hijacking my computer.
  • bogus (Score:2, Interesting)

    by khuber ( 5664 ) on Sunday May 18, 2003 @07:39AM (#5984918)
    The author doesn't seem to understand what "urban legend" means. An urban legend is not the same as a myth.

    Goetz is in denial and just waves away problems using straw men without providing a truly balanced view of cases where these things cause problems. It depends on the VM, if things are done in a tight loop, and so on.

    Suffice it to say I did not like this article. As always, you need to measure application performance for yourself to find true bottlenecks.

    -Kevin

  • Endless confusion (Score:2, Interesting)

    by Anonymous Coward on Sunday May 18, 2003 @08:29AM (#5984995)
    Many Java programmers still don't understand synchronization and Java threading. Synchronized code is slower than unsynchronized; this is normal and there's no way around it. The problem is many programmers appear to take a "just in case" approach to synchronization. For most java developers, synchronization shouldn't be much of an issue (rarely if ever should you need to mark code synchronized). Synchronized code, used correctly, doesn't have an obvious performance penalty because generally it only needs to be synchronized because of a shared resource that should be much slower than the JRE. Working with Socket springs to mind. A thread-safe container is a much more efficient approach in other cases.

    The "final is faster" stuff is totally irrelevant, even if it is true for some cases, particularly static final methods. However, final is not Java's answer to c++ inline. Final is there just to say "do not override this." If the reason it's there is for "performance," it shouldn't be there.

    The immutable object thing is equally irrelevant. Strings are a particularly pleasant illustration, taking the argument about them to its logical conclusion leaves you with an array of char. If that's what you want to work with, what you probably want is a C compiler. You can look under the hood at StringBuffer and String and try to dope out what the compiler and runtime are doing. The better approach is to think about what you're doing, and make sure you're thinking in Java. Often if strings are actually the bottleneck it's because the coder wants their perl or c approach to a problem to work, not because garbage collection is more efficient one way or the other.

    In many ways, I wish primitives weren't exposed in the language. It would be a subtle hint to those who still think with pointers, arrays and free() in the back of their head "this isn't C" and reduce the stupid performance tricks people try. I also wish prior to 1.4 javac had issued a "this isn't perl" warning if you used more than a couple StringBuffers and StringTokenizers per class. Alas, with java.util.regex those who approach every problem with "I need a regular expression that will..." can wrap their bad habits in Java code, and 1.5 seems to be devolving to c++ with crappy pseudo-templates and precompiler-ish directives.
  • Re:Java is slow (Score:3, Interesting)

    by dubl-u ( 51156 ) * <2523987012&pota,to> on Sunday May 18, 2003 @12:35PM (#5985872)
    Telling the computer when I'm done with an object is a simpler solution (to me, anyway) than having to tweak runtime parameters.

    Simpler? Sure, if you have just a few objects. But with a lot of objects, it's much, much, much more complicated. As anybody who has spent hours running down a malloc/free error in somebody else's code can tell you.

    If you can't use new and delete or free and malloc correctly, then there's probably a lot of other things you can't do well either.

    Welcome to the human condition.

    I have a limited amount of attention and effort I can spend. The whole point of computers is to take the boring parts and have a machine do them, freeing me to think about the important, interesting parts. For the kind of stuff I code, memory allocation is in the boring category about 99.99% of the time. For the 1 time in 10,000 where it really matters, then fine, I'll write native code. But the rest of the time, let the machines do the donkey work.

    For example, I do not see how things would become much more dangerous in Java if you added a delete operator to complement the new operator.

    You can make the same argument about a lot of things in Java. E.g., multiple inheritance or platform-dependent code. The theory behind Java is that there are some features that a) take an expert to use properly, b) are dangerous when novices try to use them, and c) complicate things a lot when they exist. So Java doesn't allow those, or at least makes it hard enough to get to that only the experts bother (e.g., JNI).

    This sort of daddy-knows-best behavior is annoying, and absent business considerations, I wouldn't put up with it. But if I'm going to have to inherit the code bas of J. Random Programmer, I'd rather it be in Java, because although it's impossible to write really brilliant code, it just can't end up as bad as in C or Perl.
  • by 777333ddd ( 525062 ) on Sunday May 18, 2003 @01:52PM (#5986333)
    As a crude approximation, 90% of the time is due to 10% of the code.

    I think with respect to web programming, this is itself a myth. This rule of thumb seems to have reached the popular consciousness of developers in the 80s when desktop apps ruled. This was a time when each additional user adds a CPU. And it's true; in such a world, you don't worry about that other 90%. But when you have a fixed number CPUs shared by vastly more clients, you need to worry about more than just the 10% most offending code.

    In addition, I've found that programmers can be Soooo lazy that even the 90/10 isn't true in practice. I've seen the same expensive mistakes happen all over nearly every page of a web app.

    This is why so many intranet and internet applications seem slow. People put-off worrying about performance until the last step (just like they are told to). And then it might be too late.

    Developers get lulled into thinking everything's fine. Seems fast enough to them. But they are one user. Hundreds or thousands of real users will hit their app. If it's just OK for one, it's probably not OK for hundreds. Even if things seem lightning quick to you, they may not be for the hoard.

    In a lot of cases, performance can't be gained just by optimizing the little things here and there. In these cases, you often have to restructure how you approach things app-wide; you find yourself tweaking sections of almost every module. Or yanking out nice abstractions in favor of going bare metal. That takes even more time to do after the fact.

    My rule of thumb with web apps is actually to:

    1. Worry about performance throughout development.
    2. Time all page responses.
    3. Work on a slow machine.
    If you are developing a app, you will be irritated by a slow DB fetch, too many redundant fetches, or too much processing. YOU don't want to wait. YOU will be highly motivated to speed things up just for your own productivity. Budding performance issues will be detected early when structural changes are less expensive (if deemed needed). At the end of the project, you will be fixing bugs, not re-architecting for performance.

    dave

  • by awol ( 98751 ) on Sunday May 18, 2003 @10:07PM (#5988988) Journal
    Speed is all about your "functional" payload per unit time. Some one posted earlier that CPU time is cheaper than programmer time, which is pretty naive, becasue even if it is true the better a system (ie the longer its active life) then the higher the marginal cost of bad CPU utilisation, so it is not advisable to make that compromise if one can avoid it.

    For example, we recently "tuned" our transaction engine to the extent that it was truly CPU bound in a multiple tiered architecture with hundreds of remote clients using network comms to add transactions. Until this time, we had never been able to drive the engine at full utilisation of a single CPU (itis single threaded) because of various other inefficiencies in the system as a whole. Now that we have reached the point where the overall throughput is constrained by CPU, any enhancements we make to specifc algorithms in the system will go straight to the bottom line, of increasing our functional payload per unit time.

    What does this have to do with Java? Well, it is my belief that the vast majority of Java's problems are like the ones we used to have, ie not algorithmic weaknesses in the implemented software but structural impediemnts to fully loading the CPU, and so even if you try to optimise your code you can't gurarantee that the improvements will appear in the bottom line in terms of the applications performance.

    It is for this reason that I cannot use Java for my real world applications (oh, by the way we were 100% loading one of the processsors on a dual 750MHz SparcIII [Starfire I think], so it's not like we were trying to squeeze blood from a stone)

    It is the same reason why I cannot use functional/logic programming for the complex algorithms that we use for parts of the system, I need to control the execution path becasue I do not have the luxury of parallelising that path and so every action is on the critical path of the overall system performance. This is a fact lost on the FP, LP zealots (IMHO) [btw I like LP/FP FWIW]

  • Re:Java is Slow (Score:2, Interesting)

    by BenTels0 ( 667908 ) on Monday May 19, 2003 @02:19PM (#5992911)
    Want to prove it to yourself? Replace 'ls' some day with a Java version of it.

    Actually, I'd be willing to bet you considerable amounts of money (Euro's instead of dollars even, they're currently worth more) that the performance you are perceiving in that test is not Java pur sang but to a far greater extent the load time of the JVM (which, for a job as simple and involved code as limited as for 'ls', is undoubtedly quite significant). The JVM has a nasty tendency, you see, to preload lots of classes -- which, often, don't get used in small programs and so are deadweight. I was playing around with the JNI the other day under JDK1.4.1 on Linux and just wrote a simple "Hello World" -- a C program that embeds the JVM (pretty much what the actual "java" command does), loads a class (two, since the VM must load java.lang.Object in response to my loading my own class), creates an instance, calls a method, ends. Now, I can't claim a perceived performance of "instantaneous" like with a compiled C program, but certainly fast enough that you'd miss it if you blinked. Much faster than the "java" command, which loads a whole slew of stuff.

    Also, as another anekdotal hint towards preloading overhead, I was also playing around recently with java.math.BigInteger and wrote a program that calculates, for input n, the nth Fibonacci number. This is done recursively and it creates a new BigInteger object in each recursive call, plus two to start out with. I also put the "input-calculation" part in a loop, so I have a pretty good idea of performance minus startup overhead. And that, I can assure you, is preceived as instantaneous up until F-number 5500 and "fast" up until 17000-20000 (depends on the person). After that it slacks off until you reach 45065 (where I hit the maximum recursion depth) -- which I assure you will be "slow" pretty much everywhere. By comparison, Mathematica crawls when calculating number 50. And there's no perceived difference with Python, but my Python implementation doesn't do F-numbers beyond 998.

The moon is made of green cheese. -- John Heywood

Working...