Comparison of programming languages: Ruby, Groovy, Python and PHP

{16 Comments}

This article is part of a comparison of web frameworks which tries to measure the ease of learning and development for several web frameworks(Rails, Grails, Django and CodeIgniter) with a practical test. In order to learn Ruby, Groovy, Python and PHP, 8 hours were invested in reading documentation and a set of exercises were implemented. Source code of the implementation of the exercises can be seen here. In this article, a comparison based on this learning process and experience is made.

This article has the following structure:

  1. Comparison of time developing each exercise
  2. Comparison of readability of the languages
  3. Comparison of results in benchmarks and lines of code. From the project Computer Language Benchmarks Game
  4. Conclusions

It is important to notice that the first section can be difficult to understand without knowing about the implemented exercises. Therefore, in case you do not know about them I recommend you to skim it or jump directly to section 2.

Comparison of developing time

Table 1: Hours required to develop programming exercises with programming languages
Exercise Ruby Groovy Python PHP
Strings, files and regular expressions 4,5 3 2 2
Numbers set 4 2,5 1 2
Composite Pattern 1,5 1 1 0,5
TOTAL 10 6,5 4 4,5

Table 1 shows a comparison of timings used to implement each exercise with each technology. According to the results, Python is the programming language which required less hours to finish the exercises and Ruby was the one which needed the most. It is important to remember that the order of columns is the order in development. The first programming languages used required more time than the last ones. Python and PHP were the exception.

In the first exercise, Ruby was the first language used to implement. Therefore, at the beginning, it had to be understood the regular expressions and how to use them. For example, Ruby solution to the first exercise uses a regular expression which does not satisfy all the cases, therefore, an exception code had to be written. Nevertheless, in Groovy, the regular expression which satisfies all the cases was used. Python already had a method to separate between words and another method for removing punctuation, which increased the speed in resolving it. PHP solution is close to Python solution.

In the second exercise, in the case of Ruby it was necessary to understand the code blocks well. For the Groovy case it was easier, since Groovy block codes are similar to Ruby. For the Python case it was even easier, since Python has a number set class. When using PHP it had to be programmed in a manner closer to C++.

In the third exercise, Ruby and Python did not support control type argument, hence exceptions had to be written to control type arguments. On the other hand, Groovy and PHP allowed to control type argument. Furthermore, PHP supported abstract classes, therefore the Component class could be implemented as abstract.

Table 2: Source code lines in programming exercises
Exercise Ruby Groovy Python PHP
Strings, files and regular expressions 30 27 28 35
Numbers set 56 51 28 56
Composite Pattern 50 48 38 50
TOTAL 136 126 94 141

In table 2, the number of source code lines for each exercise was counted. Python presented the shortest code. The reasons are: Python does not need end statements in block of code, neither it uses curly braces syntax. Furthermore, in the Number Set exercise, it already had a library for implementing it, which resulted in a shorter code. Rest of programming languages are close to each other. PHP presents the longest code. It uses curly braces and does not have any special method for iteration, it uses the for structure which results in more lines. Besides, Ruby and Groovy are really close, but Groovy has control type argument for example, which allows to write less lines in the third exercise.

Based on this analysis and experience, Python presented the best productivity with the least amount of lines of code. Mainly due to the following facts: it provides a complete set of libraries which allowed to solve each exercise quickly and it has a mandatory indentation syntax. Difference in time between Groovy, Ruby and PHP can be a bit tricky, because how to solve the exercises was learnt using Ruby. Furthermore, the programmer had knowledge about how to use PHP because the manner to solve the problems is really similar to C++. Nevertheless, according to the facts PHP presented a high productivity, close to Python.

Readability

In this section, an analysis of readability of each programming language will be made. A comparison between programming languages and the natural languages will be carried out.

In natural language, a predicate is the completest part of a sentence. The subject names the “do-er” or “be-er” of the sentence; the predicate does the rest of the work. There are several types of predicate but it is always composed by a verb which may connect the subject with other modifiers or information. Object-oriented syntax is close to natural language. In the common form object.method(arguments) the object is the subject, the method is the verb and the arguments are the modifiers.

In table 3, different orders are expressed in natural language and their implementations in the different programming languages are shown. Examples have been extracted from the exercise whenever it has been possible.

In the first example, it is shown that Ruby allows to use a structure closer to a subject, the number set; the action, the method; and the question symbol which shows it expects a boolean. Groovy is similar, but it does not allow the ? symbol. In Python and PHP implementation the verb, the method, acts over the subject, in a less object-oriented manner.

In the second example, a structure for collecting inside a list of elements is shown. In this case, Groovy and Ruby are really similar, they both use the iterators and blocks of code, providing a solution really close to natural language. Groovy presents a shorter solution using the it keyword. Python and PHP present a closer syntax to C++. PHP uses the dot to sum strings, which is not as intuitive as the plus.

