返回信息流现在有一个字符串拷贝的函数
#include<iostream>
using namespace std;
void strcopy(char *s,char *r)//s目的字符串,r源字符串
{
int k=0;
while(r[k]!='\0')
{
s[k]=r[k];
k++;
}
s[k]='\0';
}
int main()
{
char a[]="hello world";
char b[20];
strcopy(b,a);
cout<<b<<endl;
}
现在的问题是为了提高效率,让这个函数一次拷贝四个字节,该怎么做?
这是一条镜像帖。来源:北邮人论坛 / cpp / #44283同步于 2010/9/27
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[讨论]一个字符串拷贝的问题
nevermadao
2010/9/27镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
memcpy?但是你还是要依次判断那个结束符\0啊
坐等大牛解答
【 在 nevermadao ([—哗]) 的大作中提到: 】
: 现在有一个字符串拷贝的函数
: #include<iostream>
: using namespace std;
: ...................
glibc里面的源码
#define UNALIGNED(x,y) (((unsigned long)x & (sizeof (unsigned long)-1)) ^ ((
unsigned long)y & (sizeof (unsigned long)-1)))
#define STRALIGN(x) (((unsigned long)x&3)?4-((unsigned long)x&3):0)
# define MKW(x) (x|x<<8|x<<16|x<<24)
# define GFC(x) ((x)&0xff)
# define INCSTR(x) do { x >>= 8; } while (0);
char *
strcpy (char *s1, const char *s2)
{
char *res = s1;
#ifdef WANT_SMALL_STRING_ROUTINES
while ((*s1++ = *s2++));
return (res);
#else
int tmp;
unsigned long l;
if (UNALIGNED(s1, s2)) {
while ((*s1++ = *s2++));
return (res);
}
if ((tmp = STRALIGN(s1))) {
while (tmp-- && (*s1++ = *s2++));
if (tmp != -1) return (res);
}
while (1) {
l = *(const unsigned long *) s2;
if (((l - MKW(0x1ul)) & ~l) & MKW(0x80ul)) {
while ((*s1++ = GFC(l))) INCSTR(l);
return (res);
}
*(unsigned long *) s1 = l;
s2 += sizeof(unsigned long);
s1 += sizeof(unsigned long);
}
#endif
}
【 在 nevermadao ([—哗]) 的大作中提到: 】
: 现在有一个字符串拷贝的函数
: #include<iostream>
: using namespace std;
: ...................
【 在 nevermadao 的大作中提到: 】
: 现在有一个字符串拷贝的函数
: #include<iostream>
: using namespace std;
: ...................
并行计算。。
sse应该可以满足。。