std :: copy与指针副本 [英] std::copy versus pointer copy

查看:109
本文介绍了std :: copy与指针副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道这两种方法中哪一种最快:std :: copy,其中

包含在标准库中,或者手动编写的指针副本?

你们有没有这方面的经验?


我认为库函数std :: copy会执行最佳地,因为它是一个库函数,因此这个函数的编写者

最能知道如何优化它...但是有些测试似乎表明

我的指针复制函数可以做得更好,特别是当它的

循环被矢量化时。我使用g ++和icc和-O2,icc和-O2

-march = pentiumiii -xK -tpp6作为编译器选项。


或者有没有另一种看待这个的方法?为了完整起见,我包括我的

测试程序:


< code>

#include< ctime>

#include< algorithm>

#include< iostream>


使用命名空间std;


typedef double Element;


class Vector

{

public:


Vector(unsigned int newsize){array = new Element [newsize]; size = newsize; } $ / $
~Vector(){if(array)delete(array),array = 0; } $ / $

void init(){for(unsigned int i = 0; i< size; i ++)array [i] = i; }

void print(){for(unsigned int i = 0; i< size; i ++)cout<< array [i]<< "英寸; cout<< ENDL; } $ / $

friend void iCopy(Element * dst,const Element * src,int n){int i = n + 1; while(i--> 0)* dst ++ = * src ++; } $ / $
friend void sCopy(Element * dst,const Element * src,int n){std :: copy(src,src + n + 1,dst); }


unsigned int size;

元素*数组;

};


int main()

{

向量a(10),b(10),c(10); a.init();

cout<< a:; a.print();

cout<< b:; b.print();

cout<< c:; c.print();


iCopy(b.array,a.array,7);

cout<< 在iCopy之后的b:; b.print();


sCopy(c.array,a.array,7);

cout<< sCopy之后的c:; c.print();


clock_t start,stop,taken;


int VECTOR_SIZE = 5000000;


矢量d(VECTOR_SIZE),e(VECTOR_SIZE);

d.init();


start = clock();

for(int i = 0; i< 10; i ++)iCopy(e.array,d.array,VECTOR_SIZE-10);

stop = clock();

take =(停止 - 开始)/ 1000;

cerr<< iCopy的时间: <<采取<< endl;


start = clock();

for(int i = 0; i< 10; i ++)sCopy(e.array,d。数组,VECTOR_SIZE-1);

stop = clock();

take =(停止 - 开始)/ 1000;

cerr< < sCopy的时间: <<采取<< endl;


start = clock();

for(int i = 0; i< 10; i ++)iCopy(e.array,d。 array,VECTOR_SIZE-10);

stop = clock();

take =(stop - start)/ 1000;

cerr< < iCopy的时间: <<采取<< endl;


start = clock();

for(int i = 0; i< 10; i ++)sCopy(e.array,d。数组,VECTOR_SIZE-1);

stop = clock();

take =(停止 - 开始)/ 1000;

cerr< < sCopy的时间: <<采取<<结束;


返回0;

}

< / code>


感谢您的回复。


问候,


Franky B.

Hello,

I am wondering which of these two methods is the fastest: std::copy, which
is included in the standard library, or a manually written pointer copy?
Do any of you have any experience with this?

I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it ... but some tests seem to indicate
that my pointer copy function can do a lot better, especially when its
loop is vectorized. I have used g++ and icc with -O2, and icc with -O2
-march=pentiumiii -xK -tpp6 as compiler options.

Or is there another way to look at this? For completeness, I include my
test program:

<code>
#include <ctime>
#include <algorithm>
#include <iostream>

using namespace std;

typedef double Element;

class Vector
{
public:

Vector( unsigned int newsize ) { array = new Element[ newsize ]; size = newsize; }
~Vector() { if ( array ) delete( array ), array = 0; }

void init() { for ( unsigned int i = 0; i<size; i++ ) array[ i ] = i; }
void print() { for ( unsigned int i = 0; i<size; i++ ) cout << array[ i ] << " "; cout << endl; }

friend void iCopy( Element *dst, const Element *src, int n ) { int i = n + 1; while ( i-- > 0 ) *dst++ = *src++; }
friend void sCopy( Element *dst, const Element *src, int n ) { std::copy( src, src+n+1, dst ); }

unsigned int size;
Element *array;
};

int main()
{
Vector a( 10 ), b( 10 ), c( 10 ); a.init();
cout << "a : "; a.print();
cout << "b : "; b.print();
cout << "c : "; c.print();

iCopy( b.array, a.array, 7 );
cout << "b after iCopy : "; b.print();

sCopy( c.array, a.array, 7 );
cout << "c after sCopy : "; c.print();

clock_t start, stop, taken;

int VECTOR_SIZE = 5000000;

Vector d( VECTOR_SIZE ), e( VECTOR_SIZE );
d.init();

start = clock();
for ( int i = 0 ; i < 10; i++ ) iCopy( e.array, d.array, VECTOR_SIZE-10 );
stop = clock();
taken = ( stop - start ) / 1000;
cerr << "time for iCopy : " << taken << endl;

start = clock();
for ( int i = 0; i < 10; i++ ) sCopy( e.array, d.array, VECTOR_SIZE-1 );
stop = clock();
taken = ( stop - start ) / 1000;
cerr << "time for sCopy : " << taken << endl;

start = clock();
for ( int i = 0 ; i < 10; i++ ) iCopy( e.array, d.array, VECTOR_SIZE-10 );
stop = clock();
taken = ( stop - start ) / 1000;
cerr << "time for iCopy : " << taken << endl;

start = clock();
for ( int i = 0; i < 10; i++ ) sCopy( e.array, d.array, VECTOR_SIZE-1 );
stop = clock();
taken = ( stop - start ) / 1000;
cerr << "time for sCopy : " << taken << endl;

return 0;
}
</code>

Thanks for any reply.

Regards,

Franky B.

推荐答案

fr ************ ***@ua.ac.be 写道:
你好,

我想知道这两种方法中哪一种最快:std :: copy,
包含在标准库中,还是手工编写的指针
副本?
你们有没有这方面的经验?
Hello,

I am wondering which of these two methods is the fastest: std::copy,
which is included in the standard library, or a manually written pointer
copy?
Do any of you have any experience with this?




是的。答案是:这取决于。在编译器,标志,库

实现等


经验法则:不要优化,除非你必须。寻找最简单的

解决方案,如果它被证明是一个瓶颈,那么试着找出原因。

-

WW aka Attila



Yes. The answer is: It depends. On the compiler, the flags, the library
implementation etc.

The rule of thumb: do not optimize, unless you have to. Go for the easiest
solution and if it proves to be a bottlneck, then try to find out the cause.

--
WW aka Attila


On Sun,2003年9月14日23:40:23 +0200,franky.backeljauw写道:
On Sun, 14 Sep 2003 23:40:23 +0200, franky.backeljauw wrote:
您好,

我想知道这两种方法中哪一种最快:std :: copy,
包含在标准库中,还是手动编写的指针副本?
你们有没有这方面的经验?

朋友无效iCopy(元素* dst,const元素* src,int n)
{int i = n + 1; while(i - > dst ++ = * src ++;}
friend void sCopy(Element * dst,const Element * src,int n)
{std :: copy(src,src + n + 1) ,dst);}


我认为std :: copy将比手动编码的

副本更快/更快。或者,如果您的标准库实现很聪明

足以以编程方式确定Element是POD,std :: copy

可以简单地调用std :: memcpy,这可能是更快。

我认为库函数std :: copy会表现最佳,因为它是一个库函数,因此这个函数的编写者会知道最好的如何优化它...但是一些测试似乎表明我的指针复制功能可以做得更好,特别是当它的
循环被矢量化时。我使用了g ++和icc -O2,并且icc与-O2
-march = pentiumiii -xK -tpp6作为编译器选项。


YMMV。

start = clock();
for(int i = 0;我< 10; i ++)iCopy(e.array,d.array,VECTOR_SIZE-10);
stop = clock();
take =(stop - start)/ 1000;
cerr<< iCopy的时间: <<采取<< endl;
Hello,

I am wondering which of these two methods is the fastest: std::copy, which
is included in the standard library, or a manually written pointer copy?
Do any of you have any experience with this?

friend void iCopy( Element *dst, const Element *src, int n )
{ int i = n + 1; while ( i-- >dst++ = *src++; }
friend void sCopy( Element *dst, const Element *src, int n )
{ std::copy(src, src+n+1, dst ); }
I think that std::copy is going to be /at least/ as fast as the hand-coded
copy. Alternatively, if your standard library implementation is clever
enough to meta-programatically determine that Element is POD, std::copy
could simply call std::memcpy, which might be faster.
I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it ... but some tests seem to indicate
that my pointer copy function can do a lot better, especially when its
loop is vectorized. I have used g++ and icc with -O2, and icc with -O2
-march=pentiumiii -xK -tpp6 as compiler options.
YMMV.
start = clock();
for ( int i = 0 ; i < 10; i++ ) iCopy( e.array, d.array, VECTOR_SIZE-10 );
stop = clock();
taken = ( stop - start ) / 1000;
cerr << "time for iCopy : " << taken << endl;




你为什么除以1000?除以CLOCKS_PER_SEC或根本没有

。另外,std :: clock_t是不可或缺的;你可能想做什么

浮点除法。


你没有发布你的结果。当我使用GCC 3.3进行测试时,我基本上得到了两个副本相同的结果。


Josh



Why are you dividing by 1000? Either divide by CLOCKS_PER_SEC or nothing
at all. Also, std::clock_t is integral; you probably want to be doing
floating-point division.

You didn''t post your results. When I tested with GCC 3.3, I get basically
the same results for the two copies.

Josh

fr ************** *@ua.ac.be 写道:
fr***************@ua.ac.be wrote:
您好,

我想知道这两种方法中哪一种最快:std ::复制,哪些包含在标准库中,还是手工编写的指针副本?
你们有没有这方面的经验?

我会认为库函数std :: copy会表现最佳,因为它是一个库函数,因此这个函数的编写者会知道如何优化它


我我想你可能会发现它根本没有得到很好的优化。它

被设计成通用的,所以它可以应用于需要通过它们的赋值运算符复制的对象(其中一个按位复制将会破坏

对象非常可怕)。

...但是有些测试似乎表明我的指针复制功能可以做得更好,特别是当它的循环时是矢量化的。我使用g ++和icc和-O2,icc和-O2
-march = pentiumiii -xK -tpp6作为编译器选项。

或者还有其他方法来看待这个吗?为了完整起见,我包括我的
测试程序:

< code>
#include< ctime>
#include< algorithm>
#包括< iostream>

使用命名空间std;

typedef double Element;

类Vector
{
公开:

Vector(unsigned int newsize){array = new Element [newsize]; size = newsize; }
~Vector(){if(array)delete(array),array = 0; }


这个坏了。删除不是一个功能,它是一个运营商。你还要为你使用的''new''运算符使用正确的''delete''运算符,

所以你需要在这里使用delete []。


至于剩下的部分,看起来很不必要......删除工作正常
带有空指针的
,''array''正在尽快消失当

析构函数完成时,将其设置为0是没有意义的。

void init(){for(unsigned int i = 0; i< size; i ++)array [i] =我; }
void print(){for(unsigned int i = 0; i< size; i ++)cout<< array [i]<< "英寸; cout<< ENDL;朋友void iCopy(Element * dst,const Element * src,int n){int i = n + 1;}} while(i--> 0)* dst ++ = * src ++; } void void sCopy(Element * dst,const Element * src,int n){std :: copy(src,src + n + 1,dst); }
Hello,

I am wondering which of these two methods is the fastest: std::copy, which
is included in the standard library, or a manually written pointer copy?
Do any of you have any experience with this?

I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it
I think you would probably find that it is not very optimized at all. It
is designed to be generic, so it can apply to objects that need to be
copied via their assignment operators (where a bit-wise copy would break
the objects horribly).
... but some tests seem to indicate
that my pointer copy function can do a lot better, especially when its
loop is vectorized. I have used g++ and icc with -O2, and icc with -O2
-march=pentiumiii -xK -tpp6 as compiler options.

Or is there another way to look at this? For completeness, I include my
test program:

<code>
#include <ctime>
#include <algorithm>
#include <iostream>

using namespace std;

typedef double Element;

class Vector
{
public:

Vector( unsigned int newsize ) { array = new Element[ newsize ]; size = newsize; }
~Vector() { if ( array ) delete( array ), array = 0; }
This is broken. delete is not a function, it''s an operator. You also
must use the proper ''delete'' operator for the ''new'' operator you used,
so you need to use delete[] here.

As for the rest of it, it seems quite unnecessary... delete works fine
with null pointers, and ''array'' is disappearing as soon as the
destructor completes, so setting it to 0 is rather pointless.

void init() { for ( unsigned int i = 0; i<size; i++ ) array[ i ] = i; }
void print() { for ( unsigned int i = 0; i<size; i++ ) cout << array[ i ] << " "; cout << endl; }

friend void iCopy( Element *dst, const Element *src, int n ) { int i = n + 1; while ( i-- > 0 ) *dst++ = *src++; }
friend void sCopy( Element *dst, const Element *src, int n ) { std::copy( src, src+n+1, dst ); }




这很奇怪(把朋友的定义放在课堂里),我是

不确定它是否合法。


此外,您使用std :: copy复制了太多元素。

的第二个参数应该是src + n。


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。



This is bizarre (putting friend definitions inside the class), and I''m
not sure if it''s legal.

Also, you are copying one too many elements with your std::copy. The
second argument should be src+n.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于std :: copy与指针副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