哪种方式更快?
Which of this ways is faster?
我想知道这两种方法哪个更快:
方法 1:结构如下:
//The Struct
struct element
{
Element* elementPtr;
uint map;
std::string name;
Vector3 pos;
long type;
}
//Type of container
std::vector<element> elementVctr;
//Finding method
for(uint ix = 0, ix < elementVctr.size(); ix++)
{
if(elementVctr[ix].name == "nameToFind")
{
return elementVctr[ix].name;
break;
}
}
方法二:VectorLike:
std::vector<std::vector<std::string>> elementVctr;
//Every element is this elementVctr[x], and its properties elementVctr[x][y]
//Finding Method
for(uint ix = 0, ix < elementVctr.size(); ix++)
{
if(elementVctr[ix][4] == "nameToFind")
{
return elementVctr[ix][4];
break;
}
}
我一直在使用 vectorLike,因为这是我所知道的唯一方法,但现在我已经学会了如何使用结构,而且我认为它们应该比我的向量方法更快。
另一个问题是,有没有比我的查找方法执行速度更快的其他方法来查看向量内部并检查它是否是我正在查找的内容?
感谢您的回答,那么您认为使用向量的向量和使用迭代应该执行更快的响应时间?
struct 方式在清晰度和设计方面更好,不一定更快。此外,由于向量的增长行为,通常不赞成使用嵌套向量。
我想知道这两种方法哪个更快:
方法 1:结构如下:
//The Struct
struct element
{
Element* elementPtr;
uint map;
std::string name;
Vector3 pos;
long type;
}
//Type of container
std::vector<element> elementVctr;
//Finding method
for(uint ix = 0, ix < elementVctr.size(); ix++)
{
if(elementVctr[ix].name == "nameToFind")
{
return elementVctr[ix].name;
break;
}
}
方法二:VectorLike:
std::vector<std::vector<std::string>> elementVctr;
//Every element is this elementVctr[x], and its properties elementVctr[x][y]
//Finding Method
for(uint ix = 0, ix < elementVctr.size(); ix++)
{
if(elementVctr[ix][4] == "nameToFind")
{
return elementVctr[ix][4];
break;
}
}
我一直在使用 vectorLike,因为这是我所知道的唯一方法,但现在我已经学会了如何使用结构,而且我认为它们应该比我的向量方法更快。
另一个问题是,有没有比我的查找方法执行速度更快的其他方法来查看向量内部并检查它是否是我正在查找的内容?
感谢您的回答,那么您认为使用向量的向量和使用迭代应该执行更快的响应时间?
struct 方式在清晰度和设计方面更好,不一定更快。此外,由于向量的增长行为,通常不赞成使用嵌套向量。