返回一个矢量 [英] returning a vector
问题描述
哪种方法最快/最好:
std :: vector< intfoo1()
{
std :: vector< intv;
...
reutun v;
}
std :: vector< int>& foo2()
{
static std :: vector< intv;
...
reutun v;
}
void foo3(std :: vector< int>& v)
{
...
}
我倾向于说foo3是最好的,但foo1和foo2更多
" ;方便和QUOT ;.有没有办法像
foo3一样快速返回矢量?
-
-Gernot
int main(int argc,char ** argv){printf
("%silto%c%cf%cgl%ssic%ccom%c"," ma",58,''g '',64," ba",46,10);}
Which method is the fastest/best:
std::vector<intfoo1()
{
std::vector<intv;
...
reutun v;
}
std::vector<int>& foo2()
{
static std::vector<intv;
...
reutun v;
}
void foo3(std::vector<int>&v)
{
...
}
I tend to say foo3 is the best, however foo1 and foo2 are more
"convenient". Is there any way of returning a vector as fast as with
foo3?
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, ''g'', 64, "ba", 46, 10);}
推荐答案
2006-09-21,Gernot Frisch < Me@Privacy.netwrote:
On 2006-09-21, Gernot Frisch <Me@Privacy.netwrote:
哪种方法最快/最好:
std :: vector< intfoo1 ()
{
std :: vector< intv;
...
reutun v;
}
std :: vector< int>& foo2()
{
static std :: vector< intv;
...
reutun v;
}
Which method is the fastest/best:
std::vector<intfoo1()
{
std::vector<intv;
...
reutun v;
}
std::vector<int>& foo2()
{
static std::vector<intv;
...
reutun v;
}
不是线程安全的,我强烈推荐这个(不管
速度)。
Not thread safe, i strongly recommend against this one (regardless of
speed).
void foo3(std :: vector< int>& v)
{
...
}
我倾向于说foo3是最好的,但foo1和foo2更多
方便 ;。有没有办法像
foo3一样快速返回矢量?
void foo3(std::vector<int>&v)
{
...
}
I tend to say foo3 is the best, however foo1 and foo2 are more
"convenient". Is there any way of returning a vector as fast as with
foo3?
你可以自己测量一下,不过我怀疑你会看到
的显着差异(你可能会因为不使用<而获得更好的表现br />
std :: vector,以便利为代价。)
-
Roland Csaszar ------- ---- \\\ /// -------------- +43 316 495 2129
软件开发------ \ \\ /// ----------- http:// www。 knapp.com
KNAPP物流自动化 - \\V // - mailto:ro ************ @ knapp.com
You could measure it for yourself, though i doubt that you will see
significant differences (you may get better performance by not using
std::vector, at the cost of convenience).
--
Roland Csaszar ----------- \\\ /// -------------- +43 316 495 2129
Software Development ------ \\\ /// ----------- http://www.knapp.com
KNAPP Logistics Automation - \\V// - mailto:ro************@knapp.com
Gernot Frisch napisa3(a):
Gernot Frisch napisa3(a):
哪种方法最快/最好:
std :: vector< intfoo1()
{
std :: vector< intv;
...
reutun v;
}
Which method is the fastest/best:
std::vector<intfoo1()
{
std::vector<intv;
...
reutun v;
}
如果编译器具有RVO,可以快速为foo3。
May be fast as foo3 if the compiler has RVO.
>
std :: vector< int>& foo2()
{
static std :: vector< intv;
...
reutun v;
}
>
std::vector<int>& foo2()
{
static std::vector<intv;
...
reutun v;
}
编程错误。
badly programmed.
>
void foo3(std :: vector< int>& v)
{
...
}
>
void foo3(std::vector<int>&v)
{
...
}
首选。
-
Marcin Gabryszewski
G DATA软件
www.gdata.pl
地址:< ;姓><点><姓><在>< GDATA><点>< pl>
preferred.
--
Marcin Gabryszewski
G DATA Software
www.gdata.pl
address:<FirstName><dot><Surname><at><gdata><dot>< pl>
Gernot Frisch写道:
Gernot Frisch wrote:
哪种方法最快/ best:
std :: vector< intfoo1()
{
std :: vector< intv;
...
reutun v;
}
Which method is the fastest/best:
std::vector<intfoo1()
{
std::vector<intv;
...
reutun v;
}
有了一个好的编译器,如果在初始化中使用
函数,上面显示的方法将是最快的:
std :: vector< inttest = foo1();
如果你用它来重新分配,它的效率会降低(但是建议使用b / b
扩展语言会带来效率):
std: :vector< inttest;
....
test = foo1();
您可以使用std :: swap重新获得速度。
With a good compiler, the method shown above will be fastest if the
function is used in initialisation:
std::vector<inttest = foo1();
If you use it to reassign, it gets less efficient (but a proposal to
extend the language will bring back efficiency):
std::vector<inttest;
....
test = foo1();
You can regain speed by using std::swap.
>
std :: vector< int>& foo2()
{
static std :: vector< intv;
...
reutun v;
}
>
std::vector<int>& foo2()
{
static std::vector<intv;
...
reutun v;
}
这根本不快:被调用者必须复制整个
向量。
This is not fast at all: The callee will have to copy the entire
vector.
>
void foo3(std :: vector< int>& v)
{< br $> b $ b ...
}
>
void foo3(std::vector<int>&v)
{
...
}
这个将非常快。问题在于易于使用和
意图:初始化不再可能:
std :: vector< inttest;
test = foo3 ();
此外,代码将不再具有强大的异常保证(因为
大概是foo3从清除向量开始)。
我会选择foo1作为默认实现,并且可能提供
foo3 as:
void foo3(std :: vector< ; int>& v){std :: vector< inttemp = foo1();
std :: swap(v,temp); }。
This one will be pretty fast to. The problem is in ease of use and
intent: Initialisation is no longer possible:
std::vector<inttest;
test = foo3();
Also, the code will no longer have the strong exception guarantee (as
presumably foo3 begins by clearing the vector).
I would choose foo1 as the default implementation, and perhaps supply
foo3 as:
void foo3(std::vector<int>&v) { std::vector<inttemp = foo1();
std::swap(v,temp); }.
>
我倾向于说foo3是最好的,但foo1和foo2更多
" ;方便和QUOT ;.有没有办法像
foo3一样快速返回矢量?
>
I tend to say foo3 is the best, however foo1 and foo2 are more
"convenient". Is there any way of returning a vector as fast as with
foo3?
/ Peter
/Peter
这篇关于返回一个矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!