本文介绍了Memset比简单循环更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




剂量任何人在这里向我解释为什么memset会比

简单循环更快。我对此表示怀疑!


在一个int数组场景中:


int array [10];


for(int i = 0; i< 10; i ++)//十个循环

array [i] = 0;





memset(数组,0,sizeof(数组));


那么,memset会在里面做什么?这是MS c-run-time的片段

代码:


void * __cdecl memset(

void * dst,

int val,

size_t count



{

void * start = dst;


#if定义(_M_MRX000)||定义(_M_ALPHA)||定义的(_M_PPC)||
定义的
(_M_IA64)

{

extern void RtlFillMemory(void *,size_t count,char);


RtlFillMemory(dst,count,(char)val);

}

#else / * defined(_M_MRX000)||定义(_M_ALPHA)||定义

(_M_PPC)||定义(_M_IA64)* /

while(count--){//在这里观看

!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!

*(char *)dst =(char)val;

dst =(char *)dst + 1;

}

#endif / *已定义(_M_MRX000)||定义(_M_ALPHA)||定义

(_M_PPC)||定义(_M_IA64)* /


返回(开始);

}


memset初始化一块内存逐字节。所以,while循环

将执行4 * 10次!

我感到困惑。为什么人们仍然相信memset比循环更快

并且适用于大多数场景。

感谢您的帮助!

Hi,

dose anybody here explain to me why memset would be faster than a
simple loop. I doubt about it!

In an int array scenario:

int array[10];

for(int i=0;i<10;i++) //ten loops
array[i]=0;

or

memset(array,0,sizeof(array));

So, what will memset do inside? Here is a snippet from MS c-run-time
codes:

void * __cdecl memset (
void *dst,
int val,
size_t count
)
{
void *start = dst;

#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) ||
defined (_M_IA64)
{
extern void RtlFillMemory( void *, size_t count, char );

RtlFillMemory( dst, count, (char)val );
}
#else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined
(_M_PPC) || defined (_M_IA64) */
while (count--) { //Watch
here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!
*(char *)dst = (char)val;
dst = (char *)dst + 1;
}
#endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined
(_M_PPC) || defined (_M_IA64) */

return(start);
}

memset initializes a block of memory byte by byte. So, the while loop
will be executed 4*10 times!
I got confused. Why people still believe memset is faster than loop
and pertains most of scenarios.
Thank you for your help!

推荐答案



`memset`由实现提供。它可能会使用

许多狡猾的实施技巧 - 无论是什么

原因 - 默认情况下可能不适用普通

C代码。


它是否/实际/更快将取决于所有

种类的东西。

`memset` is provided by the implementation. It may use
lots of Cunning Implementation Tricks that -- for whatever
reason -- might not be applied by default to ordinary
C code.

Whether or not it''s /actually/ faster will depend on all
sorts of things.



怀疑是好的。我认为。嗯,我不确定。

Doubt is good. I think. Well, I''m not sure.



这取决于。

That depends.



(fx:snip #ifdef)

(fx:snip #ifdef)



(fx:snip #else)

(fx:snip #else)



(fx:snip #endif)

(fx:snip #endif)



Urm,您是否注意到在适当的架构上发生了对RtlFillMemory的调用




你也是/假设/编译器没有用内联代码实现

`mexset`,这是允许的。也许

原始调用

Urm, did you not notice the call to RtlFillMemory which happends
on suitable architectures?

Also you''re /assuming/ that the compiler doesn''t implement
`memset` with inline code, which it''s allowed to. Perhaps
the original call



被10个移动整数指令取代。

was replaced by 10 move-integer instructions.



这里真相的实质是测量仪器而不是

投机。


- -

Chris Dollin

RIP John" BNF,Fortran,FP" Backus 3Dec1924 - 17Mar2007

The essence of the truth here is measureument as opposed to
speculation.

--
Chris Dollin
RIP John "BNF, Fortran, FP" Backus 3Dec1924 - 17Mar2007




被10个移动整数指令取代。


was replaced by 10 move-integer instructions.



制作10条清晰整数指令。


-

Chris移动零" Dollin

RIP John" BNF,Fortran,FP" Backus 3Dec1924 - 17Mar2007

Make that 10 clear-integer instructions.

--
Chris "moving zero" Dollin
RIP John "BNF, Fortran, FP" Backus 3Dec1924 - 17Mar2007




不能保证memset()更快,它可能只是一些

观察一些人有制作。如果它更快,它可能是由于使用一些经过精心调整的方法实现的,而b / b是利用程序正在运行的处理器的某些功能的实现
$ b编译器在编译循环时可能无法找到的$ b。

但是并非必须如此,这是一个多好的问题

一方面是memset()的实现,另一方面是编译器的上帝是怎样的另一方面(并且编译器甚至可以聪明到

通过一次memset()调用来替换循环,并且你的速度会有所有的差异;-)。

There''s no guarantee that memset() is faster, it''s probably just some
observation a number of people have made. And if it is faster it is
probably due to the implementation using some carefully tuned method
that exploits some features of the processor the program is running
on which the compiler may not be able to find when it compiles the loop.
But that doesn''t has to be the case, it''s a question of how good the
implementation of memset() is on the one hand and how god the compiler
is on the other hand (and the compiler could even be clever enough to
replace the loop by a single call of memset() and there goes all your
difference in speed;-).



这只是众多实现中的一种,你不能通过查看某一个来推断任何

一般性陈述。而且,当你近距离观察时,你会看到
,memset()似乎以不同的方式实现

,具体取决于架构,所以你可以''' t $ / b $ b甚至可以说如何为MS c-run-time实现memset()。但对于MS c-run-time,只有

在某种架构上。

That''s just one of many implementations and you can''t deduce any
general statement from looking at a certain one. And, as you will
notice when you have a close look, memset() seems to be implemented
in a different way depending on the architecture, so you can''t
even say how memset() is implemented for "MS c-run-time" but only
for "MS c-run-time" on a certain architecture.



这不是一个相信的问题。你必须仔细测量你正在使用的实现和架构的

行为。


问候,Jens

-

\ Jens Thoms Toerring ___

\ __________________________ http://toerring.de


这篇关于Memset比简单循环更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:54