Solved

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

Posted on 1997-07-16
6
181 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

707 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

12 Experts available now in Live!

Get 1:1 Help Now