0

Trying to work with c++ to write faster code than Matlab, and will regularly want to get an array from a function. Decided that the easiest way to code this would be to create an array to fill with the answer I want, pass the pointer to the function, then let the function fill it with the answer. In this example, I've just made the function change all of the components to some number i.

The c++ code is

#include <iostream>

using namespace std;


void test2(int *change,int n, int i) //takes an array of n items and changes it to an array of n copies of i
{
    for(int counter = 0; counter < n; ++counter)
        *(change+counter) = i;
    return;
}

int main()
{
    int n = 5;
    int i = 3;

    int butts2[n];
    for(int counter = 0; counter < n; ++counter)
        butts2[counter] = counter;
    for(int counter = 0; counter < n; ++counter)
        cout << butts2[counter] << endl;
    test2(butts2,n,i);
    for(int counter = 0; counter < n; ++counter)
        cout << butts2[counter] << endl;

    return 0;
}

The matlab code is

tic
n = 5; i = 3;
butts = zeros(n,1);
for count = 1:n
    butts(count) = count;
end
for count = 1:n
    disp(butts(count));
end
for count = 1:n
    butts(count)=i;
end
for count = 1:n
    disp(butts(count));
end
time = toc

I expected the c++ code to outperform the matlab code, but in fact the c++ code takes about 0.03s but the matlab code takes about a tenth of that time. That the matlab code is faster when I comment out the displays in each code, and for larger values of n. What is taking the extra time in the c++ code?

Ste Rose
  • 33
  • 3
  • 2
    Your C++ code is ill-formed, btw. Array lengths must be constant. –  Oct 15 '19 at 22:53
  • 5
    You didn't show how you measure time for C++. How can we know you do it right. – bloody Oct 15 '19 at 22:54
  • 4
    First thing to ask is "What optimization level did you use?" If the answer is "I don't know", find out. You can't make statements about the performance of C++ code without knowing how optimized it is. If the answer is "None", You've answered your own question. – user4581301 Oct 15 '19 at 22:55
  • @SteRose How did you measured the time in your C++ code? – Javier Silva Ortíz Oct 15 '19 at 23:00
  • Hi @Chipster, ill-informed in what sense? My understanding was that the dimensions of an array needed to be fixed at run-time which they all are here, and it runs fine. – Ste Rose Oct 15 '19 at 23:04
  • @SteRose Nope. Arrays have a constant compile time length. Using them like this is non standard. – Martin York Oct 15 '19 at 23:05
  • @JavierSilvaOrtíz I just trusted the run time that came up on the console - I take it that's likely to give an overestimate? I'll use a more reliable method and comment what that gives. – Ste Rose Oct 15 '19 at 23:06
  • 1
    Remember to benchmark C++ in Release mode and not in Debug mode. Also these samples still won't give you meaningful results. Embrace both samples in an external loop which performs them 1000 times. The overall benchmark should last at least a few seconds to see the real difference. Otherwise OS scheduler work distorts the results, not to mention init/deinit the executable by your terminal. – bloody Oct 15 '19 at 23:08
  • 4
    Your test case is so vanishingly small that you're only measuring the time it takes to print the values. Both programs complete the calculations in a fraction of a microsecond. – molbdnilo Oct 15 '19 at 23:11
  • 1
    There is no timing code around the part of your program that you want to measure. It looks like you might be using the `time` command to measure your process's execution. Be aware that time includes the OS creating the process, executing it, and returning back to the shell. – paddy Oct 15 '19 at 23:15
  • @MartinYork, How should one write a function where they want the output to be the kind of thing I'm looking for? And please forgive my misunderstanding, but what's wrong with what's going on in this code (particularly given that it works)? – Ste Rose Oct 15 '19 at 23:25
  • @SteRose The acronym is VLA, for variable length array. Look for some informations here: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard – Amadeus Oct 15 '19 at 23:31
  • Is your code using SIMD or AVX-512 instructions? Is it using threading? – Eljay Oct 15 '19 at 23:40
  • See https://stackoverflow.com/q/54871933/10957435 –  Oct 16 '19 at 01:35
  • And this too: https://stackoverflow.com/q/57367473/10957435 –  Oct 16 '19 at 01:36
  • 1
    @SteRose This test is invalid. You put start and stop times in the matlab code, but failed to do anything like that in the C++ code. I would expect equivalent "start_time" and "stop_time" function calls in your C++ code. Second, you should time release, optimized versions of the C++ code. Third, the code is not standard C++ due to the usage of VLA's. Last, 5 rinky-dink items in an array is laughable when it comes to benchmarking. Make that 100,000 or a million element array. – PaulMcKenzie Oct 16 '19 at 01:55
  • Possible dupe https://stackoverflow.com/q/20513071/3978545 – Wolfie Oct 16 '19 at 07:25
  • Also, remember std::endl implies a flush of output buffer, it might be slower than printf or simple '\n'. Please edit your code and include how you measure the time in c++ so others can understand your question better. – X. Sun Oct 16 '19 at 07:42
  • To fix your VLA problem you can change `int n = 5;` to `constexpr int n = 5;`. – Thomas Sablik Oct 16 '19 at 08:13
  • You didn't answer if compiler optimization is enabled. C++ without optimization can be very slow. – Thomas Sablik Oct 16 '19 at 08:22
  • @ThomasSablik Optimization wasn't enabled, but I put a timer in the code (rather than reading from the console) and it's giving much lower numbers than Matlab now for all values of n I've tried. I sort of understand now why people *prefer* ```c++ constexpr int n = 5``` but I'm still not really sure why the VLA problem is a problem. Is it because of where the memory is allocated, or a speed problem (obviously in larger programs)? – Ste Rose Oct 16 '19 at 17:20
  • VLAs are not Part of ISO C++. It is a compiler extension. If it works and how it is implemented depends on the compiler and its version. Your code isn't portable. It could work in one version, behave different after a compiler update and stop working after the next update. One compiler could allocate the memory on the stack, one compiler on the heap, one compiler could produce undefined behavior and one compiler could abort the compile process with a compiler error. – Thomas Sablik Oct 16 '19 at 17:52
  • @ThomasSablik Thanks, that makes sense. If I did want to write a function that would output say the sum of two 'vectors,' would doing something roughly similar with std::vector work/is there any standard way of writing functions for vectors/matrices? – Ste Rose Oct 16 '19 at 19:37
  • You should use std::vector instead of C-style arrays: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rsl-arrays – Thomas Sablik Oct 16 '19 at 19:52

0 Answers0