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 1966
"onkar" <on*******@gmai l.comschrieb im Newsbeitrag
news:11******** **************@ i38g2000prf.goo glegroups.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....@gmai l.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....@gmai l.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*******@gmai l.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....@gmai l.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....@gmai l.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++; 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 table1";
var obj=new ActiveXObject("ADODB.Recordset");
|
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 method was a piece of C code
which turned out to be incorrect and incomplete but by modifieing it would
still be usuable.
The first method was this piece of text:
|
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.
def p(self, paragraph):
| |
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 etc. Nothing makes it run faster. I
have imported the query in several dbs. I did find another where it appeared
to run very long. Still in others it runs in minutes. Ideas?
--
Message posted via AccessMonster.com...
|
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.
2. Client side processing: Web server gets form input and passes it to PHP
which includes the Javascript written in a way to make the form input
processed on the client side and rendered (probably using DOM function
calls) on that side as...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |