Friday, May 25, 2007

The for-Loop is Backword Compatible

The C++ styled for-loop is very practical and thus preserved in the D language. This code tests the speed of for-loop iteration:

void main () {
int x;
for (int i; i < 1000; i++)
for (int j; j < 1000; j++)
for (int k; k < 1000; k++)
x++;
writefln(x);
}

It took me a quarter of minute to write this piece of code, and again it compiles very fast. A billion iterations (1000 iterations per each loop) takes less than 3 seconds.

Cheers,
Boone

A Bit More Than the Typical Hello World!

If you already know C++, I would suggest to get started with codes. See for yourself how straightforward and concise the following code is:

import std.stdio;

void main (char[][] args) {
if (args.length == 1)
args ~= "John Doe";
writefln("Hi %s! This is my very first program in D.", args[1]);
}

The program writes to the console some string depending on the first arguement given by the user. If null it is set to "John Doe". That is about it. Simple, but does the job. Compilation is extremely fast; it terminated even before I noticed it. After compilation, you get a native executable that runs on both Linux and Windows.

Cheers,
Boone

About the D-Lang Blog

Hi there and welcome to my first blog about a programming language -- as you can tell from the title, it's the D language. It's been a while since I first heard about the language, but not until recently did I have a chance to really code in this language. I felt then nothing but was in awe. The language is just what I had been looking for, blending mixture of efficiency and ease of coding. An extremely short learning curve and shorter software development time are guaranteed. I have had long experience in programming and a few programming languages, including C, C++, Java, Python, Jython, PHP and Common LISP, the last one being the language used in my latest and very big project (almost twenty thousand lines of code, but remember it's LISP). There have been several problems with LISP, so I kind of looked around for an alternative. I was very much tempted indeed to go back to Java, however, that is not an option for our application at hand as efficiency and memory consumption are major points of concern. C and C++ might have done it, but I just never get along well with them. Maybe I am not good at manually dealing with memory allocation, or maybe I am just a bit sloppy sometimes. Anyway, I am not productive with C or C plus plus. It'd be obvious that Python is not a candidate here.

I came across the D language again when I searched for some reviews and language comparison on the web. I like the syntax and the language design goals, so I give it a try. With all my programming experience, I can code in D right away and I can write a simple program much faster than I can in C++ and Java. Compilation is only a snap. Running is unbelievably fast. I have then tried a little more features and within a few days made up my mind: D is the next language our system will be implemented in!!!

I created this blog to exchange my knowledge and issues to other D programmers. If you have comments or any suggestions, go ahead and drop a line. I'll also put some codes I write myself here if appropriate.

Cheers,
Boone