?
Solved

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

Posted on 1997-07-16
6
Medium Priority
?
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And 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 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

752 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