Qiao enhance the "translation" speed cache with Map

Written in 2019-05-17

In the service code in many cases you need to use "translation" between code2Name or id2Name, found that many developers are double-cycle to achieve this "translation" directly in my past experience. If a "translation" of large amount of data, the performance bottleneck on the case, and then you can consider using the Map caching way to enhance the speed.

Examples of
user tables (userId, levelNum)
level meter (levelNum, levelName)
now want the user list for each user levelNum translated into levelName show.

code show as below:

package test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TranslateTest {
      public static void main(String[] args) {
           //初始化列表数据
           int num = 10000;
           List<User> userList = new ArrayList<User>();
           List<Level> levelList = new ArrayList<Level>();
           for(int i=0; i<num; i++) {
                 userList.add(new User(i, i));
                 levelList.add(new Level(i, "等级:" + i));
           }
           
           //使用双重循环翻译
           long startTime = System.currentTimeMillis();
           translateLoop(userList, levelList);
           System.out.println("translateLoop use: " + (System.currentTimeMillis() - startTime)  + " MS");
           clearTranslateResult(userList);
           
           //使用map缓存翻译
           startTime = System.currentTimeMillis();
           translateUseMap(userList, levelList);
           System.out.println("translateUseMap use: " + (System.currentTimeMillis() - startTime)  + " MS");
      }
      
      //清除翻译结果
      static void clearTranslateResult(List<User> userList) {
           for(User user : userList) {
                 user.setLevelName(null);
           }
      }
      
      static void translateLoop (List<User> userList, List<Level> levelList){
           for(User user : userList) {
                 for(Level level : levelList) {
                      if(user.getLevelNum() == level.getLevelNum()) {
                            user.setLevelName(level.getLevelName());
                            break;
                      }
                 }
           }
      }
      
      static void translateUseMap(List<User> userList, List<Level> levelList) {
           Map<Integer, String> levelNum2Name = new HashMap<Integer, String>();
           for(Level level : levelList) {
                 levelNum2Name.put(level.getLevelNum(), level.getLevelName());
           }
           for(User user : userList) {
                 user.setLevelName(levelNum2Name.get(user.getLevelNum()));
           }
      }
      
      static class Level {
           //级数
           private int levelNum;
           //级别名称
           private String levelName;
           
           public Level(int levelNum, String levelName) {
                 this.levelNum = levelNum;
                 this.levelName = levelName;
           }
           
           public int getLevelNum() {
                 return levelNum;
           }
           public void setLevelNum(int levelNum) {
                 this.levelNum = levelNum;
           }
           public String getLevelName() {
                 return levelName;
           }
           public void setLevelName(String levelName) {
                 this.levelName = levelName;
           }
      }
      
      static class User {
           private int userId;//用户ID
           private int levelNum;//级数
           
           public User(int userId, int levelNum) {
                 this.userId = userId;
                 this.levelNum = levelNum;
           }
           
           //翻译后的值
           private String levelName;
           
           public String getLevelName() {
                 return levelName;
           }
           public void setLevelName(String levelName) {
                 this.levelName = levelName;
           }
           public int getUserId() {
                 return userId;
           }
           public void setUserId(int userId) {
                 this.userId = userId;
           }
           public int getLevelNum() {
                 return levelNum;
           }
           public void setLevelNum(int levelNum) {
                 this.levelNum = levelNum;
           }
      }
}

结果
translateLoop use: 311 MS
translateUseMap use: 9 MS

The num increase after 100,000 test results:
translateLoop use: the MS 75964
translateUseMap use: the MS 58

Summary
translateLoop using the double loop, the time complexity is O (n- n-)
translateUseMap, since the get method map is usually complex (hash does not collide with) the time of is O (1), then the total time complexity is O (n + 1
the n-)
I think: programmer, even if only to achieve business code, or have space for time, time for awareness of space; even if usually do not have access algorithm, or to understand the common and time complexity of the algorithm.

Guess you like

Origin www.cnblogs.com/mzsg/p/11977989.html