I am using JNI in my android app
I have such code
__android_log_print(ANDROID_LOG_ERROR, "SIZE", "SIZE ::: %s :: %s", std::to_string(vertices.size()).c_str(), std::to_string(NumVertices * 3).c_str());
vertices.resize(NumVertices * 3);
__android_log_print(ANDROID_LOG_ERROR, "AFTER ", "AFTER ::: %s ", std::to_string(vertices.size()).c_str());
Here vertices
it is std::vector<float>
, I am reuse the same vector every call to method, so I expect that first time method resize really make memory allocation, but next time NO (Just if I try to resize to size bigger than I have currently).
But in my case before resize size of vector around 500.000 items, and resize to 90.000 items... So, there should not be any memory allocation.
In addition I checked address &vertices[0]
before resize and after resize I get the same address - so, it is means that any memory allocation happening.
So, question is - why this line of code
vertices.resize(NumVertices * 3);
take around 4-5 milliseconds? It should take almost 0 ...
EDIT1
If I change my code like this
long vertices_size = NumVertices * 3;
if (vertices.size() == 0)
{
__android_log_print(ANDROID_LOG_ERROR, "INSIDE2", "INSIDE2 ::: %s",
std::to_string(vertices.size()).c_str());
vertices.resize(200000); // x y z for each vertex
}
long long int duration3 = duration_cast<microseconds>(high_resolution_clock::now() - t3).count();
__android_log_print(ANDROID_LOG_ERROR, "INSIDE1", "INSIDE1 ::: %s", std::to_string(duration3).c_str());
So, time execution came to 0,02
milliseconds, that looks right...
EDIT2
Compiler version
And in my CMake
file I have such configuration (not sure about it)
EDIT3
Added optimization configuration to CMake
file (according to this SO answer https://stackoverflow.com/a/41361741/5709159)