GolfScript, CJam, or Pyth?

9

2

Which language usually produces shorter golfed code: Pyth, CJam or Golfscript?

In asking this, I do not mean which has a prettier syntax or which has the most useful commands, I mean overall, which one is usually the shortest? What types of problems would we expect each language to have an advantage in?

Nico A

Posted 2015-08-08T13:14:14.707

Reputation: 2 390

Question was closed 2015-08-11T07:48:42.863

I'm not 100% sure if this is on topic here, but if it isn't, the next most logical place would be Meta. And somehow this would seem the more logical place. I think it's an entirely valid refinement of http://codegolf.stackexchange.com/q/2070/15599 choosing languages for golfing.

– Level River St – 2015-08-08T13:23:36.083

I'd say that Pyth usually ends up being the shortest. If not, it will likely be soon, as it's still under heavy development. – orlp – 2015-08-08T13:26:07.113

GolfScript seems to usually lose to CJam, I assume because of CJam's one-character builtins. – PurkkaKoodari – 2015-08-08T13:35:45.187

Since I have no idea where the chat is, should I delete this post and post it on meta? – Nico A – 2015-08-08T13:52:28.233

For chat, click the arrow to the right of the Stack Exchange logo, then chat or here.

– Dennis – 2015-08-08T13:57:45.793

I would go for meta, because in future it will be easier to find the question there than on chat. But (for @Sp3000 and those close voting) I don't entirely disagree with your position, but does that mean we should move the question I linked to meta too? I admit there are some differences between the two questions. – Level River St – 2015-08-08T14:03:26.977

5@steveverrill (and Sp3000), this is not on-topic for meta. Meta is for discussion of this Stack Exchange site (PPCG) itself, not for discussion about programming puzzles or code golf. – Doorknob – 2015-08-08T14:09:15.030

@Doorknob Then would you suggest keeping it here or just deleting the question altogether? Or maybe just asking in chat? – Nico A – 2015-08-08T14:12:03.583

3@TreFox Chat seems like the best place to ask a question like this. – Doorknob – 2015-08-08T14:12:24.437

Trefox, @doorknob has posted some data on chat showing Pyth better than Cjam better than Golfscript, but obviously it depends on the specific challenge, and the languages are under constant development. – Level River St – 2015-08-08T14:30:15.460

2All Stack Exchange sites are fundamentally Q/A, and we should try to include as much helpful, high-quality content as possible. Even if this isn't currently on-topic, we should make the necessary changes to accommodate questions like these. – lirtosiast – 2015-08-09T03:13:48.033

3Voting to reopen. I don't see how this is off-topic. This is a tips questions about golfing. Selecting a fitting language is a way to shorten code. (Though I personally believe that only comparisons within the same language are meaningful.) – xnor – 2015-08-09T06:01:39.043

@xnor If this is on topic, it's arguably a duplicate of the post steveverrill linked, and I'm not sure there is any benefit in asking the same question but limit it to three languages. – Martin Ender – 2015-08-09T07:12:43.250

@MartinBüttner In the post steveverrill linked they are only talking about well known langauges, such as Java, C++, etc. they do not mention golfing languages, which I think differentiates the two questions. – Nico A – 2015-08-09T11:47:07.783

They do cover GolfScript, J and APL. Pyth and CJam are younger than the question, but in any case, that's not the question's fault but the answers'. The right thing to do would be to encourage updated or specific answers to that question, e.g. by offering a bounty. – Martin Ender – 2015-08-09T14:55:11.490

@xnor Even if this is conceptually on-topic, how would it not be too broad or primarily opinion-based? – Alex A. – 2015-08-09T17:22:49.903

@MartinBüttner Hmm, perhaps it is a dupe. We can see how a close vote for dupe goes if it reopens. The answers here are quite nice though -- might it be possible to merge them into the other question. – xnor – 2015-08-09T18:06:29.280

@AlexA. The question is specific and people have given well-justified answers including statistics, so I think it's fine on the broadness front. – xnor – 2015-08-09T18:08:14.560

Answers

24

There is no simple answer to this question.

GolfScript almost never beats CJam. It has a lot less built-ins than CJam and the overall syntax is a bit more verbose. For example, print and {+}* become o and :+ in CJam.

The only exceptions are extremely trivial problems where the fact that GolfScript implicitly reads from STDIN is an advantage and the few occasions where block concatenation is useful (example).

With CJam and Pyth, it's a bit more complicated. Both languages can beat the other in particular challenges, although, by my completely unscientific observation, Pyth seems to have the upper hand on more occasions.

