0

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

enter image description here

And in my CMake file I have such configuration (not sure about it)

enter image description here

EDIT3

Added optimization configuration to CMake file (according to this SO answer https://stackoverflow.com/a/41361741/5709159)

enter image description here

11
  • 3
    Are you compiling with optimizations enabled? What are your specific compiler flags (and what compiler are you using)? Commented Jul 28, 2019 at 9:01
  • 1
    Pleas provide a minimal reproducible example.
    – rustyx
    Commented Jul 28, 2019 at 9:47
  • 2
    Don't forget that your print statements also take time. Commented Jul 28, 2019 at 10:33
  • @JesperJuhl If I understood you correctly, so added to question ... If is it ok?
    – Sirop4ik
    Commented Jul 28, 2019 at 10:41
  • @rustyx provided... added to question
    – Sirop4ik
    Commented Jul 28, 2019 at 10:41

0