Discover the best of the web!
Learn more about Digg by taking the tour.
Ruby/Python/C++/Java Side By Side Code Comparison
dmh2000.com — An essay comparing some aspects of the 4 languages but possibly more interesting is the page that lets you make a side by side comparison of some real code of a red-black tree algorithm done in each language. Follow the link to the comparison page.
- 1113 diggs
- digg it
- 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.
- 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...
- noseeme, on 10/12/2007, -1/+3Makes me want to learn python. :)
- bryan986, on 10/12/2007, -2/+3Ok, for one, you can't compare the speed of Java/C++/Python. C++ is compiled, Java is a hybrid compiled/interpreted, and Python is interpreted.
PLEASE! Stop making speed comparisons of the languages. There are indeed speed differences, but the they each carry their advantages and disadvantages!! - battybattybatt, on 10/12/2007, -5/+0"Ok, for one, you can't compare the speed of Java/C++/Python. C++ is compiled, Java is a hybrid compiled/interpreted, and Python is interpreted.
PLEASE! Stop making speed comparisons of the languages. There are indeed speed differences, but the they each carry their advantages and disadvantages!!
posted by bryan986 (0) at 12:39 "
Yeah, yeah, yeah! - saichele, on 10/12/2007, -1/+1I'm a big Python fan, having programmed in C++ and Java as well. Ruby looks pretty great though - at least from the example code.
- 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 :) - arghnoname, on 10/12/2007, -0/+3There'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.
- Osmanthus, on 10/12/2007, -1/+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.
- 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.
- LordVoldemort, on 10/12/2007, -3/+1Objective-C has only about as much type checking as C and the dynamism can be as much as that in Python (with the right tricks).
For instance... I was able to transfer primitive ints around as Objects in an NSArray (which only contains NSObject types) by treating the ints as pointer values. A Hack..... But it worked!
In a similar way, I was able to pass around pointers to any data.
In essence, a design like Objective-C gives a Programmer room to be clever, and if you are so worried about runtime errors, then you are not that clever to begin with. - LordVoldemort, on 10/12/2007, -5/+0Java is only fast when it comes to a niche market: pure number crunching.
- 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. - 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) - oltompic, on 10/12/2007, -2/+2Ruby ftw IMO
- motionblur, on 10/12/2007, -3/+2Pointers can kiss my ass.
- 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. - Bogtha, on 10/12/2007, -2/+1> Ok, for one, you can't compare the speed of Java/C++/Python. C++ is compiled, Java is a hybrid compiled/interpreted, and Python is interpreted.
Python is compiled to bytecode the first time you run it. You can even compile it to Java bytecode if you want. - qishi, on 10/12/2007, -2/+2Simple: You pick the best tool for the job. Language bigots are retards. I used to be like that. Then I realized nobody gave a crap if I knew the nuances of C++ memory management. I realized how stupid it was, and that languages like Python and Ruby have their places. Also found under the category of retards is whoever wrote this article (who has written a LOG of C++). That paragraph where he orally pleasures Linus is pretty horrible.
I haven't used Ruby in awhile, but for me it lost out to Python a few years back because it was extremely hard to embed. Not sure if that has changed. - dognose, on 10/12/2007, -0/+0How is 284 > 346? It says Java has the most lines, when the C++ program is much longer.
- claco, on 10/12/2007, -2/+1Ruby, Python, AND C PLUS PLUS? And still no Perl. Bunch of tools...
- kirakun, on 10/12/2007, -2/+0Why isn't Perl in the comparison? It's still the most used scripting language isn't it?
- kirakun, on 10/12/2007, -1/+2@narrator, if you're talking about development tools, C++ has many more powerful tools than java.
- 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? - 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.
- lasermike026, on 10/12/2007, -2/+0Yup, where is my Perl?!?
- 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.
- 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; - mike_p, on 10/12/2007, -1/+0I don't really know much about Ruby (even with the vast ammount of stories on Digg)... I think i may take a closer look at both Ruby and Python but... I think it's Python FTW
- ph713, on 10/12/2007, -2/+0I declare perl the winner. In perl, you don't even have to know how to implement a RedBlack tree. It's one of the many thousands upon thousands of idiomatic algorithms and patterns that come for free with perl in the form of CPAN. vis, the example from the Tree::RedBlack documentation:
use Tree::RedBlack;
my $t = new Tree::RedBlack;
$t->insert(3, 'cat');
$t->insert(4, 'dog');
my $v = $t->find(4);
my $min = $t->min;
my $max = $t->max;
$t->delete(3);
$t->print; - 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.
- 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. - ColdDimSum, on 10/12/2007, -0/+0Don't forget TCL/[incr TCL]. I've programmed in over a dozen different languages and TCL is, by far, my favorite language (although it wasn't always so, I hated TCL at first). I've also worked extensively in Perl in the past and it's nice for some things, especially if it has application-domain libraries you can use.
Actually, I would say that the key to a highly successful (on-time, on-budget, high-function, low-defect rate) project is working in a language/environment where there is a lot of support for your particular problem-space.
You don't really know a language until you have delivered and maintained 100k+ lines of code in it. - JimNtexas, on 10/12/2007, -0/+0Which one is better for client side applications that need guis?
- 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? - 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. - mfawcett, on 10/12/2007, -0/+0[quote] Posted by ph713
I declare perl the winner. In perl, you don't even have to know how to implement a RedBlack tree. It's one of the many thousands upon thousands of idiomatic algorithms and patterns that come for free with perl in the form of CPAN. vis, the example from the Tree::RedBlack documentation:
[/quote]
And you could say the same for C++. He could've just done
#include
std::map tree;
and been done with it, but I think the object (which IMO he failed at) was to show how to implement a red black tree in each language. I'm sure the other languages have similar standard library facilities available to them. - mfawcett, on 10/12/2007, -0/+0First time poster, sorry. My template code got erased as an HTML tag probably. That should read
std::map < int > tree;
how do you post less than/greater than signs correctly? - Supafly, on 11/09/2007, -0/+2Where is C# in this list? Why is C++ being compared to managed/interpreted languages?
- 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. - freeman755, on 10/12/2007, -0/+0Ah if I could i'd move my previous comment to the other recent digg article about programming. Somehow I mixed up the tabs when I was making that comment.
After reading /this/ nice article, I still like C/C++ over Java, but Perl is definately my favorite language - How many other languages can make a 3-line sudoku solver? - 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
- 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.
- rtfx, on 10/12/2007, -0/+0The Python code isn't bad. But it is perhaps overstructured, for Python. A lot of methods could be entirely eliminated by using more list comprehensions, e.g. for left/right branching. (God, I sound like a Lisp user. But it works in Python too!)
- ravenmuffin, on 10/12/2007, -0/+0LordVoldemort> For instance... I was able to transfer primitive ints around as Objects in an NSArray (which only contains NSObject types) by treating the ints as pointer values. A Hack..... But it worked!
Does Digg have personal messaging?
You'd be better to simply turn the ints into NSNumber objects to add to your array, eg,
int x = 123;
int y = 456;
NSArray * myArray = [NSArray arrayWithObjects: [NSNumber numberWithInt:x], [NSNumber numberWithInt:y], nil];
x = [[myArray objectAtIndex:0] intValue]; // 123
y = [[myArray objectAtIndex:1] intValue]; // 456 - vann, on 10/12/2007, -0/+0Wow, typically the digg comments are overly negative and needlessly harsh, but not here. The code on that page is truly stupid. You can really tell that this guy just did some kind regex replace to generate the Python and Ruby code.
There's more to a language than just the synax. The Ruby, in particular, makes me want to cry. No digg. - harrisbradley, on 10/12/2007, -3/+0No previous dev/eng will take Ruby serious. It's too hard to stare years or reinventing the wheel in the face.
- DoctorShim, on 10/12/2007, -0/+0I'd love to see a C++ Lisp code comparison.
- 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. - Teratogen, on 10/12/2007, -3/+0PERL UBER ALLES
-
Show 51 - 81 of 81 discussions

Digg is coming to a city (and computer) near you! Check out all the details on our