3

On the development of a language that compiles to JavaScript, I'm considering targeting C++ too, in order to generate faster programs when needed. My plan was to use std::vectors to hold the dynamic arrays of my language. Filling a big array repeatedly will be a normal operation (double buffering), so I've tested that:

#include <iostream>
#include <vector>
std::vector<int> gen(int w,int h){
    std::vector<int> a;
    a.resize(w*h);
    for (int i=0; i<w*h; ++i)
        a[i]=i;
    return a;
};  
int main(){
    for (int i=0; i<100; ++i)
        std::vector<int> a = gen(1000,1000);
};

Curiously, this program is not faster than the JavaScript equivalent:

gen = function(w,h){
    var a = new Float32Array(w*h);
    for (var i=0; i<w*h; ++i)
        a[i]=i;
    return a;
};
for (var i=0; i<100; ++i)
    gen(1000,1000);

Surprisingly, the JS version runs 3x faster.

clang++ my_program.cpp -o my_program
time ./my_program
real    0m1.393s
user    0m1.379s
sys     0m0.005s

time node my_program.js
real    0m0.458s
user    0m0.320s
sys 0m0.132s

Why is this happening? Should I reconsider?

10
  • 2
    Did you compile your C++ program with optimization enabled? Commented Nov 17, 2013 at 12:11
  • Have you compiled with optimizations on? Also, no need for the call to resize(). Just instantiate the vector like this: std::vector<int> a(w*h); Commented Nov 17, 2013 at 12:11
  • The problem with optimizations (I tried -O2) is that apparently it just generates a program that does nothing (because I guess it detects this program isn't doing anything). So it runs instantly. Problem is it obviously won't be able to apply those optimizations and run instantly when I'm actually using those arrays on the actual application. Ideas? Commented Nov 17, 2013 at 12:12
  • 2
    Then you must construct a realistic test case. Obviously, no code is infinitely faster than come code, so C++ is infinitely faster than Javascript :-) Commented Nov 17, 2013 at 12:14
  • 2
    @Viclib: so you are saying that JavaScript is faster but only if you tell C++ to be really, really slow? If you set up and unequal setting, e.g., chain Udain Bolt left foot to a huge metal ball and have him compete in 100m race then, you shouldn't surprised about unexpected results (although I would expect Udain to still win). Commented Nov 17, 2013 at 12:17

1 Answer 1

3

Try this version

void gen(int w,int h,std::vector<int>& a){
    a.resize(w*h);
    for (int i=0; i<w*h; ++i)
        a[i]=i;
}

int main(){
    for (int i=0; i<100; ++i) {
        std::vector<int> a;
        gen(1000,1000,a);
    }
};

The problem with your version is that it copies the vector when it returns the value. Therefore it's not a direct equivalent to the JS version.

Sign up to request clarification or add additional context in comments.

1 Comment

I would have expected copy elision to kick in here. Of course, it is not guaranteed, but still...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.