which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0; 17 1943
"onkar" <on*******@gmail.comschrieb im Newsbeitrag
news:11**********************@i38g2000prf.googlegr oups.com...
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
time it and you'll know, for your platform, compiler and optimazation
options
Bye, Jojo
onkar wrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
Why do you expect there is a difference? The inner loop looks identical.
--
Tor <torust [at] online [dot] no>
On 30 May, 12:23, onkar <onkar....@gmail.comwrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
Short Answer: It Depends.
Less Short Answer: It depends on a bunch of things, none of which is
directly related to standard C.
Pragmatic Answer: Try both and see.
What I'd Probably Do<tm>: memset(a, 0x00, sizeof a[0][0] * 1000);
Thought Experiment: What would each of your methods do if every array
element happened to be around half a memory page in size? Assuming
you are on a system with virtual memory, of course.
On May 30, 12:23 pm, onkar <onkar....@gmail.comwrote:
which one runs faster ??
Yes.
>
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
1. You haven't told me what a[rows][columns] is or what the cache line
size on your machine. If a is a 2-D array of bytes, and the cache line
was 1000 bytes, then it probably won't matter.
2. This is not really about C as such. However, as C is a Row-Major
language (see http://en.wikipedia.org/wiki/Row-major_order), it's
normally taken to be better to vary the column index faster than the
row index, or more generally to order index variation from the right.
onkar wrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
memset(a,0,1000*sizeof(a[0]));
That will be optimized in assembly by the
system provider, and it will be faster than
your loops.
jacob navia wrote:
onkar wrote:
>which one runs faster ??
for(i=0;i<100;i++) for(j=0;j<10;j++) a[i][j]=0;
OR
for(j=0;j<10;j++) for(i=0;i<100;i++) a[i][j]=0;
memset(a,0,1000*sizeof(a[0]));
That will be optimized in assembly by the
system provider, and it will be faster than
your loops.
It will also be wrong. R-O-N-G, wrong. (Hint: On the
assumption that the original loops do not invoke undefined
behavior, is sizeof a[0] == sizeof a[0][0] possible?)
Even after the obvious repair it could still be wrong.
No, I'm not talking about exotic machines where all-bits-zero
is not "zero" for the type in question. (Hint: which elements
of `unsigned char a[120][150]' are affected by the loops and
which are affected by memset?)
--
Eric Sosman es*****@acm-dot-org.invalid
"onkar" <on*******@gmail.comwrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
(The following assumes that "a" has been allocated as a
single contiguous block of memory, and that the dimensions
of a are [100][10]. You didn't state either. If these
assumptions are not true, the following does not hold.)
Your first set of nested loops might run faster, because it
involves a single traverse through the memory, one byte
increments in the same direction. The second jumps back
and forth.
A compiler might translate the first to machine laguage as
"rep stosl" (writing all the bytes in a single rapid-fire
burst).
The second, however, might get tranlated to nested loops,
which would be slower.
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org
On May 30, 4:23 pm, onkar <onkar....@gmail.comwrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
check it yourself using clock()
help - use man page for clock
Bye
Guru Jois
On 5ÔÂ30ÈÕ, ÏÂÎç7ʱ23·Ö, onkar <onkar....@gmail.comwrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
I thought them run in the same time, haha , I am new at c/c++;
onkar said:
which one runs faster ??
T a[100][10] = {0};
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www. ma**********@pobox.com wrote:
On May 30, 12:23 pm, onkar <onkar....@gmail.comwrote:
>which one runs faster ??
Yes.
>for(i=0;i<100;i++) for(j=0;j<10;j++) a[i][j]=0;
OR
for(j=0;j<10;j++) for(i=0;i<100;i++) a[i][j]=0;
1. You haven't told me what a[rows][columns] is or what the cache line
size on your machine. If a is a 2-D array of bytes, and the cache line
was 1000 bytes, then it probably won't matter.
For tuning, yes cache issues are important. There is a technique called
*loop blocking*, which address this. For example, consider a more
interesting loop:
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
A[i][j] = A[i][j] * B[j][i];
Now let us rewrite the loop, and select 'block_size' such that the A and
B memory chunks fit a cache line:
for (i=0; i<MAX; i+=block_size)
for (j=0; j<MAX; j+=block_size)
for (k=i; k<i+block_size; k++)
for (l=j; l<j+block_size; l++)
A[k][l] = A[k][l] * B[l][k];
This sort of cache optimization, is what a optimizing C compiler might
do behind the scene.
2. This is not really about C as such. However, as C is a Row-Major
language (see http://en.wikipedia.org/wiki/Row-major_order), it's
normally taken to be better to vary the column index faster than the
row index, or more generally to order index variation from the right.
IF you want to make the job easier for the C optimizer, yes then the
inner index should be over 'j'. However, for OP example, I be very
surprised if a modern optimizing C compiler really care which order the
programmer arrange 'i' and 'j'.
More important is the *readability* of the code, since the "standard"
way to code such a loop is:
for (i=0; i<ROW_MAX; i++)
for (j=0; j<COL_MAX; j++)
I like to have a very good reason before doing it the other way around.
--
Tor <torust [at] online [dot] no>
Richard Heathfield wrote:
onkar said:
>which one runs faster ??
T a[100][10] = {0};
Chapter and verse please. :P
--
Tor <torust [at] online [dot] no>
Tor Rustad wrote:
>
Richard Heathfield wrote:
onkar said:
which one runs faster ??
T a[100][10] = {0};
Chapter and verse please. :P
It doesn't matter which one of those two, runs faster.
They're not the same.
This one is a declaration.
The other one was a statement.
--
pete
On May 30, 12:17 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
Chapter and verse please. :P
Let's make it:
static T a[100][10] = {0};
Then we have:
"5.1.2 Execution environments
1 Two execution environments are defined: freestanding and hosted. In
both cases, program startup occurs when a designated C function is
called by the execution environment. All objects with static storage
duration shall be initialized (set to their initial values) before
program startup. The manner and timing of such initialization are
otherwise unspecified. Program termination returns control to the
execution environment."
Giving a time of zero seconds.
Guru Jois <gu*******@gmail.comwrites:
On May 30, 4:23 pm, onkar <onkar....@gmail.comwrote:
>which one runs faster ??
for(i=0;i<100;i++) for(j=0;j<10;j++) a[i][j]=0;
OR
for(j=0;j<10;j++) for(i=0;i<100;i++) a[i][j]=0;
check it yourself using clock()
help - use man page for clock
Better yet, use a profiler if your system provides one. (Details of
profiler use are off-topic.)
--
Keith Thompson (The_Other_Keith) 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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
On May 30, 12:23 pm, onkar <onkar....@gmail.comwrote:
which one runs faster ??
for(i=0;i<100;i++)
for(j=0;j<10;j++)
a[i][j]=0;
OR
for(j=0;j<10;j++)
for(i=0;i<100;i++)
a[i][j]=0;
If you want to actually learn something: Write a program that executes
this code and measure the execution time.
Then vary the numbers (100 and 10). Study what happens. Check if
anything unusual happens. If anything unusual happens, write down
exactly what happens, post a complete program, and someone will
explain to you _why_ it happens.
The code that you posted is useless, because we don't actually know
what it is doing. Is a an array of arrays of int, or is it an array of
pointers to int, or is it a pointer to arrays of ints, or a pointer to
an array of pointers to int? Huge difference.
Tak <ka******@gmail.comschrieb:
On 5??30??, ????7??23??, onkar <onkar....@gmail.comwrote:
>which one runs faster ??
for(i=0;i<100;i++) for(j=0;j<10;j++) a[i][j]=0;
OR
for(j=0;j<10;j++) for(i=0;i<100;i++) a[i][j]=0;
I thought them run in the same time, haha , I am new at c/c++;
leaving the topic of clc (that is: speaking OT), in practice, both can
differ significantly, but they do not need to. If the compiler
translates these "literally", you might even end up thrashing your
virtual memory (making this statement even more OT), resulting in
factors of 1000 or more between both statements.
Of course, for this to happen, it is likely that the values 100 and 10
must be much bigger than in the "samples" above.
Ok, now I'll shut my OT mouth. ;)
Regards,
Spiro.
--
Spiro R. Trikaliotis http://opencbm.sf.net/ http://www.trikaliotis.net/ http://www.viceteam.org/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rabbit63 |
last post by:
Hi:
I want to show a set of records in the database table on the clicnt browser.
I have two ways to do this (writen in JScript):
1.The first way is:
<%
var sql = "select firstname from...
|
by: The Doctor |
last post by:
Hi all,
Recently I was asked the question about whether ++U or U++ is
faster if U is a user defined type. What's the answer?
Thank you
|
by: Skybuck Flying |
last post by:
Hi,
I needed a method to determine if a point was on a line segment in 2D. So I
googled for some help and so far I have evaluated two methods.
The first method was only a formula, the second...
|
by: nelu |
last post by:
I know I can use unsinged byte, but I need it for java, which code runs
faster in C?
int f1(char b) {
return (b&0x80)|(b&0x7f);
}
or
int f2(char b) {
|
by: John Salerno |
last post by:
My initial feeling is that concatenation might take longer than
substitution, but that it is also easier to read:
def p(self, paragraph):
self.source += '<p>' + paragraph + '</p>\n\n'
vs.
...
|
by: Ganesh |
last post by:
Hi,
This is a question that pertains to pointers in general (C or C++).
Which of the following is faster and why?
for (int i = 0; i < N; i++)
= ... a...
(or)
|
by: Brian Tkatch |
last post by:
Is there a way to check the order in which SET INTEGRITY needs to be
applied? This would be for a script with a dynamic list of TABLEs.
B.
|
by: rfuscjr via AccessMonster.com |
last post by:
This is truly bizzare. I have a query that runs for hours in one Access db.
When I import it into another Access db, it runs in minutes. I compacted and
repaired the original, relinked tables...
|
by: Patient Guy |
last post by:
Which is the better approach in working with Javascript?
1. Server side processing: Web server gets form input, runs it into the
Javascript module, and PHP collects the output for document prep....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |