Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 2nd, 2005, 09:05 AM
 jjj93421
Registered User

Join Date: Apr 2004
Posts: 342
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Help with 2000 element array

I need to set every element of a 2000 element array equal to 1 in the fastest time possible. Is the only way a For loop in which it loops about 2000 times ?

Code:
```
for (int a=0; a<=2000; a++){

arrayelement[a]=1;

}```

That's how I currently do it. Is there a better way ? I couldn't find one.

#2
September 2nd, 2005, 09:29 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 19 m 15 sec
Reputation Power: 1827
> for (int a=0; a<=2000; a++)
A 2000 element array is for (int a=0; a<2000; a++)

> Is there a better way ?
Whilst there no doubt slightly faster ways (not by much), I wouldn't regard them as being better. This kind of loop isn't going to take much time in any event, so premature optimisation here is really the wrong thing to do.
http://vision.eng.shu.ac.uk/bala/c/c/optimisation/1/optimization.html

But if you insist, consider something like
Code:
```int *p = array;
for ( i = 0 ; i < 500 ; i++ ) {
*p++ = 1;
*p++ = 1;
*p++ = 1;
*p++ = 1;
}```

#3
September 2nd, 2005, 10:05 AM
 jjj93421
Registered User

Join Date: Apr 2004
Posts: 342
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Thanks. I've never seen that before. I'll see if I can tell any differences. I was just trying out different things and none of my ideas worked so I thought I'd ask here.

#4
September 2nd, 2005, 10:10 AM
 mwana mission
Contributing User

Join Date: May 2004
Location: UK
Posts: 38
Time spent in forums: 1 Day 7 h 58 m 44 sec
Reputation Power: 10
Wont work as it sets a character

#5
September 2nd, 2005, 10:15 AM
 mike65535
um, Hello?

Join Date: Nov 2004
Location: FN23fc
Posts: 719
Time spent in forums: 1 Week 5 Days 1 h 42 m 56 sec
Reputation Power: 160
What's nice about the OP's method is that it's straight forward and obvious. Not much worse than someone trying to use old code and having to try to figure out some undocumented (uncommented) strange-looking routine.

One thing to ask yourself when considering optimizing is "How often - and when - is this piece of code called?", not just "How long does this piece of code take to run?"
__________________
"America's abundance was created not by public sacrifices to "the common good," but by the productive genius
of free men who pursued their own personal interests and the making of their own private fortunes. They did not
starve the people to pay for America's industrialization. They gave the people better jobs, higher wages and
cheaper goods with every new machine they invented, with every scientific discovery or technological advance --
and thus the whole country was moving forward and profiting, not suffering, every step of the way."
--Ayn Rand

#6
September 2nd, 2005, 10:16 AM
 ZuK
Contributing User

Join Date: Aug 2005
Posts: 580
Time spent in forums: 1 Month 2 Days 13 h 22 m 26 sec
Reputation Power: 97
Quote:
 Originally Posted by mwana mission Try memset.

not quite. works for chars only.
K

#7
September 2nd, 2005, 11:01 AM
 jim mcnamara
......@.........

Join Date: Jun 2004
Posts: 1,345
Time spent in forums: 1 Week 4 Days 39 m 18 sec
Reputation Power: 55
Just as a note:
you can initialize an array when you declare it. If this even relevant:
Code:
`int array[2000]={1};`

which sets all elements to one.

Basically, if you want to worry about performance:
1. get it working perfectly first.
2. If it runs slowly, then it's time to worry. Try a profiler first. Don't guess.

#8
September 2nd, 2005, 11:05 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 19 m 15 sec
Reputation Power: 1827
> which sets all elements to one.
No it doesn't, it only sets the first element to 1.
Everything else is set to 0.

#9
September 2nd, 2005, 11:07 AM
 jim mcnamara
......@.........

Join Date: Jun 2004
Posts: 1,345
Time spent in forums: 1 Week 4 Days 39 m 18 sec
Reputation Power: 55
Oh. to answer your first question - this is fast, how fast depends on your system architecture. Keep an array parked in memory that is already correctly initialized. memcpy it into the working array.

Code:
```int keep[2000]={1};
int array[2000]={1};

/* ........... we want to re-init array[] here:*/
memcpy(array,keep,sizeof(int)*2000);```

#10
September 2nd, 2005, 10:52 PM
 jjj93421
Registered User

Join Date: Apr 2004
Posts: 342
Time spent in forums: 2 Days 3 h 34 m
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
Hi Jim. Yeah, I don't think the "int array[2000]=1 " method works because I tried it. I wish it did. I think I've decided on the memcpy solution. Thanks everyone for your input.

#11
September 6th, 2005, 03:16 PM
 CASSANOVA
Contributing User

Join Date: Jul 2005
Location: KOLKATA
Posts: 39
Time spent in forums: 11 h 41 m 50 sec
Reputation Power: 0
pointer

Using pointer might help us to make things a bit faster.

#12
September 6th, 2005, 04:45 PM
 jim mcnamara
......@.........

Join Date: Jun 2004
Posts: 1,345
Time spent in forums: 1 Week 4 Days 39 m 18 sec
Reputation Power: 55

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help with 2000 element array