A few particular cases where picking one over the other is rather easy:

  • Choose Pyth if it has a built-in that CJam lacks and significantly simplifies the task.

    For example, Pyth has r<iterable>2, which is Python's swapcase(). If that's useful in a challenge (example), it will be hard to beat with CJam.

  • On occasions, you have to build the output piece by piece. After finishing the program, CJam implicitly prints the entire stack, so you can just dump the pieces and let the interpreter do the rest.

    For example, challenges are usually won by CJam.

  • The length of a single built-in may be a decisive factor in a challenge.

    Here, Pyth's C, which is shorter than CJam's 256b, was enough to outgolf CJam.

    This would be longer in Pyth, partly because trigonometric functions are 3-character built-ins in Pyth and 2-character built-ins in CJam.

To sum it up, there is no single language that will magically make you win all code golf competitions. You haven't even considered APL, J, K, O, Q, TI-BASIC, Retina, Perl, sed and Mathematica, which all have beaten both CJam and Pyth in a few occasions.

You have several options:

  • Learn Pyth and CJam. Use the one that's more suited for each task.

  • Learn the one (first) you like better.

    Personally, I'm crazy about stack-based languages, which I find much more easier to write and read.

  • Learn the one suited better for the challenges you enjoy.

    is your thing? Go with CJam. Nothing beats ? Forget about both and learn J.

Dennis

Posted 2015-08-08T13:14:14.707

Reputation: 196 637

4+1, this is a nice and comprehensive answer. However, it basically boils down to "it depends," which is more a fault of the question than of your answer. – Alex A. – 2015-08-08T17:36:15.863

8

Doorknob posted the following statistics on Chat, which I understand are based on answers to existing challenges. All credit goes to him. They certainly agree what I would have imagined.

golfscript is shorter than pyth 6 times (~13.043%)
golfscript is shorter than cjam 16 times (~20.513%)
pyth is shorter than golfscript 39 times (~84.783%)
pyth is shorter than cjam 59 times (~60.204%)
cjam is shorter than golfscript 57 times (~73.077%)
cjam is shorter than pyth 31 times (~31.633%)

I was going to leave these to Doorknob to post, but I found them interesting and thought if the question is going to stay open a while longer, it would be nice to post them for posterity, before it gets closed.

Obviously the best language will depend on each specific challenge, and this data only considers the corpus of challenges that we have at the moment. A change in the type of challenges would alter the balance between the languages.

Additionally, this data reflects the state of the languages today (Aug 2015). The languages are still in development, so it will be interesting to see how they perform in later versions.

This only covers challenges where at least two of the languages participated, which is probably fairer than considering all challenges. Nevertheless, as in virtually any statistical analysis, it is arguable that there could be some selection bias. It's possible that many challenges were so suited to a particular language that the others were not even considered.

Level River St

Posted 2015-08-08T13:14:14.707

Reputation: 22 049

5

It's worth mentioning that these statistics are for challenges where at least two of the relevant languages participated, which as Martin discusses in chat could be subject to selection bias

– Sp3000 – 2015-08-08T16:31:31.107

1

Considering that the point of code golf is to think in terms of a given language's kolmogerov complexity, in my opinion it doesn't matter much which language you use as long as you are clever. All of {Golfscript,CJam,Pyth,J} allow you to exercise creativity as do non-golfing languages. While code-golf technically claims "shortest code wins" this typically only really matters within each language. Often more important (as far as I can tell from looking at voting patterns, etc) is how creative or difficult your solution is.

For example HQ9+ while hilarious doesn't really win a 99 bottles of beer competition, because writing 9 isn't exactly showing effort. Meanwhile the Malbolge execution of 99 bottles of beer is considered incredibly cool despite being very lengthy due to its creative nature and the effort put in to make it. If HQ9+ did win we could merely define a language L which states: L outputs the solution to this problem, otherwise the code is compiled as C and executed.

This is why most old golfing sites still in operation are language-specific (Perl, Vim, C [if you count IOCCC]) because comparing code-sizes of different languages just doesn't make much sense. Most languages will have at least one challenge where they win. That doesn't make them 'better' than the alternatives.

Interestingly enough I have found that if you stretch the definition of language too much you get very creative/odd solutions to problems. (EG: many ASCII-art competitions could be won by a series of Vim keystrokes over at vimgolf.com).

If code-length is really all you care about, there are even more terse languages then the mentioned Golf-oriented languages. The reason they are often used for golfing is that their code-size is typically directly related to algorithmic complexity, whereas in for example C, you may sacrifice algorithmic complexity for shorter syntax to out-golf the competition. Meanwhile the even terser languages aren't really designed to be human programmable, and are more oriented to approximating kolmogorov complexity or theoretical applications.

My recommendation: learn whichever golfing language is based on a language you like. If you enjoy Python, learn Pyth. If you enjoy Ruby, learn GolfScript. If you like C/C++/Java learn CJam (and if you find you enjoy CJam you may wish to look into Joy which is a similarly concatenative-stack-based language designed for usability rather than golfability)

LambdaBeta

Posted 2015-08-08T13:14:14.707

Reputation: 2 499