Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Speed of finding a size of an array.

Reply
Thread Tools

Speed of finding a size of an array.

 
 
Annajiat
Guest
Posts: n/a
 
      12-09-2005
Hi,
a)
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

b) Is there any faster alternative to initializing memory?

c) What is the difference between comp.lang.c and alt.comp.lang.c ?

d) Is there any performance difference in local and global array?

Thanking you
আন্নািজয়াত আলীম রােসল
Annajiat Alim Rasel
Secretary
BUCC
BRAC University Computer Club
BUCC: http://groups-beta.google.com/group/bucc
BUCC Programming Contest Wing:
http://groups.yahoo.com/group/buacm
http://groups-beta.google.com/group/buacm

http://www.velocityreviews.com/forums/(E-Mail Removed)
(E-Mail Removed)
(E-Mail Removed)

Monga Crisis:
http://adnan.phpxperts.com/monga/
http://adnan.phpxperts.com/monga/feedback.php

Quote: I don't talk. I don't talk much. However, when I talk, I forget
to stop.

 
Reply With Quote
 
 
 
 
Laurent Deniau
Guest
Posts: n/a
 
      12-09-2005
Annajiat wrote:
> Hi,
> a)
> int grid[1010][1010];
> Which of the following is faster?
> 1. memset(grid,0,1010*1010);
> 2. memset(grid,0,sizeof(grid));


1. since you initialize only part of the array (typically
sizeof(int)/sizeof(char)) but this is probably not what you expected.

> b) Is there any faster alternative to initializing memory?


int *g = (int*)grid;
int *p = g + 1010*1010;
while(p-- != g)
*p = 0;

?

> c) What is the difference between comp.lang.c and alt.comp.lang.c ?


alt.

> d) Is there any performance difference in local and global array?


should not.

a+, ld.
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      12-09-2005
Annajiat wrote:
>
> Hi,
> a)
> int grid[1010][1010];
> Which of the following is faster?
> 1. memset(grid,0,1010*1010);
> 2. memset(grid,0,sizeof(grid));


1 would probably be faster.
sizeof grid is equal to 1010*1010*sizeof(int),
so 1 and 2 are different.
Usually when people ask "which is faster?"
they mean for two equivalent operations.

--
pete
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      12-09-2005
<(E-Mail Removed)> wrote:
>int grid[1010][1010];
>Which of the following is faster?
>1. memset(grid,0,1010*1010);
>2. memset(grid,0,sizeof(grid));


You probably meant:
1. memset(grid,0,1010*1010*sizeof(int));
Otherwise the two statements are not equivalent.

I would expect a modern compiler to compute
the result of "1010*1010*sizeof(int)" at compile
time, therefore there would be no difference
between 1. & 2.
If that is not the case, then the only difference
would be the time required to compute the product.

>b) Is there any faster alternative to initializing memory?

That depends on the target CPU, the compiler being used and
the details of the memset() provided in the C library you
are using.

>c) What is the difference between comp.lang.c and alt.comp.lang.c ?

I have not looked into alt.comp.lang.c, so I do not know
anything about its current contents, level, etc.
In general, anybody can create a newsgroup under the "alt."
hierarchy, creating a group under "comp." requires a formal
proposal & voting process.

>d) Is there any performance difference in local and global array?

As in b) depends on CPU and compiler. You should run a test to
find out in your system.

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      12-09-2005
In article <(E-Mail Removed)>,
Roberto Waltman <(E-Mail Removed)> wrote:

> <(E-Mail Removed)> wrote:
> >int grid[1010][1010];
> >Which of the following is faster?
> >1. memset(grid,0,1010*1010);
> >2. memset(grid,0,sizeof(grid));

>
> You probably meant:
> 1. memset(grid,0,1010*1010*sizeof(int));
> Otherwise the two statements are not equivalent.
>
> I would expect a modern compiler to compute
> the result of "1010*1010*sizeof(int)" at compile
> time, therefore there would be no difference
> between 1. & 2.


You are overlooking a small, but important detail, which doesn't only
affect execution speed, but also correctness.
 
Reply With Quote
 
Kleuskes & Moos
Guest
Posts: n/a
 
      12-09-2005
On Fri, 09 Dec 2005 18:11:14 +0000, Christian Bau wrote:

> In article <(E-Mail Removed)>,
> Roberto Waltman <(E-Mail Removed)> wrote:
>
>> <(E-Mail Removed)> wrote:
>> >int grid[1010][1010];
>> >Which of the following is faster?
>> >1. memset(grid,0,1010*1010);
>> >2. memset(grid,0,sizeof(grid));

>>
>> You probably meant:
>> 1. memset(grid,0,1010*1010*sizeof(int));
>> Otherwise the two statements are not equivalent.
>>
>> I would expect a modern compiler to compute
>> the result of "1010*1010*sizeof(int)" at compile
>> time, therefore there would be no difference
>> between 1. & 2.

>
> You are overlooking a small, but important detail, which doesn't only
> affect execution speed, but also correctness.


Great... I love puzzles.

memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
inspection of the array will then yield 0xF2F2F2F2 as raw integer value
which (probably) isn't what the OP expects (assuming a 32 bit integer).

Right?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-09-2005
Kleuskes & Moos <(E-Mail Removed)> writes:

> On Fri, 09 Dec 2005 18:11:14 +0000, Christian Bau wrote:
>
>> In article <(E-Mail Removed)>,
>> Roberto Waltman <(E-Mail Removed)> wrote:
>>> 1. memset(grid,0,1010*1010*sizeof(int));

>> You are overlooking a small, but important detail, which doesn't only
>> affect execution speed, but also correctness.

> Great... I love puzzles.
>
> memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
> inspection of the array will then yield 0xF2F2F2F2 as raw integer value
> which (probably) isn't what the OP expects (assuming a 32 bit integer).
>
> Right?


No. Completely wrong.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      12-09-2005
<(E-Mail Removed)> wrote:
Christian Bau wrote:
>> Roberto Waltman <(E-Mail Removed)> wrote:
>>> >int grid[1010][1010];
>>> >Which of the following is faster?
>>> >1. memset(grid,0,1010*1010);
>>> >2. memset(grid,0,sizeof(grid));
>>>
>>> You probably meant:
>>> 1. memset(grid,0,1010*1010*sizeof(int));
>>> Otherwise the two statements are not equivalent.
>>>
>>> I would expect a modern compiler to compute
>>> the result of "1010*1010*sizeof(int)" at compile
>>> time, therefore there would be no difference
>>> between 1. & 2.

>>
>> You are overlooking a small, but important detail, which doesn't only
>> affect execution speed, but also correctness.

>
>Great... I love puzzles.
>
>memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
>inspection of the array will then yield 0xF2F2F2F2 as raw integer value
>which (probably) isn't what the OP expects (assuming a 32 bit integer).
>
>Right?


Wrong. And no puzzle here. Christian refers to the
fact that "memset(grid,0,sizeof(grid))" will always
have the correct size of grid, while the correctness
of the other statement depends on the programmer
updating it manually if the size of grid changes.

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-09-2005
Kleuskes & Moos <(E-Mail Removed)> writes:
> On Fri, 09 Dec 2005 18:11:14 +0000, Christian Bau wrote:
>> In article <(E-Mail Removed)>,
>> Roberto Waltman <(E-Mail Removed)> wrote:
>>
>>> <(E-Mail Removed)> wrote:
>>> >int grid[1010][1010];
>>> >Which of the following is faster?
>>> >1. memset(grid,0,1010*1010);
>>> >2. memset(grid,0,sizeof(grid));
>>>
>>> You probably meant:
>>> 1. memset(grid,0,1010*1010*sizeof(int));
>>> Otherwise the two statements are not equivalent.
>>>
>>> I would expect a modern compiler to compute
>>> the result of "1010*1010*sizeof(int)" at compile
>>> time, therefore there would be no difference
>>> between 1. & 2.

>>
>> You are overlooking a small, but important detail, which doesn't only
>> affect execution speed, but also correctness.

>
> Great... I love puzzles.
>
> memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
> inspection of the array will then yield 0xF2F2F2F2 as raw integer value
> which (probably) isn't what the OP expects (assuming a 32 bit integer).


Nope. 0x3F2 is 1010 decimal, and is used in the third argument to
memset(), which is of type size_t. It's not truncated to a byte.

You'll find the answer in other responses in this thread. If you want
to figure it out for yourself, ask yourself why these two:
memset(grid,0,1010*1010);
memset(grid,0,sizeof(grid));
are *not* equivalent.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      12-09-2005
In article <(E-Mail Removed)>,
Kleuskes & Moos <(E-Mail Removed)> wrote:

> On Fri, 09 Dec 2005 18:11:14 +0000, Christian Bau wrote:
>
> > In article <(E-Mail Removed)>,
> > Roberto Waltman <(E-Mail Removed)> wrote:
> >
> >> <(E-Mail Removed)> wrote:
> >> >int grid[1010][1010];
> >> >Which of the following is faster?
> >> >1. memset(grid,0,1010*1010);
> >> >2. memset(grid,0,sizeof(grid));
> >>
> >> You probably meant:
> >> 1. memset(grid,0,1010*1010*sizeof(int));
> >> Otherwise the two statements are not equivalent.
> >>
> >> I would expect a modern compiler to compute
> >> the result of "1010*1010*sizeof(int)" at compile
> >> time, therefore there would be no difference
> >> between 1. & 2.

> >
> > You are overlooking a small, but important detail, which doesn't only
> > affect execution speed, but also correctness.

>
> Great... I love puzzles.
>
> memset sets byte and will *incorrectly* chop 0x3F2 to 0xF2. Subsequent
> inspection of the array will then yield 0xF2F2F2F2 as raw integer value
> which (probably) isn't what the OP expects (assuming a 32 bit integer).


The first one sets 1010 * 1010 bytes.
The second one sets 1010 * 1010 ints. Which is usually two or four times
more than 1010 bytes.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Finding Server... Finding Host.... enough already!!! Leesa_Tay@softhome.net Computer Support 2 01-20-2006 10:23 AM
Finding speed of a CDR using "test"? TheHiddenPoster Computer Support 1 11-28-2005 07:35 AM
speed speed speed a.metselaar Computer Support 14 12-30-2003 03:34 AM



Advertisments