Thats impossible.
It will be possible if the processor is 100*sizeof(int) bit.
But unfortunately - as far as i know we have only till 128 bit processors.
Main Topics
Browse All TopicsIs there any way that I can initialize, say an int array of size 100 to all 1 in O(1) time?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
"we have only till 128 bit processors. "
completely wrong
cellular computers
artificial neuronal networks
some more standard parallel machines
examples :
http://www.freenetpages.co
http://www.cs.nmsu.edu/~pf
plus :
DIVA wideWord processor
"The combination of the execution control unit and
WideWord datapath is regarded as the WideWord
Processor. This component enables superword-level
parallelism [11] on wide words of 256 bits[...]"
http://www.isi.edu/~draper
http://www.globaltechnosca
and
the Ax36 Video DSP uses a VLIW of 256 bits
http://www.omdi.com/downlo
and
I avoided to mention the existing 256 bits graphics chips :D
Is it "possible"? A rather dopey question if you ask me.
I suspect the question writer meant "using typical CPU instructions", and the answer is NO.
That's because most computers have a limited path to memory, which means that they can only
send results to memory in N bytes at once, where N is typically 1,2,4,8, or 16. I don't know
of any computer with a 100-byte path to memory, so initializing big arrays has to be done serially
in time, which results in a O(n) process.
BUT there are many other things hooked up to memory that may be capable of doing this:
(a) There may be a DMA controller chip, that if you tell it "DMA 100 words from nonexistent device XX",
it will dump 100 words of zeroes into your array. This usually happens in O(1) time, i.e. it doesnt take any more CPU
time to ask for 200 word transfer than it does for 100 words.
(b) There may be a graphics engine around (most VGA cards made in the last 5 years have one) which can do this in O(1).
(c) There may be a cache-controller chip that can do this in O(1), plus it can do fancy things, impossible fromt he CPU side, like address a whole row or column at once.
(d) There may be a CPU helper chip, like a AMD "Nortbridge" or "Southbridge", which can do really low-level memory tricks, such as turn off the power or refresh strobes to the memory chips, effectively initializing the whole SIMM at once.
(e) Most any other peripheral card that can become a bus-master, such as some sound cards, most USB or firewire cards,
most SCSI cards, they can potentially do a O(1) memory zap.
(f) Some virtual memory systems let you say "discard page at address XXXXX", then the memory-mapping hardware can just flip a very few bits and discard (and effectively zero) 4K at a time.
But one suspects the question writer wasnt this clever.. they're just fishing for the usual "No, zeroing memory takes O(n) time." answer.
Regards,
grg99
There is additional discussion on this very same problem here:
http://www-tcsn.experts-ex
#define array_size 100
int array[array_size];
int back[array_size];
int fwd[array_size];
int top= -1;
initialize_array(){
top = -1; /* mark array as empty */
}
insert_into_array(int index, int value){
if( index < 0 || index >= array_size ){
error("index out of bounds");
}
if( 0 <= back[index] && back[index] <= top && fwd[back[index]] == index ){
/* array[index] contains valid data */
}else{
/* array[index] is empty, mark index as valid */
top+=1;
fwd[top]=index;
back[index]=top;
}
array[index]=value;
}
int read_from_array(int index){
if( index < 0 || index >= array_size ){
error("index out of bounds");
}
if( 0 <= back[index] && back[index] < top && fwd[back[index]] == index ){
/* array[index] contains valid data */
return(array[index]);
}else{
error("no data at that index");
}
}
#define array_size 100
int array[array_size];
int back[array_size];
int fwd[array_size];
int top= -1;
int initvalue = 1;
initialize_array(int value){
top = -1; /* mark array as all initvalue */
initvalue = value;
}
insert_into_array(int index, int value){
if( index < 0 || index >= array_size ){
error("index out of bounds");
}
if( 0 <= back[index] && back[index] <= top && fwd[back[index]] == index ){
/* array[index] contains valid data */
}else{
/* array[index] is empty, mark index as valid */
top+=1;
fwd[top]=index;
back[index]=top;
}
array[index]=value;
}
int read_from_array(int index){
if( index < 0 || index >= array_size ){
error("index out of bounds");
}
if( 0 <= back[index] && back[index] < top && fwd[back[index]] == index ){
/* array[index] contains valid data */
return(array[index]);
}else{
return initvalue;
}
}
grg99 says:
> One sneaky way that might SEEM to be O(1) is to declare the array as a static global array.
> static variables are by C's definition, initialized to zero.
>
> But this is done by the startup code in C0.OBJ, and it may be done quickly, but it's still most likely done by
> a loop, so it's going to be O(n). It's just that it's a HIDDEN loop, but it's still most likely O(n).
You could try a static global array with explicit iniitialization. That would get initialized at compile
time, rather than load time (note that one of the array elements is not zero):
static int a[100] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 200,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
>You could try a static global array with explicit iniitialization. That would get initialized at compile
>time, rather than load time (note that one of the array elements is not zero):
I forgot that case!
It souinds "free", but actually it's worse than most options, as then the zeroes have to be read from disk, which is going to be a BIGFACTOR * O(n) process,
at least 1,000 times slower than having the CPU zero out memory. But it's "invisible" as you don't need an explicit loop
in your code.
No free lunch here either.
Sorry,
grg99
> It sounds "free", but actually it's worse than most options, as then the zeroes have to be
> read from disk, which is going to be a BIGFACTOR * O(n) process, at least 1,000 times slower
> than having the CPU zero out memory.
Actually for such a small program, the extra 400 bytes will likely land in the same 4k page as
the rest of the program (or its data at least). It really doesn't take any longer to read 400
bytes from a file than to read 4 bytes, because the data is read from the file in chunks that
are either sector sized, cluster sized, or VM page sized (depending on the OS). So you only
have to worry if the initialized data spills into an additional "chunk" (which probably resides
in the drive's 2MB-8MB read-ahead cache anyway).
But even so, all our suggestions just push the work further back in the timeline. It still has
to be done some time. Although doing the work at compile time amortizes its impact across
all future runs.
>Actually for such a small program, the extra 400 bytes will likely land in the same 4k page as
>the rest of the program (or its data at least).
Ah yes, the cat and rat infinite factory. You can breed an infinite number of cats,
as there's always at least one rat around, and the cats can eat that rat.
If the rats go hungry, just chop up some cats and feed them to the rats.
Well, not quite, you're just hoping the rat won't miss having its tail eaten. But what about the next cat?
It's still O(n) times a BIG factor. My hard disk can read zeroes at about 5MB/sec plus about 20msec to get started. My CPU can
zero memory about 500 times as fast as that, with zero initial delay. So we're talking about O(n) * 500.
But you're right, sometimes you'll get the first few cats fed for free.
Regards,
grg99
This is not the solution to your problem, but maybe you can still use it somehow: :-)
-you have a zero-based integer array
int[] numbers
-use element numbers[0] as index multiplicator when accessing array-elements with index>0
This means that instead of:
number[index] to access element at position index>0
you would use
number[number[0] * index]
or maybe
number[number[0] & index]
- if you use * operator then assign number[0]=1
- if you use & bitwise-AND operator then assign number[0]=-1
- when you need to set all values to zero, just assign number[0]=0
As a result, all lookups
number[number[0] * index] == number[0 * index] == number[0] == 0
also
number[number[0] & index] == number[0 & index] == number[0] == 0
Convenient, but still useless :-)
...useless because when you wish to assign a non-null value to some array-element, you still have to fill zeros at all positions :-)
Greetings,
</ntr> :)
>This is not the solution to your problem, but maybe you can still use it somehow: :-)
You're darned tootin' it's not a solution!
It's worse than O(n), as every reference to the array is going to incur the quasi-initialized overhead.
(I'm assuming as in most cases each array element is going to be accessed more than once).
Of course it is going to incurr overhead, I was not talking about array i/o but about the zeroing operation :-)
Otherwise, it is pointless to discuss array-initialization in O(1) if you are not willing to sacrafice something somewhere.
Personally, I would (if possible at all) redesign the accessing logic, so a hash-map or a linked-list would be used instead of an array, and there you would anyway have to meet the overhead problem, even if initialization would be possible in O(1).
As J.R. said - "Sometimes you've gotta lose to win" :-) (pardon my bad English)
Greetings,
</ntr> :)
You can initialize an array on O(1) incurring only O(1) overhead on all array accesses
http://mmlab.ceid.upatras.
Business Accounts
Answer for Membership
by: stsanzPosted on 2003-09-19 at 02:03:29ID: 9392034
By definition, the memory architecture of computers leads to O(n) when initializing array of size n.
No bypass.
But you can optimize the process, by accessing the memory in blocks of machine word size.
For example, in C, memset standard function is optimized this way.
What language do you use?