I want to check my C++ code running time.
So, I wrote this C++ code.
But, this code can run in visual studio, not in bela.
How do I fix that code?
I want to check my C++ code running time.
So, I wrote this C++ code.
But, this code can run in visual studio, not in bela.
How do I fix that code?
You may want to be more precise about what can run in visual studio, not in bela.
and provide code that one can copy/paste to try it out easily.
This is the code in bela, and I don't know about C++ very well. Other basic code run in visual studio(ex : hello world).
https://github.com/pikavo/bela-test/blob/main/record%20time%20test
There are a few issues here. First: a regular Bela program doesn't include a main()
function. There is one hidden in the backend that will be used to initialise the audio and call setup()
, render()
and cleanup()
. If you include a main()
function, then that becomes the entry point of the program and if that doesn't call the Bela API, the program will not do any of the Bela stuff.
Second: the code you wrote is invalid: you cannot have function definitions inside another function ( you have setup()
, render()
and cleanup()
inside of main()
).
Third: Bela processes audio in real-time. It means that the timing with which render()
gets called is directly connected with the passing of time. In other words, it will only take exactly 1 second to process 1 second of audio, unless [cit] you are requiring more CPU than the board's capabilities in which case you will get dropped blocks and timing becomes unpredictable and the whole program becomes unusable.
Now to your question: if you want to measure how long the code in render()
takes to run, what you do is what you'd normally do in any other context: you run the function several times in a row and measure the time it takes, then divide by the number of times you ran it and you get the time it took to run. You can do this from within setup()
, which comes with a valid BelaContext*
argument that is suitable to be passed to render()
. Note that clearly this will not process any actual input audio or generate any output audio. It's just burning CPU cycles.
Here's an example:
93line 'cout' and 'endl' is not work in bela..!
you'll need what you'd need in any other C++ code that wants to use them:
#include <iostream>
using namespace std;
giuliomoro
I put your code
#include <iostream>
using namespace std;
And bela said
Makefile.linkbela:42: recipe for target '/root/Bela/projects/time_cal/time_cal' failed
Makefile:557: recipe for target '/root/Bela/projects/time_cal/time_cal' failed
Build finished
./build/core/default_main.o: In function main':
cleanup'
/root/Bela/core/default_main.cpp:92: undefined reference to
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: [/root/Bela/projects/time_cal/time_cal] Error 1
make: [/root/Bela/projects/time_cal/time_cal] Error 2
and stop. what is meaning this?
the code I posted was never meant to be complete. In fact, it won't even get to the point where you got, because it would fail at compile time as it's missing #include <Bela.h>
and it contains ....
in the last line.
You were meant to add the content of the setup()
function to your project, alongside whatever you already had in setup()
. Your code would also include a render()
and cleanup()
functions.
I updated the code above so that it uses a decent clock and gives useful results.
giuliomoro I tried that code, and It makes very useful result! Thank you!