473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Efficiency Issue - true or false?

This is supposed to be a very general question.
Consider the following 2 code samples:

Code Sample 1:

for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
/* Do something here */
}
}

Code Sample 2:
(Now we declare j in advance rather than each time inside the i loop
as in code sample 1)

int j;
for (int i=0; i<n; ++i)
{
for (j=0; j<n; ++j) // Notice the differnce here, now
{
/* Do something here */
}
}
Is code sample 2 faster than code sample 1?
Plz specify the reasons too.

Jul 23 '05 #1
3 998
ra*********@gmail.com wrote:
This is supposed to be a very general question.
Consider the following 2 code samples:

Code Sample 1:

for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
/* Do something here */
}
}

Code Sample 2:
(Now we declare j in advance rather than each time inside the i loop
as in code sample 1)

int j;
for (int i=0; i<n; ++i)
{
for (j=0; j<n; ++j) // Notice the differnce here, now
{
/* Do something here */
}
}
Is code sample 2 faster than code sample 1?
Plz specify the reasons too.


No, they will produce exactly the same code. The reason is that
"creating an int" doesn't actually require any work at all, except
marking some register / piece of memory in which it will live.

You could find that if j was a complex class, the second might be
faster, although in most cases it probably wouldn't be, as "complexclass
j = 0" would be mapped to "complexclass j(0)" and in many cases
constructing a new object may be faster than assigning to an existing one.

Chris
Jul 23 '05 #2
ra*********@gmail.com wrote:
This is supposed to be a very general question.
Consider the following 2 code samples:

Code Sample 1:

for (int i=0; i<n; ++i)
{
for (int j=0; j<n; ++j)
{
/* Do something here */
}
}

Code Sample 2:
(Now we declare j in advance rather than each time inside the i loop
as in code sample 1)

int j;
for (int i=0; i<n; ++i)
{
for (j=0; j<n; ++j) // Notice the differnce here, now
{
/* Do something here */
}
}
Is code sample 2 faster than code sample 1?
Plz specify the reasons too.

In both cases two ints are created and used. In subsequent loops with
more int counters, you can't expect any additional cost either because
the compiler can perform further optimisations since it knows that the
variables are used for loop counters only, like placing them in a register.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
ra*********@gmail.com wrote:
Is code sample 2 faster than code sample 1?
No.
Plz specify the reasons too.


You must write one more line.

--
Salu2
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Kin Pang | last post by:
Hi, I have a routine where I'm using a std::map from a pair of ints to double for caching evaluation results. The cache depth could be upto say 1000 in depth. However, my application needs to...
2
by: john in fl | last post by:
Hello, I created an Access97 database wich I just updated to Access 2000. This database had a form that ran a VB event procedure on the current record. This procedure prevented editing if a...
1
by: kig25 | last post by:
Hello, When using the VB.NET PrintDocument class, I seem to be encountering an issue where the sub pd_PrintPage handles PrintDocument.PrintPage (upon continuing if HasMorePages = true) will...
0
by: Dan | last post by:
Issue making textbox visible based on specific input from a radio button list in an EditItemTemplate I want to setup a gridview that when in edit mode and when the user selects "Other" from a...
5
by: CarrieR | last post by:
I am working within a DB I have been devolping in Access 2003, and have encountered a report coding issue I was hoping someone could help me with: I have 18 reports, each running off some smaller...
9
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm...
0
by: Shades799 | last post by:
Hi All, I was wondering if any of you could help me with a very difficult problem that I am having. I have an ASP site that works with an Oracle database using an ADODB connection. This...
1
by: klaul | last post by:
Hi there. I'm having an issue with autopostback, in that it doesn't seem to be working! The webform I am using pulls the data from an AccessDataSource to populate the gridview, which has Edit and...
0
by: suzyq89 | last post by:
Hi there, I'm a newbie with AS2....and i wanted to learn to create a photo gallery that scrolls right or left, and that when you click on the photo it becomes bigger. Well I found a tutorial from...
10
Jezternz
by: Jezternz | last post by:
Not exactly an event but it has the same general purpose. Basicly it will fire the event when the user stops typing for 500 miliseconds. The reason for this, is I have an ajax form and I want the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.