Sunday, April 19, 2009

Writing your own programming language with the DLR

Benjamin Nitschke's has written a couple of posts on writing your own programming language with the Dynamic Language Runtime.

The first is a long list of links and resources on the DLR and the tools (like the ANTLR parser) used in creating a programming language. The second is a very long entry comparing the performance of code written in various ways - including on top of the DLR.
I will also try to post some useful links about my recent DLR (Dynamic Language Runtime for .NET) research.

I have been working a bit on the DLR before, mostly together with Silverlight, which was cool, but Silverlight was way too hard to work with and I still think it is not distributed enough. And even before that quite a bit with the Visual Studio SDK, early IronPython versions and other language implementations in .NET and even with native c code (but usually I just modified existing samples). I have also modified Lua for my own needs recently and made it run on the Xbox 360 and PS3 and modified some behaviour for my projects, since we use it for our upcoming game and I use it quite a lot together with IronPython on the tools I write at work.
The last few days I was working a little bit with the DLR and got to a point today where I was wondering about the performance of a very simple while loop in the DLR, specifically in the ToyScript DLR sample project and of course IronPython. I am totally aware that the following comparison DOES NOT really provide accurate information about the actual performance of the languages and techniques used. Please keep this in mind when reading this article, it only covers a very certain aspect of all used languages. More specially how they perform when adding numbers in a while loop a lot of times! Some of it reflects that compiled languages are faster than dynamic languages, but you should not compare the absolute values, Python or Lua is not really 100 times slower, in many cases you can reach almost similar performance as long as you implement or call performance-critical code in a clever way (e.g. calling C++ code, using frameworks, etc.).

I was only interested about very rough comparisons to figure out which ways are fast and which languages or techniques are kinda slow. Some code written in Assembler or direct IL code execution was really fast (duh, no wonder), but all execution times of C++, C#, Assembler or IL are way faster than all other dynamic languages or DLR approaches. The important thing for me was to figure out why dynamic languages like IronPython or ToyScript on top of the DLR were like 100 times slower for this specific code. At the end of writing this article I was able to provide pretty good performance with the DLR, which is just 2-3 times slower than the fastest execution time. That's pretty good and could probably be even improved more.

Basically I used the following code sample, which adds 1.0 to counter 10 million times while decreasing the loops variable until it reaches 0. In languages that use types, counter would be a double (64bit floating point number).
A very interesting entry that compares the performance of a simple loop in:
  • C# 3.0
  • Native C++
  • Assembler
  • IronPython 2.6
  • ToyScript
  • Python 2.6
  • Python 3.1
  • Lua 5.1.4
  • Script.NET
  • IL Emit
  • Irony + DLR (dummy)
  • DLR AST with objects
  • DLR AST with doubles
His charts not only compare execution times but also parse and build times.

No comments:

Post a comment

Note: only a member of this blog may post a comment.