简体   繁体   English

Java +提高性能和可伸缩性

[英]java + increasing performance and scalability

below is the a code snippet, which returns the object of a class. 下面是一个代码片段,它返回一个类的对象。 now the object is basially comparing to some parameter in loop. 现在,对象正在与循环中的某些参数进行基础比较。

my concern is what if there are thousands of objects in loop, in that case performance and scalability can be an issue. 我担心的是,如果有成千上万的对象在循环中,那么在这种情况下,性能和可伸缩性就会成为问题。 please suggest how to improve this code for performance part 请提出如何改进此代码的性能部分

public Widget get(String name,int major,int minor,boolean exact) {
   Widget widgetToReturn = null;
   if(exact) {
       Widget w = new Widget(name, major, minor);

       // for loop using JDK 1.5 version
       for(Widget wid : set) {
            if((w.getName().equals(wid.getName())) && (wid.getVersion()).equals(w.getVersion())) {
                widgetToReturn = w;
                break;
            } 
       }
   } else {
       Widget w = new Widget(name, major, minor);
       WidgetVersion widgetVersion = new WidgetVersion(major, minor);

       // for loop using JDK 1.5 version
       for(Widget wid : set) {
           WidgetVersion wv = wid.getVersion();
           if((w.getName().equals(wid.getName())) && major == wv.getMajor() && WidgetVersion.isCompatibleAndNewer(wv, widgetVersion)) {
                widgetToReturn = wid;
           } else if((w.getName().equals(wid.getName())) && wv.equals(widgetVersion.getMajor(), widgetVersion.getMinor())) {
                widgetToReturn = w;
           }
       }
   }
   return widgetToReturn;

} }

I think Will's question is the 1st question to ask - why are you holding the Widgets in a none efficient data structure? 我认为Will的问题是第一个要问的问题-为什么将Widgets置于效率不高的数据结构中?

If you use a structure like this: 如果使用这样的结构:

Map<String, Map<WidgetVersion,Widget>> widgetMap;

You can write the following code: 您可以编写以下代码:

public Widget get(String name,int major,int minor,boolean exact) 
{
   Widget widgetToReturn = null;

   Map<WidgetVersion,Widget> widgetVersionMap = widgetMap.get(name);
   WidgetVersion widgetVersion = new WidgetVersion(major, minor);
   widgetToReturn = widgetVersionMap.get(widgetVersion);
   if(widgetToReturn==null && exact==false) 
   {
       // for loop using JDK 1.5 version
       for(Entry<WidgetVersion,Widget> entry : widgetVersionMap.entrySet()) 
       {
           WidgetVersion wv = entry.getKey();
           if(major == wv.getMajor() && WidgetVersion.isCompatibleAndNewer(wv, widgetVersion)) 
           {
                widgetToReturn = entry.getValue();
           } 
       }
   }
   return widgetToReturn;
}

That way for exact searches you have an O(1) search time and for no-exact you have O(K) where K is the number of versions a widget has. 这样,对于精确的搜索,您有O(1)的搜索时间,对于没有精确的搜索,则有O(K),其中K是小部件具有的版本数。

Rather than a simple "set" of Widgets, you probably need to maintain a Map<WidgetVersion, Widget> . 您可能需要维护Map<WidgetVersion, Widget>而不是简单的Widget“集合”。 This will give you O(1) (for a hash map) or O(logN) (for a tree map) lookup, compared with the O(N) lookup of the current version. 与当前版本的O(N)查找相比,这将为您提供O(1) (对于哈希映射)或O(logN) (对于树映射)查找。

(You might actually need two maps, or a Map> or even something more complicated. I cannot quite figure out what your exact / inexact matching is supposed to do, and it also depends on how many versions of a given widget are likely in practice.) (您实际上可能需要两张地图,或者一个Map>甚至更复杂的东西。我无法完全弄清楚您应该执行的精确/不精确匹配,这还取决于实际中给定小部件的多少版本。)

In addition, the logic of your "exact" case looks broken. 另外,“精确”案例的逻辑看起来很混乱。 You are creating a widget, looking in the set of existing widgets, then: 您正在创建一个小部件,查看现有小部件的集合,然后:

  • if you find a match you return the widget you just created ... (so why bother with the lookup??) 如果找到匹配项,则返回刚刚创建的小部件...(为什么要麻烦查找??)
  • if you didn't find a match you return null . 如果找不到匹配项,则返回null

而且这些小部件不在以名称为键的地图中...为什么?

Map<String, List<Widget>> widgetMap;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM