Skip to main content

Ralsina.Me — Roberto Alsina's website

The missing language

In the last month or two I have been writ­ing a fair bit of C code.

I re­al­ly dis­like C. But I have not been able to find a lan­guage to re­place it with, ex­cept C++, and that's not much of an im­prove­ment for what I did.

Maybe some­one can read this and help me. I don't mind learn­ing a new lan­guage. Re­al­ly, it's no prob­lem.

The goal is writ­ing small pro­grams (in the hun­dreds of lines re­gion), which have the fol­low­ing re­quire­ments:

  1. The pro­­gram MUST be cor­rec­t. Can nev­er seg­­fault or crash in some oth­­er way.

Since the code is short, I think I have man­aged, us­ing bstr­lib, to get this, af­ter quite a bit of pain, by au­dit­ing and unit-test­ing. But I am not sure, and I will nev­er be.

It would be awe­some if the lan­guage could be garbage-­col­lect­ed for this rea­son.

  1. It MUST have a de­­cent string han­dling.

The pro­grams han­dle mail. So, it's strings ga­lore.

  1. It MUST have very low over­­head. The pro­­grams should run and end quick­­­ly. Very quick­­­ly, be­­cause peo­­ple may be wait­­ing for them to end.

  2. It MUST be a rea­­son­ably sim­­ple lan­guage. I am not a great pro­­gram­mer, and these tools will be hacked at by peo­­ple with lim­it­ed pro­­gram­ming skills (sysad­mins like me!)

  3. Triv­ial ac­cess to C li­braries.

So, let's see.

C: gives 3, 4 (in a fash­ion), and 5

C++: 2, 3, maybe 4, and 5

D: It seems to give me all of the­se, along with a C-­like syn­tax... but I can't get it to work yet (cor­rec­tion, I got dmd to work, so it's look­ing good for D!).

So, any tak­er­s?

noteventime / 2006-04-17 19:33:

Python might be an idea if you don't mind scripting languages.

Lars Ivar Igesund / 2006-04-17 19:37:

Hey!

I think you found the perfect match in D, and I don't see any good reason for it not to work for you, unless you don't use Windows or Linux for your OS.

Check out the #D channel at freenode, I and the rest there will do our best to help you with any problems.

larsivi

Anonymous / 2006-04-17 19:39:

try D

Aaron Krill / 2006-04-17 19:48:

I say use Python. 2.4 is quite speedy.

Or use Java.

Robin / 2006-04-17 19:53:

Also try Ruby if a interpreted "scripting language" is a choice. Access to C libraries is said to be easier with Ruby than with Python. And programming in Ruby is fun!

winckler / 2006-04-17 19:57:

try ml

Inorog / 2006-04-17 19:58:

Hi Roberto

I'm intrigued by D since quite some time. I only wish it came in the gcc suite. I seem to remember to have read it can link just well to C (and C++?) libraries, so we wouldn't have the high barrier of entry ruby has compared with python, for example.

Well, I guess I only need a project that would help me get a practical use for D.

Roberto Alsina / 2006-04-17 20:07:

Those who propose python and ruby: you don't understand exactly how quick these programs need to work.

They are run, 5 or 6 of them, for each message in a mail server that processes hundreds of thousands of messages per day.

Sometimes per hour.

For example: a dash shell script (much lighter than python or ruby) had much more overhead than rewriting it in C! :-)

I am a big python advocate, but they are not the tool for this job.

Jesper Louis Andersen / 2006-04-17 20:15:

My first bet was scheme. Specific powerful compilers are Gambit, Chez (not open-source) and stalin. But since you need extreme speeds, you will need to think about how memory is allocated.

ML-variants are extremely fast. Take a look at mlton or ocaml for getting speeds on par with C.

oort / 2006-04-17 20:17:

I had similiar problem and I've chosen C. My program, which must run really really fast, was written in bash. Next, I've tried python, which occured to be slower, and then, I tried and ported it to C, which is much faster. Simple fragment was even 100 times faster than its bash version. So, I need to stay with C, even with its problems and maybe this is time to learn writing safe code ;)

Roberto Alsina / 2006-04-17 20:20:

oort: the problem you had with bash could be if the program is dominated by loading time.

Once a bash script is in memory and running, it's almost finished ;-)

I have notinced HUGE performance improvements by switching for dash in shell scripts. It loads in about 1/10th the time of bash, and then the script speed is rather unimportant.

Roberto Alsina / 2006-04-17 20:21:

Re: lisp/scheme... ok, I have been postponing lisp for the last 12 years, maybe it's time to look into it already....

ML is very interesting as well.

George Shapovalov / 2006-04-17 21:13:

Looks like you are ready for Ada ;).

No, seriously, looking at your list of requirements, it seems to be a good fit. #1 is just legendary. #2 is covered - you have fixed, bounded and unbounded strings. WRT #3 and 5 I will just mention that since a few years Ada has become an official gcc frontend. Which means that you get trivial linking with C code (and C++ for that matter), very much the same code efficiency (same backend) and it is provided by all major distributions. As for #4, you just have to try. You won't regret it, I promise ;).

For starting up I cannot recommend enough this book:
http://www.amazon.com/gp/pr...
(Programming in Ada 95 by John Barnes)

ahoy / 2006-04-17 21:21:

ocaml or eiffel
both compile fast and lead to programs that do not crash

Ian Monroe / 2006-04-17 21:43:

I was going to suggest Ruby, but I guess that won't work.

So I have to agree with George Shapovalov - Ada would be a good choice. Its the defacto standard for writing "correct" programs. It was designed for flying airplanes and ICBMs; I think it can handle your mail. ;)

Not a particularly easy language, but it is the first language that my university teaches so its not particularly difficult either.

SiberianHotDog / 2006-04-17 22:17:

While you are trying to get D to work(which is the best match in my eyes too, unless you need C++ support[=gui :P]), you could also take a look at http://www.hpl.hp.com/perso... .

palpatine / 2006-04-18 00:35:

My vote goes to Ada (2005 is the best version by far) and Ocaml. In both cases you need to learn how to write good code (question of art more than mere technics ;) ).
Ada is imperative, it can give access to pointers (and registers, ideal for real time :) ), so you must be careful to what you're doing with it (no nasty code like in C because of harsh code verifications, but you can have your program not crashing and doing rubbish instead :s ). It's a very solid language, I use it every day at work (OK, I work on a military project ^^).
Ocaml is functional, so it's pure mathematics. It's even more strongly typed than Ada (but it forbids to have coertion, also present in Eiffel, for that very reason), so the compiler is good enough to guess what the types are (sometimes you can also declare a function without precising what the arguments are !). It is very strong with recursions (a 'for' loop isn't nice, though), and ML syntax allows to have matching, it rocks ! :)
I don't practice Eiffel, but I also recommend you to take a look at it. That's a very powerful OO language...
Concerning rapidity, Ocaml is better, but we have to precise that Ada2005 comes with a lot of optimized libraries (useful for not creating everything from scratch like before, with pointers and free for trees by instance :/ ), and the compiler adds a lot of code for verification at the execution (and the coder of Ariane5's bugged module should not have disabled the int overflow verification...), hence the relative lack of performance...

Jon Severinsson / 2006-04-18 09:34:

I'd give my vote to Ada as well, recommending the same book as above (Programming in Ada 95 by John Barnes).
#1 is the goal of Ada, you won't get another language that is better on this point.
#2 is also great, though you must remember what kind of string you've got (fixed, unbounded or bounded) and convert between them. (Not that different from C++, but might be considered a drawback compared to say Java)
#3 is covered, perfomance is identical to C/C++
#4 It is as simple as you get without going VB. It's not C syntax, but Algol/Pascal, but that actually only makes it simpler (though you might have to re-learn some stuff if you only know C based languages).
#5 Is also coverd, as it's a front-end to GCC. It will also easily bind to C++.

palo / 2006-04-18 10:27:

I'm not sure if Lua would be fast enough - it is faster than python/ruby/etc, but it's certainly not C :)

On the other hand, it's incredibly small and simple. So maybe...

james / 2006-04-18 14:09:

how about cyclone?

Roberto Alsina / 2006-04-18 15:00:

Cyclone? Got URL?

fulan / 2006-04-19 11:55:

Objective-C is fast enough, Apple use it

Chris Parker / 2006-04-19 14:04:

Lisp or C#

Lisp has Perl regex, which is actually faster than the Perl version. With sbcl, all of my code is compiled, and it has a gc to boot.

C# seems like a Lisp copy.

Roberto Alsina / 2006-04-19 14:19:

C# won't work. While the code runs fast enough, it takes too long to load.

Everyone: before you suggest another language that is interpreted/has a VM, please try this:

* write the smallest program you can

* start it 100000 times in sequence

* time it

* compare it with the smallest shell script you can write running the same way.

If it's not 20 times faster than the script, it's not worth it, because C/C++/D are 50 times faster than the script.


Contents © 2000-2023 Roberto Alsina