Sponsored by HowLifeWorks
How Private Online Shopping Clubs Work view!
howlifeworks.com - How to become a member and get discounts of up to 80% on must-have luxury goods
82 Comments
- bytesmythe, on 10/12/2007, -0/+6@yann
Amen. That's terrible Ruby code! I would expect it to be much cleaner and a lot shorter than the Java version. (My favorite thing to do in Advanced Operating Systems was turn in projects written in Ruby. The rest of the class was using C, so their code was pages long, while mine was around 15 lines.)
To those of you not familiar with Ruby, don't look to this for any kind of decent comparison. It really does look like the author almost used search-and-replace to create the code. This approach destroys the elegance of coding in Ruby. - illynova, on 10/12/2007, -1/+6Java is only fast when it comes to a niche market: pure number crunching.
Ummm, actually that would be fortran. - y2048, on 10/12/2007, -0/+4Didn't read the article, skipped straight to the code and all I can say is Wow! - comparing the same method in Ruby and Java:
Ruby:
21: def rb_insert_left(val,sw)
22: if (@left == nil) then
23: @left = RedBlackTree.new(val)
24: else
25: @left.rb_insert(val,sw)
26: end
27: end
and Java:
33: private RedBlackTree RBinsertLeft(T k,int sw) {
34: RedBlackTree l;
35: RedBlackTree b;
36:
37: l = __left;
38: if (l == null) {
39: __left = b = new RedBlackTree(k);
40: }
41: else {
42: b = l.RBinsert(k,sw);
43: }
44: return b;
45: }
13 lines of Java to do the same as 7 lines of Ruby? Java must SUCK, right?
ehm, not so fast - the Java code seems to be purposely padded and made to look more complex than necessary; it also uses a naming convention (method starting with upper-case char) that no self-respecting Java developer would ever use.
actual code created by a "real" Java developer would look more like
33: private RedBlackTree rbInsertLeft(T val,int sw) {
34: if (_left == null) {
35: _left = new RedBlackTree(val);
36: return _left;
37: } else
38: return val.rbInsert(val, sw);
39: }
same amount of lines, remarkably similar code to the Ruby version, only type-checked at compile time instead of runtime.
with such bias in the code, I don't expect much truth from the article... I think I'll skip it. - arghnoname, on 10/12/2007, -0/+4There's nothing wrong with comparing speed differences. It's up to the programmer to decide if that's a priority to him or her, or if he or she would rather save time in development and maintenance.
- Narrator, on 10/12/2007, -0/+4The big win for Java is that the development environment, tools ,etc are able to statically analyze the code much more comprehensively than dynamic language development environments. This allows them to do all kinds of interesting refactoring, auto complete, and code analysis that dynamic languages don't support.
Thus, the comparisons in the article mostly apply to developers who code in vi. I bet if you counted up total keystrokes in IntelliJ or Eclipse for coding the above program it would be far less in Java than in Ruby or Python using whatever is the most advanced development environment for these languages. The benefits for larger projects are even greater. - mercnboy3, on 10/12/2007, -1/+4It says java has the most lines of code, but it is plain as day that c++ has more...
- Supafly, on 11/09/2007, -0/+3Where is C# in this list? Why is C++ being compared to managed/interpreted languages?
- blitzman, on 10/12/2007, -0/+3Then there is the D programming language, www.digitalmars.com/d/, which is C++ reengineered to add automatic memory management, builtin strings, etc. It is possible to get the performance of C++ without the productivity-sapping problems.
- cplusplus, on 10/12/2007, -0/+3At the end of the the C++ code he says:
// no garbage collection
343: delete v;
344: delete root;
Those two lines could be removed if he avoided the new's at the top of main()
ie change
RedBlackTree *root = new RedBlackTree(2);
into:
RedBlackTree(2) root; - noseeme, on 10/12/2007, -1/+3Makes me want to learn python. :)
- eventualbuddha, on 10/12/2007, -0/+2Here's my Ruby rewrite. It's still not totally Ruby-ified, but it's closer and it makes it significantly shorter: http://brian.maybeyoureinsane.net/blog/wp-content/redblack.txt
- ahoyhere, on 10/12/2007, -0/+2This guy's Ruby code is pathetic, which as far as I'm concerned nullifies every single thing he's written. Which is too bad.
eventualbuddha's Rubyfication (see comments above) is much better, but still you can go further yet to eliminate code cruft and take advantage of what makes Ruby really great. Especially see the repetitive code like the two methods rb_insert_left and rb_insert_right, and rot_left and rot_right. These two functions are identical except for one tiny part. Ridiculous! What a waste. Ruby allows you to define methods in a class programmatically, such as (I don't have time to test this and rewrite the whole thing, but this should give you an idea):
['left','right'].each do |dir|
define_method("stuff_#{dir}") do |arg1, arg2|
if(self.send(dir)) # this will access @right or @left
# do stuff here
else
# do other stuff here
end
end
end
This means you don't have to have 30 lines consistening of two functions with one word different. - james.britt, on 10/12/2007, -0/+1"Ruby blocks/lambda/yield seemed more or less equivalent (to me) to Python's named class or function. Didn't seem a big win to be able to write an anonymous function inline."
This so misses the mark it isn't funny. But the bottom line is that really understanding a language (beyond the syntax/feature-list level) requires spending a fair amount of time with it, and don't think that was the case here. At least the comments don't seem to indicate a profound understanding of Ruby. - clayasaurus, on 10/12/2007, -0/+1C++ vs Java vs D
1. Garbage collection is THE big win for Java and D.
2. Simplicity over C++ complexity is a big win for Java and D.
3. C++ is much harder to write and get right than any of the other choices
4. C/C++/D is way faster than Java
5. Language scaffolding requirements are similar for all
6. C/C++/D is the only way to go for low level systems programming.
D scores for the win! - hanshasuro, on 10/12/2007, -0/+1I'm no expert in Ruby, but it seems like he wrote all of his classes in a very "C-esque" style, which Ruby certainly provides for. However, based on my limited experience, the real power and conciseness of the language is not present if you write Ruby code while thinking like a C/Java programmer. You actually need to change the way you program a little bit to get the full power of a language like Ruby.
Maybe someone with more experience can comment on his Ruby style? - Ryosen, on 10/12/2007, -0/+1It says that Java has the most lines of code, but they're style is a bit verbose. They cite that the reason Java has more is because of the end braces. While I, too, prefer to put end braces on their own line, you could just as easily put them on a previous line. Hell, you can have the entire program on one line. And, since you can do this with pretty much any language, lines of code tends to be a pretty silly way of comparing languages.
A much better approach would be the complexity of the code, as well as the performance of the compiler/virtual machine. Let's face it, certain versions of COBOL will let me run a quicksort algorithm using a single line, but doesn't make it superior to C, which might require 40 lines and runs a lot faster. It just makes it easier. But convenience comes at a price.
Still, it's a nice article that helps to visualize some of the differences between the various languages. - kirakun, on 10/12/2007, -1/+2@narrator, if you're talking about development tools, C++ has many more powerful tools than java.
- Osmanthus, on 10/12/2007, -2/+3The author's complain that C++ is hard; this is probably because they don't know what the hell they're doing. A simple recursive function has been turned into an object nightmare using STL conventions. Why did they do that? Maybe its because they had an agenda to meet to prove that C++ is the devil, but more likely its because they are bad engineers who don't know how to Keep It Simple Stupid.
- listrophy, on 10/12/2007, -0/+1@dmh2000:
multi-line comments in python look like:
def some_function(self, x):
"""
This is
a
multi-line comment """
do_something(x) - garyl2k, on 11/27/2007, -0/+1http://www.freegifts4you.co.uk
- TokenUser, on 10/12/2007, -1/+2The performance of Java will only ever be as good as the JVM upon which it is running.
Once C is compiled into an exe is is as fast as it will ever be on that platform. On the other hand new Java JVMs are released regularly, and when they are running on new CPUs, performance IS slower than an exe, but not significantly ESPECIALLY if it is used for operations where there are other resource bottlenecks such as databases. Best solution is to profile your code, and build a hybrid - build a library of modules in C/C++ and call them from the Java code.
I still have a hard time taking Ruby or Python seriously ... but that is just the language bigot in me :) - freeman755, on 10/12/2007, -0/+1After I saw that Wikipedia page a few months ago, I thought it would be cool to have a more in depth comparison between several of the languages, so I made this web page: http://programming.thecomputerworld.net/ .
On the webpage there's a side-by-side comparison of the following programs in C, Java, Perl, Python, PHP, and Javascript: For loops, Input, Functions, Random number generation, How to find Fibonacci numbers, How to find prime numbers, how to read/write/append to files, how to make a basic number guessing game, how to modify arrays, and how edit strings (convert to upper/lower case, find/replace, reverse, rotate, etc). I did it mainly to learn how to do the stuff in those languages, so some of my code is probably newbie-ish. - warpdude, on 10/12/2007, -0/+1I'm not so sure about this comparison, especially with the Ruby code. It doesn't seem like the author took advantage of the strongest features of Ruby (e.g. blocks and iterators) at all. Even from a cursory glance of the code, it seems that he could've saved nearly _twenty_ lines on the Ruby side just by using the access control functions that're available to Ruby (attr_accessor), instead of defining set/get methods for each instance variable.
- altjeringa, on 10/12/2007, -0/+1I'll buy that bridge. For the aspects he examined I think he summarized things pretty nicely. The big differences will appear when one actually chooses an application domain; CLI tools vs Desktop Apps vs Web Applications.
- lukes, on 10/12/2007, -0/+1"I'm no expert in Ruby, but it seems like he wrote all of his classes in a very "C-esque" style, which Ruby certainly provides for. However, based on my limited experience, the real power and conciseness of the language is not present if you write Ruby code while thinking like a C/Java programmer. You actually need to change the way you program a little bit to get the full power of a language like Ruby."
Yes I was about to post about this when I saw your comment. He has literally transcribed the code of the C++ program into the Ruby language, without actually managing to write Ruby code :). I don't know Python but I assume it to be similar. I would like to see a Digg where someone who knows Python and Ruby a lot better re-write the code on that page in a style that is more relevent for comparison. Someone? - ogami, on 10/12/2007, -0/+1@doctorshim
You said you wanted a comparison of C++ and Lisp, here's one better, a NASA paper comparing C/C++, Java, and Lisp/Scheme. (It's in PDF though...) www-aig.jpl.nasa.gov/public/home/gat/lisp-study.html - veracon, on 10/12/2007, -0/+1Very interesting article. I personally like Python the best, but I can see that there are many similarities to Ruby.
- peanutz, on 10/12/2007, -0/+0Just went over Ruby once and saw the first few lines of code in the article. Can't
RED = 0
BLACK = 1
be written as
RED, BLACK = 0,1 - quetivity, on 10/10/2007, -0/+0The combination of JavaScript and XML is just the best solution for me, everything else I really do not use.
- 808kick, on 10/12/2007, -0/+0Right tool for the right job. There is no language that works well for every task, I could personally use 3 or more different languages for any project utilizing what I like about each of them.
- Crazen, on 10/12/2007, -1/+1Lack of strong typing for anything serious is a joke. Java can be, and has show cases where, it is faster than C/C++ due to dynamic optimization during runtime where the JVM has time to understnad what needs to be optimized and how to do it. Static optimization won't accomplish that. Using a weakly typed language for anything bigger than your personal homepage or project either means you are running a prototype, have a bunch of monkeys to throw at spaghetti code, or you don't have any tool other than a hammer. The fact is that there are two classes of languages. C++, java, C# vs. crappy scripting languages that are great for programs that don't exceed a screen of code.
- drunkJerkface, on 10/12/2007, -0/+0The author should stick by his title. Why the hell does he compare Java to C?
- thefrenzy, on 10/12/2007, -3/+3The only way the average C++ program is faster than a Java app running on a suitably modern JIT JVM is if it is doing some serious number-crunching with heavy vectorization. For anything else the speed difference is negligible, plus, the safety and ease of programming with threads far outweigh that advantage.
- vicaya, on 10/12/2007, -1/+1Application writers and library writers are different beasts. Application writers tend to prefer ease of use and expressiveness (python, ruby, perl), while true library writers prefer performance and flexibility. Hard core library writers tend to like C++ more because it provides great abstraction without (or with minimum) performance penalty. Check out boost.org!
And then, there are language purists, who noticed that all the languages mentioned above are weakly typed languages (dynamic or static.) that offer little in type safety. Sigh!
Hopefully we'll see Scala (wishing it'd replace Java), OCaml, Haskell, Clean... on Digg sometime. - ptolomy, on 10/12/2007, -0/+0This probably goes for all the languages, but for my ruby version I got rid of "copy()", because
rot_left and rot_right can handle it themselves, as below.
Makes the code tighter, in my opinion.
def rot_left
new_left = self.dup
new_left.right = @right.left
@left = new_left
@val, @color = @right.val, @right.color
@right = @right.right
end - tempusrob, on 10/12/2007, -0/+0Just skimmed it, but it looks like a great article. He doesn't draw too many hard-lined conclusions, which is all too often the case with other language comparisons.
- sanogopapa, on 07/31/2008, -0/+0In a similar way, I was able to pass around pointers to any data.
http://seoweb.info - wulanshout, on 11/08/2008, -0/+0http://www.wulanshout.com/seo/busby-seo-test-seo-c ...
Busby SEO Test has been released! The next Busby Web Solutions Search Engine Optimisation Challenge, start on October 1st, and Finish on January 31st,2009. Get join and wind $ 5000 grand prize - quicksort, on 10/12/2007, -0/+0 54: RedBlackTree(RedBlackTree *b) {
55: m_val = b->m_val;
56: m_left = b->m_left;
57: m_right = b->m_right;
58: m_color = red;
59: }
I stopped reading the article (and registered just to post this, I could not take solace in just lurking and let this happen, I was outraged when I saw this kind of stuff by garbage collectors fanboys) when I saw that, thats a very “nice” C++ code by a java "engineer" wannabe
, in line 54 the default constructor for all the 4 data members is called nice isnnt it?, it would not
be much of a problem for the pointers and the integer, but for line 55 depending of type T you got
a very nice quickback!!!
But i smell this digg site is a java/vb.net "engineer" fest, 50+ comments and nobody could spot this tremendous error, sad.
The proper C++ code is this (it could be enhanced by adding const correctness btw)
54: RedBlackTree(RedBlackTree *b) m_color(red),m_val( b->m_val),m_left(b->m_left), m_right(b->m_right)
55: {}
Excuse the grammar - mourner, on 10/12/2007, -0/+0Oh man, the python source is so ugly! It's way too much over-"underscored".
- kingpin77, on 12/16/2007, -0/+0you can't really make comparisons on speed... its not the main issue, each is good for different things!
http://www.kingpin-seo.co.uk - mpeters, on 11/29/2007, -0/+0yes...
http://www.articlethat.com - Anekdoten, on 10/12/2007, -0/+0It's a nice article but he should gave compared apples to apples by using C# as the fourth language and not C++.
- quicksort, on 10/12/2007, -0/+0Oh my God Gross!!!!
226: // construct with an initial value
227: RedBlackTree(T x) {
228: m_val = x;
229: m_left = 0;
230: m_right = 0;
231: m_color = red;
232: }
1) [ 227]pass by value of T::x, thus one call to copy constructor of T
2) [227] default initialization for all data members, could be very expensive with T::m_val, another cute call to a constructor of T
3) [228]one assignment operator call of T:operator=, again it could be as expensive as you want for a given type T
Total Score: wrong order of construction, 2 calls to T construcors
, 1 call to T::operator=
227: RedBlackTree(const T & x) m_color(red),m_val( b->m_val),m_left(0), m_right(0)
228: {}
Totals: 1 call to the copy constructor of T
I just don’t belive this->engineer coded any time C++ with Meyers book by her side (cute thing to throw to look like one in the know), this girl just never coded any thing besides a port of K&R "Hellow World" in C++
excuse the grammar - drbrain, on 10/12/2007, -0/+0I mean, here:
http://segment7.net/temp/RedBlackTree.rubyist.rb - ilovecelebs, on 11/28/2007, -0/+0Quite interesting.
http://www.carinsurancequoteportal.info
http://www.healthinsurancequoteportal.info - drbrain, on 10/12/2007, -0/+0RedBlackTree.rb, as a Rubyist would write it.
Weighs in at 187 lines, including the author's original whitespace, making it shorter than his Python version. The big win came from use of attr_* instead of spelled-out accessors.
As a pragmatist I didn't bother with dynamic method creation because its too clever and adds no clarity, especially if I have to come back and look at this code in another two weeks. I used Symbols instead of meaningless constant integers and made the public API ruby-like with [] and each for use with Enumerable.
Use of shortcut if is rampant as it should be.
The point of using a particular language should be to clearly, cleanly express what you are trying to do. Ruby does this very well for me, but Smalltalk beats it hands-down. - pavel989, on 02/07/2008, -0/+0Hi everyone. I've gotten really into programming because I wanted to make games. i start with C++ thought it was way to hard and now think its kind of ugly. from there i looked at java, and didnt understand a single thing. i started with them because apperently they dominate the game industry. But i disliked java for the same reason as C++. i then went onto VB and actually kind of liked it. but dont remember it at all.
before i go on, id like to say that by no means am i even experienced in any language. i jump around from langauge to language as soon as i complete a progam, looking for the prettiest and most powerfull language.
after a while, my computer crashed and i went onto linux were C derivatives and python rule. but i looked into ruby. and i thought it was absolutly awesome. i still do. unfourtently it lacks a true compiler, its so easy to get your thoughts out onto a computer screen.
next i tried python, and liked it too, at first i hated the concept of tabs meaning something but thought that it allowed for the best code understanding. (it can be done in any language, (methinks) but here its forced)
and then I was wondering how igot into this to begin with and realized, i wanted to make games.
so it comes down to this: (according to this digg) ruby/python are slower than java. which i cannot believe. Java lags so much. ive tried eve. its writtin in python. ive tried runescape. runescape. quite 3d and a lot of math, can have horrible lags. EVE, a crazy 3d space world, is on the fly. I cant believe that its slower. C++ i can argue is probably the fastest out of the languages compared. but its so like, ew. And ive heard that u can use ruby to make games.
well u can do that with anything. But really would writing a big game in ruby make the game slow? from what i understand, ruby has kickass g/c. And i hear consistent argument but no one ever explaines. so ima ask here.
what is the best game devel language, and why. not a comparison, and nothing too obvious. we knwo cpp is fast. we know java can be played on almost any platform. but so can ruby and python. so why dont companies use them? - ptolomy, on 10/12/2007, -0/+0For the sake of amusement, I re-implemented this in Haskell. The result is (minus comments, but including type annotations) about 45 lines. I would wager that it probably performs quite well, also.
Of course, Haskell is almost cheating, because doing tree-based stuff is where functional languages of that family tend to excel.
I also re-worked eventualbuddha's ruby code, increasing readability, upping the idiomatic-ness (?), and reducing the code by another couple dozen lines.
In summary, I really don't think this examination really shows the strengths or weaknesses of any of the languages so much as it shows the inexperience of the dude who made them. And, of course, without knowing the scope of use of the eventual code, this kind of thing is useless.
It is fun, though. Inspired me to waste an hour or two. :-) -
Show 51 - 85 of 85 discussions



What is Digg?