Re: [Vala] Fwd: Why is Vala 10 times slower than C ?

Hi Serge and all,

Serge Hulne wrote:
Actually, what I do not grasp is the following:

- Vala is allegedly just a thin object-oriented layer on top of C (glib2).

-  One might therefore expect a runtime performance which is closer to
say C or C++, than to the (slower) performance of a high-level
language (like , e.g.Python).

- However, it turns out that, surprisingly Vala is not only slower
than C and C++ but even slower than Python.

This is the part that I do not understand !

I've just seen that somebody already did the same, but I too tried to
optimize your wordcount implementation a bit and got to the following Vala

namespace WordCount {

    private int count_spaces(string str) {
        int result = 0;
        for (int i = 1; i < str.length; i++) {
            if (str[i] == ' ') {
        return result;

    public int main(string[] args) {
        File file;
        DataInputStream dis;
        string line;
        int wc, lc;

        for (int i = 1; i < args.length; i++) {
            file = File.new_for_path(args[i]);

            try {
                dis = new DataInputStream(;
                wc = 0;
                lc = 0;

                while ((line = dis.read_line(null)) != null) {
                    wc += count_spaces(line);

                stdout.printf("lc = %d. wc = %d\n", lc, wc);
            } catch (Error e) {
                stderr.printf("Error processing file '" + file.get_path()
                    + "': " + e.message);


        return 0;


Note that "valac --pkg gio-2.0 wc2.vala" is necessary to compile my version,
since it uses the Gio package. Compiling your own program as wc1 and my
variant as wc2, I get the following results (when run on the file ):

$ time /usr/bin/wc ../shakespeare.txt
124456  901325 5582655 ../shakespeare.txt

real    0m0.258s
user    0m0.248s
sys     0m0.004s

$ time wc1
lc = 124457. wc = 1418390

real    0m0.874s
user    0m0.860s
sys     0m0.008s

$ time wc2 ../shakespeare.txt
lc = 124456. wc = 1184092

real    0m0.278s
user    0m0.260s
sys     0m0.012s

So there really is no speed penality for coding in Vala as compared to C.
Some noteworthy points:

1. My implementation is still quite readable standard Vala code. No need to
revert to dirty tricks.
2. Even your own version was only 3 times slower than the C version, not 10
times. Maybe you're using an old version of the Vala compiler? (I used 0.12.0)
3. The most important changes in my experiment where:

a. Switching from FileStream to Gio's DataInputStream. That brought the time
down to 0.559s.

b. Switching from line.split(" ") (which creates lots of unnecessary
objects) to my count_spaces method. That brought it further down to the
0.278s noted above.

Best regards

|------- Dr. Christian Siefkes ------- christian siefkes net -------
| Homepage: | Blog:
|    Peer Production Everywhere:
|---------------------------------- OpenPGP Key ID: 0x346452D8 --
Everyone by now presumably knows about the danger of premature optimization.
I think we should be just as worried about premature design -- deciding too
early what a program should do.
        -- Paul Graham, Hackers and Painters

Attachment: signature.asc
Description: OpenPGP digital signature

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]