473,624 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick way to zero array

Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?

May 12 '06 #1
14 52462
memset( lnscores, 0, chunksize )

May 12 '06 #2
ro**********@gm ail.com opined:
Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I
am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker
way to do this? I know there must be a trick since this array is one
big contiguous chunk of memory right?


If your array has file scope you don't even have to zero it yourself.
All such variables get zeroed at startup. Otherwise, declare it and
initialise thus:

int lnscores[WHATEVER_SIZE] = {0};

If you have to re-zero it afterwards, you could use `memset()`.

--
Worlds are conquered, galaxies destroyed -- but a woman is always a
woman.
-- Kirk, "Conscience of the King", stardate unknown

<http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>

May 12 '06 #3
On 2006-05-12, ro**********@gm ail.com <ro**********@g mail.com> wrote:
Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?


memset(lnscores , 0, chunksize * sizeof lnscores[0]);

Assuming 0 is represented by 0... what type is lnscores?
May 12 '06 #4
ro**********@gm ail.com schrieb:
Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?


As you did not tell us what type lnscores has, there is no
definite "quickest" way.
If lnscores[count] = 0 means that all bits of lnscores[count] are
set to zero, you can use memset() to zero out all bits.

Otherwise, you can use memcpy():
-zero out lnscores[0]
-memcpy() lnscores[0] to lnscores[1]
-memcpy() lnscores[0] and lnscores[1] to lnscores[2] and lnscores[3]
....
-memcpy() lnscores[0], ..., and lnscores[16777215] to
lnscores[16777215], ..., and lnscores[33554431]
- Last step (no doubling):
memcpy() lnscores[0], ..., and lnscores[6445567] to
lnscores[33554431], ..., and lnscores[39999999]

This _may_ be quicker than the loop itself. It may be slower if
your compiler recognizes the "zero out" pattern and does something
intelligent.

Try it out and measure.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 12 '06 #5
if not all elements will be used, there are some methods to just zero out as
fewer as needed
<ro**********@g mail.com> wrote in message
news:11******** **************@ d71g2000cwd.goo glegroups.com.. .
Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?

May 12 '06 #6
ro**********@gm ail.com wrote:
Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?


Why do you have to zero the array? Are you only doing something to a
few elements at each iteration? While there may be a fast C function
that zeros arrays, unless you have a parallel machine you can't zero
all cells at once, so this is likely to be costly. I suggest you
seek a better algorithm--for example, whenever you update an element
in an iteration, before you leave that loop, zero that element, so
the whole array returns to the zeroed state.

--
Julian V. Noble
Professor Emeritus of Physics
University of Virginia
May 12 '06 #7
Julian V. Noble wrote:

Why do you have to zero the array?
Zeroing out any piece of allocated memory is necessary to avoid
unforeseen results, which are annoying most of the time.
Are you only doing something to a
few elements at each iteration? While there may be a fast C function
that zeros arrays, unless you have a parallel machine you can't zero
all cells at once, so this is likely to be costly. I suggest you
seek a better algorithm--for example, whenever you update an element
in an iteration, before you leave that loop, zero that element, so
the whole array returns to the zeroed state.


That's a good idea.

You can use "calloc" for allocating for the first time. It already
returns memory region fillled with zero. Then memset or bzero are also
good option.
I mostly use bzero for such things.

BTW, 40 million elements array, are u doing image processing or some
other kind of signal processing?

cheers,

--Himanshu

--
+-----------------------------------+
| Himanshu Singh Chauhan |
| MCA (Final Year) |
| I.G. National Open University |
| Delhi (India) |
| |
| Contact: hs********@gmai l.com |
+-----------------------------------+
May 12 '06 #8
Himanshu Chauhan <hs********@gma il.com> writes:
Julian V. Noble wrote:
Why do you have to zero the array?
Zeroing out any piece of allocated memory is necessary to avoid
unforeseen results, which are annoying most of the time.


It's not always necessary. For example, if you can keep track of
which elements of the array you're currently using, you don't have to
worry about what's in the other elements. The best approach depends
on what you're actually doing with the array.

[...] You can use "calloc" for allocating for the first time. It already
returns memory region fillled with zero. Then memset or bzero are also
good option.
I mostly use bzero for such things.


calloc() and memset() will set the array contents to all-bits-zero.
If you have an array of integers, it will set each element to 0. If
it's an array of some pointer or floating-point type, it will
*probably* do so, but it's no guaranteed (the language doesn't
guarantee that a floating-point 0.0 is represented as all-bits-zero).

bzero() is non-standard, and might not exist on all systems.

It's not obvious that memset() will be faster than a loop explicitly
setting each element to zero. You should measure the performance of
the code.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 12 '06 #9
Himanshu Chauhan wrote:
Julian V. Noble wrote:

Why do you have to zero the array?
Zeroing out any piece of allocated memory is necessary to avoid
unforeseen results, which are annoying most of the time.


Depends on whether all bits zero is a valid initialiser. It is not
necessarily valid for floats, doubles or pointers.
Are you only doing something to a
few elements at each iteration? While there may be a fast C function
that zeros arrays, unless you have a parallel machine you can't zero
all cells at once, so this is likely to be costly. I suggest you
seek a better algorithm--for example, whenever you update an element
in an iteration, before you leave that loop, zero that element, so
the whole array returns to the zeroed state.


That's a good idea.

You can use "calloc" for allocating for the first time. It already
returns memory region fillled with zero. Then memset or bzero are also
good option.
I mostly use bzero for such things.


Why choose the non-standard bzero when there is a perfectly standard memset?
BTW, 40 million elements array, are u doing image processing or some
other kind of signal processing?


Please don't use stupid contractions like u for you.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
8304
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
13
8728
by: DazedAndConfused | last post by:
Is there a quick way to fill an array with zeros or make all elements empty/nothing?
22
2273
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
3
1719
by: Brian | last post by:
A quick question here - What can be achieved in IL which is not possible in C# ? o Creation of an ArrayList o Creation of a Dictionary o Creation of a two dimensional array o Creation of a non-zero based array Please let me know what is the right answer. Many thanks.
0
367
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array in C since there are no first class array types. But one can return a pointer to an incomplete array via the illegal but widely supported zero array struct hack: #include <stdlib.h> typedef struct byte_vector_t byte_vector_t;
10
2385
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Got a simple question. I am new to c# but this is not making me any sence. If i declare: string myStringArray = new string; How the heck could i fill it with more than one element? Regards
12
3972
by: rajus | last post by:
I want to store the (x,y) coordinates of about 10,000 points in a 2D array.How can I store them and retrieve them later?
9
1482
by: John B | last post by:
Hi all, Say I have the int 123456789 What would be the quickest/best way to convert it to int{1,2,3,4,5,6,7,8,9} What I came up with was to determine the largest factor of 10 (100M) divide by that, truncate decimals and that'd be the 1st number, subtract that number multiplied by the current factor, next smallest factor etc.. But it seems very roundabout, surely there'd be a better way :)
24
2177
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is this really valid c++ or c? where can I find out how this is implemented? Im concerned because I had a 3rd party library wrapper which was
0
8182
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8352
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7178
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6115
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4085
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2614
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1496
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.