diff --git a/Optimising usage -- (std vector)/Log.cpp b/Optimising usage -- (std vector)/Log.cpp new file mode 100644 index 0000000..b460a7b --- /dev/null +++ b/Optimising usage -- (std vector)/Log.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; + + Vertex(float x, float y, float z) + : x(x), y(y), z(z) + { + } + Vertex(const Vertex& vertex) + :x(vertex.x), y(vertex.y), z(vertex.z) + { + std::cout << "Copied!" << std::endl; + } + +}; + +int main() + { + + std::vector vertices; + vertices.reserve(3); + + vertices.emplace_back(1, 2, 3); + vertices.emplace_back(4, 5, 6); + vertices.emplace_back(7, 8, 9); + + std::cin.get(); + } + + +// to optimise vertex that constructing inside main and copied to vector class,just construct in vector class itself +// just pass the parameter list itself in place of vertex object to get constructed in vector class itself +//tells vector construct a vertex object with the following parameters in place in our actual vector memory \ No newline at end of file diff --git a/Optimising usage -- (std vector)/Log.exe b/Optimising usage -- (std vector)/Log.exe new file mode 100644 index 0000000..3046a3a Binary files /dev/null and b/Optimising usage -- (std vector)/Log.exe differ diff --git a/Optimising usage -- (std vector)/main.cpp b/Optimising usage -- (std vector)/main.cpp new file mode 100644 index 0000000..1a68386 --- /dev/null +++ b/Optimising usage -- (std vector)/main.cpp @@ -0,0 +1,43 @@ +#include +#include +#include + +struct Vertex +{ + float x, y, z; + + Vertex(float x, float y, float z) + : x(x), y(y), z(z) + { + } + Vertex(const Vertex& vertex) + :x(vertex.x), y(vertex.y), z(vertex.z) + { + std::cout << "Copied!" << std::endl; + } + +}; + +int main() + { + + + std::vector vertices; + vertices.reserve(3); //takes capacity of 3 and its not resizing by copying or giving it in constructor is saying just construct it 3 vertices objects vertices(3) but we want memory + + vertices.push_back(Vertex(1, 2, 3)); + vertices.push_back(Vertex(4, 5, 6)); + vertices.push_back(Vertex(7, 8, 9)); // via defualt constructor + + std::cin.get(); + } + + +//This is about optimising using of std::vector class on reallocation and copying [slow code] +// rules of optimisation is basically knowing your environment +//how do things work and what do i need to do exactly and what should happen +// so now optimising about reducing copying our objects!! especially object storing vector class +// WE NEED TO KNOW WHEN COPY CONSTRCTOR CALLING +// constructing this vertex object on inside of our main funtion stack frame then pushing[copying] it to the vertices vector cause copy so can we construct that vertex in place of actual memory vector allocated to us +// vector resizing twice here one by defualt and 2 when moves to 2nd element and 3 moving to 3rd element +// make enough memory to hold vector from beggining is optimising 2 \ No newline at end of file