Speed of finding a size of an array.

A

Annajiat

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

(e-mail address removed)
(e-mail address removed)
(e-mail address 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.
 
L

Laurent Deniau

Annajiat said:
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.
 
P

pete

Annajiat said:
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.
 
R

Roberto Waltman

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. ]
 
C

Christian Bau

Roberto Waltman said:
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.
 
K

Kleuskes & Moos

Roberto Waltman said:
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?
 
B

Ben Pfaff

Kleuskes & Moos said:
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.
 
R

Roberto Waltman

Roberto Waltman said:
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. ]
 
K

Keith Thompson

Kleuskes & Moos said:
Roberto Waltman said:
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.
 
C

Christian Bau

Kleuskes & Moos said:
Roberto Waltman said:
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.
 
C

Christian Bau

Roberto Waltman said:
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.

Just to make sure we are talking about the same things:
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

1. Contains an absolut blatant error that will make you wish you could
disappear in the ground when you spot it. You are probably looking for
some subtle mistake. There is nothing subtle about it.
 
C

Christopher Benson-Manica

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.

Unless I'm missing something, Roberto was aware of that difference and
said so.
 
C

Christian Bau

Christopher Benson-Manica said:
Unless I'm missing something, Roberto was aware of that difference and
said so.

It seems I was completely thrown off by the second sentence of his reply
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.

and somehow missed that _he_ was the one who posted the correction from

Apologies. My fault.
 
C

Christian Bau

Thad Smith said:
Christian said:
Roberto Waltman said:
Christian Bau 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.

Just to make sure we are talking about the same things:
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

1. Contains an absolut blatant error that will make you wish you could
disappear in the ground when you spot it. You are probably looking for
some subtle mistake. There is nothing subtle about it.

Sorry, Christian 2, you are wrong. Christian 1 saw the above

and took it into consideration when writing

Christian 1 was therefore referring to the fact that sizeof(grid) is a
the correct size, while 1010*1010*sizeof(int) is evaluated as
(int)(1010*1010) * sizeof(int), which overflows in the case that an int
is 16 bits and size_t is properly sized to permit a declaration of
int grid[1010][1010];

No, Christian suffered from a severe lack of concentration. The problem
you spotted could have been used for some face saving, but I didn't spot
it (and there were maybe five years where I used implementations where
this would have been a problem, that is int = 16 bit and enough memory
for 1010 x 1010 ints).
 
T

Thad Smith

Christian said:
Roberto Waltman said:
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.

Just to make sure we are talking about the same things:
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

1. Contains an absolut blatant error that will make you wish you could
disappear in the ground when you spot it. You are probably looking for
some subtle mistake. There is nothing subtle about it.[/QUOTE]

Sorry, Christian 2, you are wrong. Christian 1 saw the above

and took it into consideration when writing

Christian 1 was therefore referring to the fact that sizeof(grid) is a
the correct size, while 1010*1010*sizeof(int) is evaluated as
(int)(1010*1010) * sizeof(int), which overflows in the case that an int
is 16 bits and size_t is properly sized to permit a declaration of
int grid[1010][1010];

Right?
 
M

Malcolm

Annajiat said:
a)
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

Other people have pointed out this error.
However with a million elements, any difference in the way you call the
function will be totally trivial.
b) Is there any faster alternative to initializing memory?
Not in C. However you have over 4 million bytes to initialise. A lot of
computers are equipped with hardware which can clear or transfer memory in
parallel, and this may be the way to go.
c) What is the difference between comp.lang.c and alt.comp.lang.c ?
Dunno.
d) Is there any performance difference in local and global array?
Not really. It is a micro-optimisation problem. One some systems the global
might be slightly faster (because of overhead in setting up the local
array), on other systems the local may be slightly faster (maybe because the
stack is more likely to be in the cache). But the difference is probably
trivial in comparison to the algorithm you use to manipuate the array.
 
K

Keith Thompson

Malcolm said:
Annajiat said:
a)
int grid[1010][1010];
Which of the following is faster?
1. memset(grid,0,1010*1010);
2. memset(grid,0,sizeof(grid));

Other people have pointed out this error.
However with a million elements, any difference in the way you call the
function will be totally trivial.
b) Is there any faster alternative to initializing memory?
Not in C. However you have over 4 million bytes to initialise. A lot of
computers are equipped with hardware which can clear or transfer memory in
parallel, and this may be the way to go.

And if so, memset() may use it if the implementer has chosen to
optimize it for a particular target CPU.
 
M

Malcolm

Keith Thompson said:
And if so, memset() may use it if the implementer has chosen to
optimize it for a particular target CPU.
The problem is that, in ANSI C

memset(array, 0, HUGE_NUMBER);
if(array[index] == 0)
{
/* this condition must always be true */
}

However if the memory is being cleared in parallel, really you want to do
something useful whilst the clear takes place.
 
E

Eric Sosman

Malcolm said:
Keith Thompson said:
And if so, memset() may use it if the implementer has chosen to
optimize it for a particular target CPU.

The problem is that, in ANSI C

memset(array, 0, HUGE_NUMBER);
if(array[index] == 0)
{
/* this condition must always be true */

Only for suitable types of `array'.
}

However if the memory is being cleared in parallel, really you want to do
something useful whilst the clear takes place.

Sorry; nobody named "comp.programming.threads" lives
here. You must have a wrong number.

(More seriously, C's model of the computing universe is
single-threaded and synchronous. If I were looking for things
to parallelize in C, I'd not worry about memset() until after
I'd done something about I/O.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top