473,574 Members | 2,511 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Very basic performance question

Given a loop like this:

for (int ix=0; ix<myArray.leng th; ix++) {
System.out.prin tln(ix);
}

or especially this:

for (int ix=0; ix<Math.sqrt(sh oeSize)*hairlen gth/(IQ); ix++) {
System.out.prin tln(ix);
}

I've always assumed that it made more sense to look up or calculate the
value in the second term of the for loop, rather than make Java look it up
or calculate it, especially for large numbers of iterations of the loop. It
just made no sense to do the same calculation or lookup a thousand or a
million times; it seemed a lot more reasonable to do the calculation or
lookup ONCE, store the value in a variable, and then plug the variable into
the for loop, like this:

int loopController = Math.sqrt(shoeS ize)*hairlength/(IQ);
for (int ix=0; ix<loopControll er; ix++) {
System.out.prin tln(ix);
}

Is my reasoning sound or does Java have "tricks" (optimizations) that make
it more sensible to leave the lookup or calculation in the loop, this
avoiding the need to create the variable that controls the loop?

I've been wondering about that for a while and am finally getting around to
asking ;-)

I realize that I could set up a benchmark to find out for sure but I'm
betting that the answer is already well-known so I'd rather ask here and
save myself the work ;-)

--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
Nov 12 '05 #1
1 1183

"Rhino" <rh****@NOSPAM. sympatico.ca> wrote in message
news:Z1******** ***********@new s20.bellglobal. com...
Given a loop like this:

for (int ix=0; ix<myArray.leng th; ix++) {
System.out.prin tln(ix);
}

or especially this:

for (int ix=0; ix<Math.sqrt(sh oeSize)*hairlen gth/(IQ); ix++) {
System.out.prin tln(ix);
}

[snip]

Oops, sorry, wrong newsgroup!

Rhino
Nov 12 '05 #2

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

Similar topics

2
2070
by: Craig Stadler | last post by:
I have a fairly large database on my (2 million records) WIN32 mySQL 4.0.20 The question is: I am adding large amounts of data to it (300,000 to 500,000 at a time) with the standard (INSERT INTO) command. Question is, do I have to reindex, and when and how often? Does mySQL automatically build indexes on the fly as I add data?
3
1464
by: nick | last post by:
Hi, I'm pretty much a database beginner and have what I think is a basic question: If I have a table which has a 'status' column and I can have say three statuses: "active", "pending", "inactive". Is it better to break these out into a different "Statuses" table? On the one hand it seems to me it would be better to be in a different
9
10492
by: Vito DeCarlo | last post by:
I've been having this problem for a few weeks. PLEASE read this post before responding with some simple reason that has nothing to do with my problem. If you need more information, please request it as I'll be checking this post very often. PROBLEM: Occaisionally, our web server begins running ASP.NET pages extremely slowly. Other...
2
1613
by: Joel Vazquez | last post by:
Visual Basic.NET Application RunTime Crashes and Stalls Im a newbie if you could say in .NET ive been working with it the past 3 months and have done lots of things with it, without any prior knowledge. I have a few question regarding the stability of my application. I have a synchronization application that connects to a service and...
14
1714
by: Steve | last post by:
Sorry in advance for my ignorance. Any help would sure be appreciated. I'm writing a fairly simple application with VB.Net and am obviously a bit of a newbie. This application will be used by 1, 2 or at most 3 people concurrently and I'm using Access 2003 for my data source. We are not dealing with a large amount of data (5 or 6 tables,...
25
680
by: Jhon | last post by:
Hi every one, I got very basic question, here i go: Say i have 11001 11010 bits which are infact 10 bits. Now i want to address every bit so if it is zero i would add one and if it is one then i would be adding zero. Folks say it manchester coding. Please note that left hand side just accept a single bit for every operation.
17
2399
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) } and call it 50 times, will it cause problems because you are not
4
2150
by: sqlguy | last post by:
Why do we have to contact MS for a problem that has been with this compiler from at least the beta of VS 20005. I am so sick and tired of the 30 - 40 clicks it takes to dismiss VS when there is a problem. Can they not just post the fix. I see no reason to contact MS since I have most likely sent about 1500 dumps to them and I would think...
19
1793
by: frankiespark | last post by:
Hello all, I was perusing the internet for information on threading when I came across this group. Since there seems to be a lot of good ideas and useful info I thought I'd pose a question. Threading is a new concept for me to implement. Here is my problem. I have a system that receives xml files and records their file locations in a...
6
4250
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. this.ContentID = (int)ci.Conid; vs. this.ContentID = Convert.ToInt32(ci.Conid); I tend to use the latter form because it seems more descriptive to me,...
0
7794
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, 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...
0
7710
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...
0
8040
Oralloy
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. ...
0
8092
tracyyun
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...
0
6437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5296
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3745
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2230
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
1
1332
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.