Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Bruno

fastest way to clean an array

This topic is 6206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys Do you guys know a fastest way to clean an bidimensional array like array[200][200], without a "for" loop = 0 ?? I''ve been looking at the function ZeroMemory, but i''m afraid i don''t know how to use it. Thanks Bruno

Share this post


Link to post
Share on other sites
Advertisement
Like this:

        
memset(array[0], 0, 40000 * sizeof(array[0]));


Of course, you can replace the sizeof with the size of the data type you are using.

-Jussi

"I want a place to hide, somewhere far from your side"


Edited by - Selkrank on October 6, 2000 6:55:22 AM

Share this post


Link to post
Share on other sites
that code you posted shouldn''t even compile.

    
//either this way

memset(array, 0, 40000 * sizeof(array[0]));

//or like this

memset(&array[0], 0, 40000 * sizeof(array[0]));


don''t call me picky

Share this post


Link to post
Share on other sites
quote:
Original post by Ridcully

that code you posted shouldn''t even compile.


Yes it should. If the array was of type char, the array''s data type would be char**, and array[0]''s data type would be char*, so array[0] is right.

So, either array[0] or &(array[0][0]).

-Jussi

"What have we done?
Who killed the sun?"

Share this post


Link to post
Share on other sites
and why not. its perfectly legal aint it?
my reply was to ridcully

Edited by - zedzeek on October 6, 2000 8:42:32 AM

Share this post


Link to post
Share on other sites
You use ZeroMemory like this:

        
ZeroMemory(pointertomemory, lengthtoset);
[/source]

ex.

[source]
int numbers[10];
ZeroMemory(&numbers, sizeof(numbers));


It is just like memset without the second parameter of what to set the memory to (It is actually just a macro for memset).

And for a bidimensional array is would be the same (I'm pretty sure)

-------------
cOnfuSed

Edited by - Confused on October 6, 2000 8:48:53 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Fastest way? MMX, baby.

memset() is indeed a very fast function in that it doesn''t copy byte by byte (at least, the Visual C version is). It sets all dword aligned dwords (dword aligned so that two writes are not needed in order to write one dword) and then goes to work on the lead and trailing bytes.

With the FPU you can set 8 bytes at a time instead of 4 but this is only faster if the data is cache-warmed.

You can also set 8 bytes of data at a time with the MMX and this is your target if you use the same alignment strategies of memset().

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Most implementations of memset are just a for-loop and is generally one of the _slowest_ ways to clear memory. The fastest way really depends on what system you''re talking about. Ideally you''d have some sort of vector unit that could write 4 or 8 bytes at a time, or a dma controller that can write constant values to memory. On the N64 it was faster to have the graphics system render a black quad to main memory. I guess for Intel systems, MMX would be best....

Share this post


Link to post
Share on other sites
argh!
how could i overlook that?
well, must be that damn headache i''ve got at the moment. probably i shouldn''t visit the board while i am ill.

sorry anyways
rid

Share this post


Link to post
Share on other sites
try this:

memset compiled to ASM is usually just a for loop MOVing 0 into every DWORD.

MOV is an expensive operation. Most ASM guru''s use X xor X to clear a register/memory position.

not sure how well this compiles in VC, but doing a large for loop and xor''ing every element with itself should be pretty fast.



actually, now that I think of it, im not sure how well this performs when accessing memory, because i''ve only used it on registers before, and C doesnt let you touch the registers.

heh... might actually turn out slower.

psuedo ASM:
mov ax, memory
xor ax
mov memory, ax

-or-
move memory,0


oh bah. ignore this entire post.

===============================================
If there is a witness to my little life,
To my tiny throes and struggles,
He sees a fool;
And it is not fine for gods to menace fools.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!