tag:blogger.com,1999:blog-33042626.post4027411500260777721..comments2008-04-29T13:31:40.134-07:00Comments on Paul Buchheit: Java running faster than CPaul Buchheithttp://www.blogger.com/profile/08521809827597159995noreply@blogger.comBlogger48125tag:blogger.com,1999:blog-33042626.post-4196939604652108512008-04-03T06:26:00.000-07:002008-04-03T06:26:00.000-07:00Paul - I think java does use the -ffast-math optim...Paul - I think java does use the -ffast-math optimisation. It is used by default, and can be turned off using the 'strictfp' keyword.<BR/><BR/><BR/>www.ipmango.comAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-33042626.post-68161293878634962112007-11-14T16:33:00.000-08:002007-11-14T16:33:00.000-08:00quite java is not faster, cos in those machines th...quite java is not faster, cos in those machines the JVM optimizes the bycode, in gcc all depends of teh code and the instrucctions used, as show in the many commnent posted here.. is obvioslyMCKAY Brothershttps://www.blogger.com/profile/06614258331558376069noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-32063527982317985582007-08-12T04:18:00.000-07:002007-08-12T04:18:00.000-07:00whats the language java itself is written in?try t...whats the language java itself is written in?<BR/><BR/>try to understand this please: http://www.kano.net/javabench/<BR/><BR/>regardsthelittlebughttps://www.blogger.com/profile/08822178808121654570noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-15209153932272413462007-06-12T13:49:00.000-07:002007-06-12T13:49:00.000-07:00IO is cheaper in C than in Java because C's IO pro...IO is cheaper in C than in Java because C's IO provides neither thread-safety nor Unicode support. This is why Java's lead increased even further when IO was removed.Kevin Greerhttps://www.blogger.com/profile/12182965047434655482noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-52037356034379795902007-06-10T06:21:00.000-07:002007-06-10T06:21:00.000-07:00I think the sentenceObviously the results will be ...I think the sentence<BR/><BR/><I>Obviously the results will be different with different code and different machines, but it's clear that the JVM is getting quite fast.</I><BR/><BR/>says it all to me. I do not care about compiler flags or "switch-these-lines-and-I-am-faster" thingies. The thing I learned about Java and C today is that they are (regarding the performance) not that far from each as, say, 1999. You do not have to count in magnitiudes anymore, and that simply is good to know.Georgihttps://www.blogger.com/profile/14147510898520565363noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-41419649815381120612007-06-08T23:48:00.000-07:002007-06-08T23:48:00.000-07:00Paul Buchheit wrote "Update: ... I tried replacing...Paul Buchheit wrote <EM>"Update: ... I tried replacing the print calls..."</EM><BR/><BR/>Cool. Now try Matthew's loop suggestion, or just use <BR/><BR/>while(i <= MAX_ITERATIONS) {<BR/>...<BR/>if (zi2 + zr2 > BAILOUT) return i;<BR/>} <BR/><BR/>That improved both programs on my machine, be interesting to see what happens on your hardware.Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-4176656758187966152007-06-08T10:36:00.000-07:002007-06-08T10:36:00.000-07:00I should add specint2000 RATE, which measures thro...I should add specint2000 RATE, which measures throughput.Sanjeev Singhhttps://www.blogger.com/profile/05812698688245264209noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-69214791548061778102007-06-08T10:34:00.000-07:002007-06-08T10:34:00.000-07:00Andrew, moore's law has been a reasonable proxy fo...Andrew, moore's law has been a reasonable proxy for throughput thus far and throughput is more relevant to servers (where you would be using java or rhino-compiled javascript :). Look at Intel's best server chips from 2002q1 and 2007q1 (specint2000):<BR/><BR/><BR/>2002q1: Xeon 2.2Ghz (1core), 9.41<BR/>2007q1: Xeon 2.66Ghz (4core), 108<BR/><BR/>11x more.Sanjeev Singhhttps://www.blogger.com/profile/05812698688245264209noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-55689715618131833482007-06-08T08:45:00.000-07:002007-06-08T08:45:00.000-07:00Paul Buchheit wrote "... people seem to be taking ...Paul Buchheit wrote <I>"... people seem to be taking this post to be the "Ultimate C vs Java shootout". It's not."</I><BR/><BR/>One of the reasons search engines give so much weight to page titles, is that people give so much weight to titles.<BR/><BR/>When you title something <B>"Java running faster than C"</B> you really should expect people to take your meaning as - Java is faster (without qualification) than C.<BR/><BR/>Paul Buchheit wrote <I>"For the majority of applications, speed is no longer a valid excuse..."</I><BR/>For some definition of <I>majority of applications</I>, for some definition of <I>speed</I>...<BR/><BR/>I have a suspicion that neither of us know much about what the "majority of applications" do.Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-78368551213255095512007-06-08T08:31:00.000-07:002007-06-08T08:31:00.000-07:00Barry wrote "...and that's the point...I see that ...Barry wrote <I>"...and that's the point...</I><BR/>I see that I should have written - you're guessing wildly without any evidence whatsoever :-)<BR/><BR/>Barry wrote <I>"What you think the JVM is optimizing today may be completely changed...</I><BR/>Maybe it will, but I think your comments were intended to be about what the JVM is optimizing today.Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-51303085296086430622007-06-08T08:21:00.000-07:002007-06-08T08:21:00.000-07:00You're right I am guessing, and that's the point. ...You're right I am guessing, and that's the point. What you think the JVM is optimizing today may be completely changed in another version. So maybe in Java 7, even those for loops would be optimized out if you removed the trace statements. I'm actually surprised they're not optimized now.<BR/>It's clear the JVM is optimizing as the program runs. You can actually see the asterisks printing faster and faster as it runs. What's not clear is what exactly is being optimized. Is it the math calculations, or the printing, a little of both, or something completely different? Maybe it figures out the program is printing the same thing over and over and compiles the program to simply<BR/>print "*"<BR/>print "**" etc. And the math is no longer needed. WOW! That would be something, wouldn't it?Barry Andrewshttps://www.blogger.com/profile/02438861991966473878noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-53895553573268034572007-06-08T07:55:00.000-07:002007-06-08T07:55:00.000-07:00Barry wrote "My point is, it seems this example is...Barry wrote <I>"My point is, <B>it seems</B> this example is trying to..."</I><BR/>afaict neither Erik Wrenholt's page nor this page say that the comparison is purely math (and obviously the program does produce output).<BR/><BR/>Barry wrote <I>"...more than likely the JVM is going to optimize the run2 method completely out..."</I><BR/>I think you're guessing - and perhaps you'd agree that guessing won't take us very far in a this kind of discussion.<BR/><BR/>Java Elapsed 4.068<BR/>Java Elapsed 3.353 // no err.print<BR/>Java Elapsed 0.022 // no if(iterate <BR/><BR/><BR/>Barry wrote <I>"...extremely difficult ( if not impossible ) to write code that is equivalent in Java and C..."</I><BR/>There's an Ada front-end to GCC so Ada programmers make this point by saying - it's the same program if the compiler produces the same assembler.<BR/><BR/>imo the programs on Erik Wrenholt's page are wierd - it seems like there's been an attempt to write programs that are line-by-line the same even though they are in different languages, presumably to suggest that they are as equivalent as can be. <BR/>And of course that doesn't take into account that <I>seemingly similar</I> statements in different languages do different things.Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-74249971811419831682007-06-08T07:01:00.000-07:002007-06-08T07:01:00.000-07:00My point is, it seems this example is trying to co...My point is, it seems this example is trying to compare the speed of math calculations between 2 languages. To do that you need to take out ALL other factors; in this case printing to the console. You cannot simply comment out System.err.print in Java because more than likely the JVM is going to optimize the run2 method completely out since it doesn't do anything without the trace statements. This is an invalid test case for comparing math calculation speed. Try putting the timer around the math calculations themselves and see what happens. Another thing is that it's extremely difficult ( if not impossible ) to write code that is equivalent in Java and C unless you are a JVM guy and know the internals of how it performs optimizations at runtime.Barry Andrewshttps://www.blogger.com/profile/02438861991966473878noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-68250661245299177412007-06-07T17:25:00.000-07:002007-06-07T17:25:00.000-07:00Barry wrote "The bottleneck is in fputs. If you re...Barry wrote "The bottleneck is in fputs. If you replace these with cout the program runs 2X faster than Java!"<BR/><BR/>afaict commenting them out completely seemed to make the c program ~10% faster?<BR/><BR/>of course, you also changed the java program to use BufferedOutputStream and single byte output, didn't you?Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-26069955970556325072007-06-07T13:31:00.000-07:002007-06-07T13:31:00.000-07:00Well I think everyone is missing something here. I...Well I think everyone is missing something here. If you profile the C program, you will see that the bottleneck is not in the math calculations at all. The bottleneck is in fputs. If you replace these with cout the program runs 2X faster than Java! Of course, now you're using C++. ;) I think math calculations are still going to be much faster in C than in Java which is one reason C and C++ are still used the most in games.Barry Andrewshttps://www.blogger.com/profile/02438861991966473878noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-58989092917073321902007-06-07T11:45:00.000-07:002007-06-07T11:45:00.000-07:00"If CPU speed doubles every 18 months..." CPU spee..."If CPU speed doubles every 18 months..." CPU speed does not double every 18 months. I suspect you're thinking of Moore's Law, which says that <I>component density</I> doubles every 18-24 months (he modified the rule several times). The use of the Law as a proxy for speed is not correct. This is even more true in the multicore era.Andrew Binstockhttps://www.blogger.com/profile/16321156191558412680noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-62543799971741442502007-06-07T10:51:00.000-07:002007-06-07T10:51:00.000-07:00mea culpa it looks like -mfpmath=sse -msse2 makes ...<EM>mea culpa</EM> it looks like -mfpmath=sse -msse2 makes no noticeable difference on my machine.Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-12897946628001416282007-06-07T08:50:00.000-07:002007-06-07T08:50:00.000-07:00Following on from Matthew's replacement of the whi...Following on from Matthew's replacement of the while loop by a for loop .... <BR/><BR/>Simply using a while loop in an <STRONG>ordinary way</STRONG> maybe marginally faster than the for loop -<BR/><BR/>int mandelbrot(float x, float y)<BR/>{<BR/> float cr = y - 0.5;<BR/> float ci = x;<BR/> float zi = 0.0;<BR/> float zr = 0.0;<BR/> int i = 0;<BR/> while(i <= MAX_ITERATIONS) {<BR/> i++;<BR/> float temp = zr * zi;<BR/> float zr2 = zr * zr;<BR/> float zi2 = zi * zi;<BR/> zr = zr2 - zi2 + cr;<BR/> zi = temp + temp + ci;<BR/> if (zi2 + zr2 > BAILOUT) return i; <BR/> } <BR/> return 0; <BR/>}Isaac Gouyhttps://www.blogger.com/profile/00226627985460018169noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-28713843065847481612007-06-07T06:47:00.000-07:002007-06-07T06:47:00.000-07:00lborupj - Probably because the compiler can replac...lborupj - <I>Probably because the compiler can replace the 2 * temp with a shift left operation.</I><BR/><BR/>But 'temp' is a floating point number so bit shifting isn't going to work.Kevin Greerhttps://www.blogger.com/profile/12182965047434655482noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-34276795192119815212007-06-07T00:46:00.000-07:002007-06-07T00:46:00.000-07:00Just an observation, the statement "If CPU speed d...Just an observation, the statement "If CPU speed doubles every 18 months[...]" implies this might be true. It isn't. Far from it, as you might have noticed, this is not happening. Moore's law actually states that the number of transisters doubles every 18 months, not the speed. That is why we are now going multi-core, because they can't ramp the speed up very much.karadochttps://www.blogger.com/profile/16986742745952050273noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-54198491403528575452007-06-06T11:48:00.000-07:002007-06-06T11:48:00.000-07:00Back in 1997/1998, I wrote a spreadsheet applicati...Back in 1997/1998, I wrote a spreadsheet application (Integer, http://athena.com) and was able to match Excel's numeric performance on a single CPU box and scale linearly up to 16x on SMP boxes.<BR/><BR/>The application was more than just a "snippet" of code, but was a full-on application that did "lots of stuff."<BR/><BR/>Complex Java code can be as fast or faster than C code. Except for hairy stuff that's better shuttled off to a special purpose processor (e.g., GPU), it's easier to write Java code that runs faster than C code in the "whole application" context.David Pollakhttps://www.blogger.com/profile/16630520857988769066noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-22877496251852629412007-06-06T11:10:00.000-07:002007-06-06T11:10:00.000-07:00When running the java code on my Dell laptop, if I...<I><BR/>When running the java code on my Dell laptop, if I change:<BR/><BR/>zi = temp + temp + ci;<BR/><BR/>to<BR/>zi = 2 * temp + ci;<BR/><BR/>then I get slightly better results.<BR/><BR/>I wonder why this is?<BR/></I><BR/><BR/>Probably because the compiler can replace the 2 * temp with a shift left operationlborupjhttps://www.blogger.com/profile/02921555657283185092noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-27409764180935931942007-06-06T08:13:00.000-07:002007-06-06T08:13:00.000-07:00When running the java code on my Dell laptop, if I...When running the java code on my Dell laptop, if I change:<BR/><BR/>zi = temp + temp + ci;<BR/><BR/>to <BR/> zi = 2 * temp + ci;<BR/><BR/>then I get slightly better results.<BR/><BR/>I wonder why this is?Kevin Greerhttps://www.blogger.com/profile/12182965047434655482noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-56862679858219608812007-06-06T08:04:00.000-07:002007-06-06T08:04:00.000-07:00Did the GCC manual also tell you there is no -O8 o...Did the GCC manual also tell you there is no -O8 or -O9 optimization?<BR/><BR/>-Os, -O2 and -O3 are the only valid -O optimizations recognized by GCC.Anonymoushttps://www.blogger.com/profile/14180694986659591222noreply@blogger.comtag:blogger.com,1999:blog-33042626.post-79835398384663266722007-06-06T06:44:00.000-07:002007-06-06T06:44:00.000-07:00Paul - I think java does use the -ffast-math optim...Paul - I think java <I>does</I> use the -ffast-math optimisation. It is used by default, and can be turned off using the 'strictfp' keyword.Mr Investorhttps://www.blogger.com/profile/15534847556067924236noreply@blogger.com