Solved

Why is myList.isSelected(i) so slow?  Is there a faster way?

Posted on 1997-07-16
6
192 Views
Last Modified: 2006-11-17
Does anyone know how to speed up the following code?

len = myList.countItems();
for(i=0; i < len; i++) {
  if (myList.isSelected(i)) {
    // ... do something
  }
}

The time lag seems to be on checking to see if an item is selected or not.
Whether or not the item is selected in the list doesn't change the lag.

It takes about 1 sec per item.  For example, my list has 300 items and the
above code takes about 5 minutes to complete.

Thanks.
0
Comment
Question by:mag062397
6 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223139
What does isSelected() method do? If it's a short method which it should be then it should take much less than 1 sec to execute. Why don't you print the timestamp to figure out how much isSelected takes vs. the if blok itself.
0
 
LVL 2

Expert Comment

by:webster030697
ID: 1223140
The very first thing I'd do is get rid of len

for(int i = 0; i < myList.countItems(); i++)
{    if(myList.isSelected(i))
     {    //do stuff
     }
}

of course this is not a huge deal, unless this piece of code gets called a lot.
0
 

Author Comment

by:mag062397
ID: 1223141
Comment for msmolyak:

isSelected is a method in the java.awt.List class.

It determines whether an item in the list is selected or not.

The following code takes basiclly no time at all:

len = myList.countItems();
for(i=0; i < len; i++) {
  if (i==10) {
    System.out.println("Hello");
  }
}

So, I don't think it is the if statement.
-----------------------------------------------------------------------
Comment for webster

The len is there to speed things up.  It is my understanding the a function call takes longer than resolving the value of a variable.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:webster030697
ID: 1223142
My mistake. I must have been brain dead when I looked at that.
0
 

Author Comment

by:mag062397
ID: 1223143
One solution I came up with is to use getMultipleSelections.

This ran much much faster.  It took 2 seconds to loop through 300 items.

However, I still wonder why isSelected runs so slowly.
????
0
 
LVL 4

Accepted Solution

by:
rembo earned 70 total points
ID: 1223144

What sort of system are you running on?  This list
is a peer list, which means it has to contact the
local window system to determine that.  You may try
grabbing a 100% Java component off the net.  Netscape's
Internet Foundation Classes are the one's I would
recommend looking at.

You can download IFC here:

http://search.netscape.com/download/ifc.html

It's performance should be considerably better.

-Tony


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 67
wordmultiple challenge 12 116
solarwind tftp server 2 44
diffSum example 4 19
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now