问题描述
嗨。
我有一个有趣的问题。下面介绍的C程序需要
才能在我的机器上复制128 MB数据。然而我知道
机器可以更快地获得
,因为在DOS提示下完成复制只需要4秒钟就可以完成
/>
128 MB副本(副本既是读写又是。)。那么为什么这个
比如3x
更糟糕呢?
/ *从b-开始复制b的len数字> StartOffs从a-> StartOffs开始。
* /
int DiskInt_Copy(DiskInt * a,DiskInt * b ,长len)
{
长DigitsRemaining,BufferLen,MaxBuffer;
/ *设置长度* /
DigitsRemaining = len;
MaxBuffer = DISK_BUF_SIZE / sizeof(DIGIT);
BufferLen = MaxBuffer;
if(DigitsRemaining< ; 0)返回(ERR_SUCCESS);
if(DigitsRemaining< MaxBuffer)
BufferLen = DigitsRemaining;
/ *复制数字* /
DiskInt_SeekDigit(a,0);
DiskInt_SeekDigit(b,0);
而(DigitsRemaining)
{
fread(diskbuf1,BufferLen,sizeof(DIGIT),b-> fd);
fwrite(diskbuf1,BufferLen,sizeof(DIGIT),a - > fd);
DigitsRemaining - = BufferLen;
if(DigitsRemaining< MaxBuffer )
BufferLen = DigitsRemaining;
}
/ *完成! * /
DiskInt_SeekDigit(a,0);
返回(ERR_SUCCESS);
}
两个文件a->> fd和b-> fd用wb +打开。 (读/写,
二进制)。
这里,DIGIT定义为unsigned long和DISK_BUF_SIZE
等于1,048,576。
可能有几个原因。
试试这个
void copy(char * in,char * out)
{
FILE * fpin = fopen(in,rb);
FILE * fpout = fopen(out," wb");
int ch;
断言(fpin&& fpout);
while((ch = getc(fpin))!= EOF)
putc(ch,fpout);
fclose(fpin);
fclose(fpout);
}
这将告诉你机器可以复制128 MB的速度,使用标准库提供的默认
缓冲。运行你自己的缓冲
计划超过顶部会降低速度,因为缓存效果和
之类的。
-
免费游戏和编程好东西。
可能有几个原因。
试试这个
void copy (char * in,char * out)
{
FILE * fpin = fopen(in," rb);
FILE * fpout = fopen(out," wb");
int ch;
断言(fpin&& fpout);
while((ch = getc(fpin))!= EOF)
putc(ch,fpout);
fclose(fpin);
fclose(fpout);
}
这将告诉你机器可以多快复制128 MB,使用默认
缓冲由标准库提供。运行你自己的缓冲
计划超过顶部会降低速度,因为缓存效果和
之类的。
它仍然需要相同的~13秒。那么为什么Windows's
复制呢?命令更快?
可能有几个原因。
试试这个
void copy (char * in,char * out)
{
FILE * fpin = fopen(in," rb);
FILE * fpout = fopen(out," wb");
int ch;
断言(fpin&& fpout);
while((ch = getc(fpin))!= EOF)
putc(ch,fpout);
fclose(fpin);
fclose(fpout);
}
这会告诉你机器的速度有多快$ / b $ b可以复制128 MB,使用标准库提供的默认
缓冲。
运行你自己的缓冲
计划超过顶部将由于缓存效应等原因,往往会降低速度,降低b $ b。
它仍然需要相同的~13秒。那么为什么Windows's
复制呢?命令走得更快?
它有可能使用一些有效的操作码
,它们与任何C代码构造都不直接相关。 br />
-
pete
Hi.
I have an interesting problem. The C program presented below takes
around
12 seconds to copy 128 MB of data on my machine. Yet I know the
machine can go
faster since a copying done at a DOS prompt takes only 4 seconds to do
the same
128 MB copy (and a copy is both a read and write.). So why is this
thing like 3x
worse, anyway?
/* Copy len digits of b starting at b->StartOffs to
* a starting at a->StartOffs.
*/
int DiskInt_Copy(DiskInt *a, DiskInt *b, long len)
{
long DigitsRemaining, BufferLen, MaxBuffer;
/* Set up lengths */
DigitsRemaining = len;
MaxBuffer = DISK_BUF_SIZE/sizeof(DIGIT);
BufferLen = MaxBuffer;
if(DigitsRemaining < 0) return(ERR_SUCCESS);
if(DigitsRemaining < MaxBuffer)
BufferLen = DigitsRemaining;
/* Copy digits */
DiskInt_SeekDigit(a, 0);
DiskInt_SeekDigit(b, 0);
while(DigitsRemaining)
{
fread(diskbuf1, BufferLen, sizeof(DIGIT), b->fd);
fwrite(diskbuf1, BufferLen, sizeof(DIGIT), a->fd);
DigitsRemaining -= BufferLen;
if(DigitsRemaining < MaxBuffer)
BufferLen = DigitsRemaining;
}
/* Done! */
DiskInt_SeekDigit(a, 0);
return(ERR_SUCCESS);
}
Both files, a->fd and b->fd are opened with "wb+" (read/write,
binary).
Here, "DIGIT" is defined as "unsigned long", and "DISK_BUF_SIZE"
equals 1,048,576.
There could be several reasons.
Try this
void copy(char *in, char *out)
{
FILE *fpin = fopen(in, "rb);
FILE *fpout = fopen(out, "wb");
int ch;
assert(fpin && fpout);
while( (ch = getc(fpin)) != EOF)
putc(ch, fpout);
fclose(fpin);
fclose(fpout);
}
That will tell you how fast the machine can copy 128 MB, using default
buffering as provided by the standard library. Running you own buffering
scheme over the top will tend to slow things down, due to cache effects and
the like.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
There could be several reasons.
Try this
void copy(char *in, char *out)
{
FILE *fpin = fopen(in, "rb);
FILE *fpout = fopen(out, "wb");
int ch;
assert(fpin && fpout);
while( (ch = getc(fpin)) != EOF)
putc(ch, fpout);
fclose(fpin);
fclose(fpout);
}
That will tell you how fast the machine can copy 128 MB, using default
buffering as provided by the standard library. Running you own buffering
scheme over the top will tend to slow things down, due to cache effects and
the like.
It still takes the same ~13 sec. So then why does Windows''s
"copy" command go faster?
There could be several reasons.
Try this
void copy(char *in, char *out)
{
FILE *fpin = fopen(in, "rb);
FILE *fpout = fopen(out, "wb");
int ch;
assert(fpin && fpout);
while( (ch = getc(fpin)) != EOF)
putc(ch, fpout);
fclose(fpin);
fclose(fpout);
}
That will tell you how fast the machine
can copy 128 MB, using default
buffering as provided by the standard library.
Running you own buffering
scheme over the top will tend to slow things down,
due to cache effects and the like.
It still takes the same ~13 sec. So then why does Windows''s
"copy" command go faster?
There''s a chance that it might use some efficient opcodes
that don''t directly correlate to any C code constructs.
--
pete
这篇关于在C程序中缓慢复制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!