Python gets a lot of love these days, particularly as a good language for beginning programmers. Its friendly, flexible syntax combined with the fact that it’s a scripting language makes it an especially good choice for newbies. But can it really compete with the more traditional, compiled programming languages such as C and Java in terms of performance? New research suggests that a scripting language like Python might indeed be all you need for most programming tasks.
Sebastian Nanz and Carlo Furia of the Department of Computer Science, ETH Zurich, Switzerland in their newly published paper “A Comparative Study of Programming Languages in Rosetta Code,” compare the performance of a handful of prominent languages. They analyzed data from Rosetta Code, a repository of solutions to common tasks across different programming languages, such as matching strings against regular expressions, computing the factors of a positive integer and reading a specific line from a file. They chose to compare top languages representing four major programming paradigms: procedural, object oriented, functional and scripting.
Using a snapshot of Rosetta Code data from June 2014, the authors analyzed 7,087 solutions to 745 tasks across two top programming languages from each of those four categories: C and Go (procedural), C# and Java (object oriented), F# and Haskell (functional) and Python and Ruby (scripting). The languages were compared based on a number of different metrics such as conciseness, run time, memory usage and failure rates. Some of the key results are probably what you would expect, but others maybe not so much.
Scripting and functional languages generate the most concise code. Comparing the (non-comment, non-empty) lines of code for the same task, the authors found that scripting language code was slightly more concise than functional, with Python code being the most concise. Object oriented and procedural programs were found to be “significantly more verbose”. Java programs were particularly lengthy, being, on average, almost four times longer than equivalent functional or scripting code.
Compiled, strongly typed languages have significantly fewer runtime failures. Even though scripting language code may be more concise, it tends to produce more runtime failures. The researchers found Python generated, by far the most failures. Go, on the other hand, was the least failure prone, followed by C# and Haskell. Compilers clearly catch and prevent lots of problems before runtime, while strong static typing also leads to fewer errors later.
Scripting languages are faster for everyday tasks. As the conventional wisdom suggests, compiled languages were found to be faster for computing intensive tasks. The procedural languages, and C in particular, were by far the fastest when evaluating tasks with significant inputs, while OO, functional and scripting all lagged well behind. However, more interestingly, when only considering more mundane (and common) tasks (e.g., manipulating strings, computing checksums) Python, Ruby and Haskell were all faster than C. The authors suggest that the larger runtime executables of programs compiled to native code are costly for these smaller tasks.
While the procedural languages do well in this study in ways you’d probably expect, (fewer failures, faster for significant computing tasks, better memory usage) the results also suggest that, sometimes, using these languages might be overkill. Often times, if what you’re trying to accomplish isn’t particularly complicated or input intensive, Python may do it faster and with less code. Of course, you may land up spending some of those time savings dealing with runtime errors later. In any case, it's good food for programmer thought.
Read more of Phil Johnson's #Tech blog and follow the latest IT news at ITworld. Follow Phil on Twitter at @itwphiljohnson. For the latest IT news, analysis and how-tos, follow ITworld on Twitter and Facebook.