# Performance question Classic List Threaded 7 messages Open this post in threaded view
|

## Performance question

 Hello everyone, I have a quick question about how to optimize some Glazedlists code.  I have reduced a much more complex network of lists from my real application to this simple example, that has a source list, wrapped in a grouping list that puts values in buckets, and a function list to calculate the average of each bucket: EventList sourceList = new BasicEventList(); GroupingList groupingList = new GroupingList(sourceList, new BucketComparator()); FunctionList, Double> functionList =                         new FunctionList, Double>(groupingList, new AverageFunction()); A contrived example but it shows my problem, which is that I am incurring the O(n) overhead of calculating the average price on each insert, where as there is obviously an O(1) update function for an average that I should be using.  The question is, how can I rewrite this for better performance? For reference, I have pasted in the full text of a class that exercises my problem below. Thanks in advance for any help. graham /////////// Sample code import java.util.Comparator; import java.util.List; import ca.odell.glazedlists.BasicEventList; import ca.odell.glazedlists.EventList; import ca.odell.glazedlists.FunctionList; import ca.odell.glazedlists.GroupingList; import ca.odell.glazedlists.FunctionList.Function; public class FunctionListTest {         /**          * @param args          */         public static void main(String[] args) {                 EventList sourceList = new BasicEventList();                 GroupingList groupingList = new GroupingList(sourceList, new BucketComparator());                 FunctionList, Double> functionList =                         new FunctionList, Double>(groupingList, new AverageFunction());                 long now = System.currentTimeMillis();                 for (int i = 0; i < 100000; i++){                         sourceList.add((double)i);                         if (i % 1000 ==0){                                 for (Double double1 : functionList) {                                         System.out.println(i+": "+double1);                                 }                                 System.out.println("Took: "+((System.currentTimeMillis()-now)/1000.0));                                 now = System.currentTimeMillis();                         }                 }                         }                 static class BucketComparator implements Comparator{                 int MAX = 25;                 int BUCKET_SIZE = 5;                                 public int compare(Double arg0, Double arg1) {                         if (arg0 > MAX && arg1 > MAX){                                 return 0;                         } else {                                 return (((int)(double)arg0)/BUCKET_SIZE) - (((int)(double)arg1)/BUCKET_SIZE);                         }                 }                         }                 public static class AverageFunction implements Function, Double> {                 public Double evaluate(List arg0) {                         int size = arg0.size();                         if (size == 0){                                 return 0.0;                         } else{                                 double total = 0.0;                                 for (Double double1 : arg0) {                                         total += double1;                                 }                                 return total/size;                         }                 }         } }
Open this post in threaded view
|

## Re: Performance question

Open this post in threaded view
|

## Re: Performance question

Open this post in threaded view
|

## Re: Performance question

Open this post in threaded view
|