[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 1997-07-16
6
Medium Priority
?
233 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 140 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 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