The third example is similar to the first one. Ruby uses the ! symbol to make reference to a method which will change the state of the object. Groovy uses an object-oriented manner and closer to natural language but does not use the exclamation symbol. In this case, Python is closer to natural language than before. PHP still uses the verb, function over the object.

Finally, the fourth example shows the ability of Ruby to use the unless statement.

Table 3: Comparing readability of programming languages
Natural Language Code Examples
Is the list of numbers empty? Ruby
@theNumberSet.empty?
Python
len(self.the_set)
Groovy
numbersMap.size() == 0
PHP
count($this->arrayset);
Collect names of each element and write them in a paper called result. Ruby
@composite.each{ |item|
result+=item.toString
}
Python
for element in   self.listComponents:
string += element.toString()
Groovy
components.each{
string += it
}
PHP
foreach ($this->list as       $element){
$string .=   $element->__toString();
}
Remove white spaces from the line Ruby
line.chomp!
Python
line = line.rstrip()
Groovy
Line.trim()
PHP
$word = trim($word);
If variable is not a component type, throw an exception. Ruby
raise ‘No component type’ unless component.kind_of?(Component)
Python
if not isinstance(component,Component):
raise NameError(“No component type”)

In table 3 a list of capabilities which make a language readable are shown. They are extracted from the experience developing these exercises and the website. Python is supposed to be highly object oriented, but it has been seen some examples where Ruby and Groovy are more object-oriented than Python. Furthermore, Python is the only programming language which considers indentation mandatory, which is a good feature for improving readability. The fifth feature shows languages which support keyword arguments. This feature was added to Ruby in version 2.0. It increases readability since it is not necessary to remember the order of the arguments for a method, instead it can be used named arguments.

Table 4: Comparing features for readability in programming-languages
Feature Example Ruby Groovy Python PHP
Highly object oriented 3.times{ puts ’hello’ } yes yes almost no
It allows to use ? And ! in methods line.chomp! yes no no no
Unless and until statements println ’hello’ unless dont
knowhim
yes no no no
It uses block codes and iterators listnumbers.each{|number|
print number
}
yes yes no no
Indentation is mandatory and is
used to delimit blocks of code
- no no yes no
Keyword arguments. Arguments for a method can be passed by a name travel(from=’pointa’, to=’pointb’) yes no yes no

Based on these examples and the experience developing, Ruby is considered to be the closest to the natural language because it has special syntax features. Groovy will be the second, Python the third, and PHP the last one.

Performance

In this subsection a brief analyses of performance for each programming language will be made. Results in figure 1, shows that Ruby, PHP and Python are similar in performance. Groovy is not shown. But Groovy is considered to be 8 times slower than Java. Hence, it would be around 10 20 times slower than C.

Performance comparison of programming languages.
Figure 1: Performance comparison of programming languages. Source CLBG

Figure 2 shows difference in execution time, memory and lines of code of Python over Ruby for 10 tiny examples. It is important to notice that Ruby and Python are similar in lines of code in these examples. In some examples Python is shorter and in others larger.

Python vs Ruby in peformance
Figure 2: Python vs Ruby in peformance. Source CLBG

Conclusions

Doing these exercises allows to have some level of experience with the different programming languages. It is difficult to measure productivity, due to the fact that the first implementation is when the first solution for the problem is described, hence it requires more time. Nevertheless, the next conclusions are extracted:

  • Python was the most productive language. Mainly due to the existence of libraries. It was the most concise too. It is important to remember that in The Computer Language Benchmark Game: Ruby and Python were similar in lines of code.
  • Ruby is the most readable language. Groovy is really similar to Ruby. Python presents new syntax features which are not common in C++, while PHP is really close to C++, hence less readable.
  • Groovy presents the best performance. PHP, Python and Ruby have a similar performance.

Those are the conclusions from this experience.

In my opinion:

  • Ruby is the most readable language, I really like it, I think its semantics and syntax is the way to go. I just miss the mandatory indentation from Python.
  • Python has libraries for almost everything. It is not only something  I have observed from this set of exercises. I have talked with colleagues about this topic and they agree Python has loads of libraries, specially about scientific computing. Furthermore, it is said it is well prepared to call C or C++ functions within Python. This feature allows you not to care about performance while developing and, if it is necessary, to make profiling at the end and to translate the parts of code which require more time to be executed. Nevertheless, it is not as readable as Ruby neither as object oriented and it does not have the same performance as Groovy.
  • Groovy is really similar to Ruby but it is not the same. Nevertheless, it has a better performance. Furthermore, you can develop in Groovy and call functions in Java. This allows to have the same methodology than with Python and C/C++.
  • PHP was designed for web development. Its syntax is really close to C++ but it is easier. It is fine for web development in case you do not want to use a web framework. Furthermore, WordPress, Joomla, Drupal, Moodle among others are build with this language. Hence, if you are planning to make a web project which adapts to those CMS, you will need to learn PHP to modify its behavior.

What do you think about this comparison? Which language do you think is more productive? and readable? What about the performance? Do you agree with my opinion?

Thanks for reading. Comments, criticism and sharing will be appreciated.