Live data from Hacker News

Ruby is too slow for programming competitions

blog.clifreeder.com

91–100 of 254 posts

Re: Ruby is too slow for programming competitions

#91

Earlier quoted context omitted.

Wouldn't you want to use a language that is familiar in a competition? If Ruby is your forte, surely you should use it? I fail to see how "magic" properties of a language matter if you've got reasonable algorithmic chops and focus on solving the problems rather than trying to be clever.

> Wouldn't you want to use a language that is familiar in a competition? If Ruby is your forte, surely you should use it? If you're familiar with knives why not bring a knife to a gun fight? If knives are your forte, surely you should use it?

[deleted]

Re: Ruby is too slow for programming competitions

#92
post #20

I also come to the conclusion that Ruby-like languages are just not the right tool for this kind of problem, but it should be pointed out that you would have been able to solve the 10^14 data set easily, even with Ruby. I was solving that problem with PHP and got it running reasonably fast (1s per range) for ranges up to around 10^60. PHP is probably faster than Ruby, but it's still in the same area. The first trick…

I learned python about a month ago and was able to solve all three parts of this problem correctly (part C was the most difficult). My code was poorly optimized, but with some careful forethought, python's bottlenecks were not too problematic.

The first two parts were very easy with only brute force by working thru the square root of the full range once, caching the numbers, and using that cache. I don't see why people (including the auhor) recalculated the numbers for each goven range, especially in part B which required 10k such ranges.

That said, my python code, as a relative newbie, took 10s to cache all fair and square numbers from 1 to 10^14. I ran thru from 1 to 10^7, checked if palindromic using strings, and if good checked the square. (there were only 39 such numbers in total). I then ran all 10k answers in seconds.

Part C was MUCH more tricky, until I discovered the same trick most others found, that only some of the square root palindromes are valid with digits 0, 1, and 2. That said, as a python n00b, my code took 40 mins to run the range from 1 to 10^100. I asked the google staff if pre-caching is allowed, and they said yes, as long as my code to generate the pre-cache is provided wih my submission.

Properly optimizing my python would probably give similar performance. But the point is that with a bit of forethought, even poorly-optimized python code by a newbie was sufficient to solve all three parts of this question.

Re: Ruby is too slow for programming competitions

#93
post #76

Earlier quoted context omitted.

Yeah sure. It's just that for performance reasons some languages really are better than others.

Agreed. If performance is the main (or an important) criteria for the competition then the choice of language is rather more significant.

It's why IOI (http://www.ioi2012.org/competition/rules/) doesn't allow interpreted languages, because they are (or at least when the competition was first held were) just too slow.

If you have only 1-5 seconds of computations time you're going to choose for a compiled language every time

Re: Ruby is too slow for programming competitions

#94
post #66

Great, so stop dicking around with some zero-sum game ( http://en.wikipedia.org/wiki/Zero%E2%80%93sum_game - a competition may only have one winner ) and go build a business, where no one cares what language you use as long as the solution works for your customers - and there can be more than one winner in many cases. Or even if you don't have a side project or startup or something, go build something cool and open s…

You realize this is mostly a type of game right? "Do X task the best way you know how". If you think games are a waste of time, welp. I'm clicking the down arrow, because not only are you being a dick, you're adding nothing to the discussion.

I think it adds a lot to the discussion to point out that games are just games and that in the real world, the speed of a language's implementation may not be what actually matters, at all.

No thanks for the name calling.

Re: Ruby is too slow for programming competitions

#96
post #29

I'm not a rubyist but it appears the OP was doing something like this: for each range (start to sqr(end)) for each number in the range check if the number is fair and square If you do this you're checking the same numbers many times (total count of numbers checked is over 3 hundred million calls...) The right approach is to first compute all fair and square numbers between 0-10^14, store the result, and then simply c…

A slightly more interesting approach: find out all non-overlapping ranges you need to test, and then run the fair-and-square generator, testing each to check in which of the ranges it should be put, finally combining the sub-ranges to make the ranges asked for. My (not particularly optimized) Go solution ran through the C-large-1.in set in 3 odd seconds.

Re: Ruby is too slow for programming competitions

#98
First of all, there are plenty of people who solved both large inputs with Python/Ruby (with and without pre-computing all the fair and square numbers).[0][1]

As others have stated, the contests are designed such that language shouldn't matter and problem sets are deemed solvable with Python as a baseline. For this particular question, the official contest analysis goes into detail on how to reduce the solution space:

https://code.google.com/codejam/contest/2270488/dashboard#s=...

Now the issue becomes the trade off between development speed vs runtime performance.

Is it possible that you might solve the problem with a worse solution in a better performing language? Yes.

Is it possible you might finish a solution within the time constraints in a more expressive language? Yes.

If something is CPU bound in production, then going with a better performing language is a viable consideration.[2] However, my goal in programming contests is often to become a better programmer in the process. As a result, I choose to focus more on improving algorithmic complexity which is language agnostic.

[0]: http://www.go-hero.net/jam/13/solutions/0/3/Ruby

[1]: http://www.go-hero.net/jam/13/solutions/0/3/Python

[2]: For the pedantic, I understand that language performance is different from implementation performance.

Edit: Corrected links, thanks victorhn.

Re: Ruby is too slow for programming competitions

#99
post #85

I have noticed a similar thing recently. A programming challenge with a 10s max time restriction, and just reading the input and storing it in an array takes over 7 seconds. I allocate the array ahead of time, as soon as I know the size. The equivalent C/C++ version took under a second.

How did you read the input? All at once or one line at a time?

Re: Ruby is too slow for programming competitions

#100
post #93

Earlier quoted context omitted.

Agreed. If performance is the main (or an important) criteria for the competition then the choice of language is rather more significant.

It's why IOI ( http://www.ioi2012.org/competition/rules/ ) doesn't allow interpreted languages, because they are (or at least when the competition was first held were) just too slow. If you have only 1-5 seconds of computations time you're going to choose for a compiled language every time

I thought rules were there to discourage unfair advantage, not disadvantage? Why would you need a rule for that?

In any case, I see both python and ruby on the page you linked to, so perhaps I'm missing something.

Post reply on HN