| Python vs Ruby? |
|
I was trying to run a little "benchmark" today, so to speak, to give me a better idea as to which language I should choose for a project I'm planning on starting soon. I'm going to have to iterate over large arrays for this project, so I decided to give it a go at seeing the difference in performance when it came to inserting numerous objects into an array, and then transferring those objects one-by-one to a string. Here's the code I used: Pythontestlist = [] Rubytestlist = [] Now, the two seem very similar in program flow (at least, to me). So I ran the Python program. This is what I got: real 0m1.679s Ok, so it played with 1 million objects (well... technically more than double that) in under 2 seconds. Sounds good so far! Now, when I ran the Ruby program, this is what I got: *snoring* Well, I didn't really get that, but I had to Ctrl+C the thing after over 67 minutes! What went wrong? Well, I went ahead and broke symmetry and changed the last block of code to look like this: testlist.each do |n| And I finally got some sane results: real 0m4.103s Now, while this shows me that Python was able to perform this operation significantly faster than Ruby (and will certainly hold weight when it comes to choosing the language I will use), I'm still thoroughly confused as to why the previous method didn't work! Maybe I just don't know enough about string concatenation (after posting this I'm going to go reread that section in "Programming Ruby: The Pragmatic Programmer's Guide"). Anyone have any ideas? I'd like to hear them. It's likely just something I totally overlooked, as usual ;) Anyway, in case you were wondering, it really is recommended to use "<<" instead of "+" for string concatenation, because it is faster. |
| Last Updated on Sunday, 08 February 2009 04:57 |
