Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Optimising usage -- (std vector)/Log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <vector>

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<Vertex> 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
Binary file added Optimising usage -- (std vector)/Log.exe
Binary file not shown.
43 changes: 43 additions & 0 deletions Optimising usage -- (std vector)/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <iostream>
#include <string>
#include <vector>

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<Vertex> 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