最近在使用用Spring Boot 做后端,很糾結一個問題
Map<String, Object> 和自定義的Dto性能差距
1. 使用Map<String, Object> 非常方便,而且不用定義很多的類,但是因為Object會涉及到很多的裝箱和拆箱,會造成性能流失。
2. 定義多個輸入和輸出的dto類,看起來清晰,但是很麻煩。
但是我寫了2個這種方法,然后經過測驗,發現兩者性能并沒有多少區別。有沒有大佬能給我講解下?
下面貼上部分代碼,假如有不好的地方,請指出。
這是使用Map<String, Object>
@PostMapping(path = "/gettimebymember")
public Map<String, Object> getTimeByAdAccountAndMember(@RequestBody Map<String, Object> obj ){
Map<String, Object> ret = new HashMap<String, Object>();
Map<String, Object> data = new HashMap<String, Object>();
try {
// 查出該賬號的所有下屬
String date = (String)obj.get("date");
String adAccount = (String)obj.get("adAccount");
UserInfo supervisor = userInfoService.GetUserInfoByAdAccount(adAccount);
int yearInt = Integer.parseInt(date.substring(0, date.indexOf('-')));
List<UserInfo> list = userInfoService.ListAllActive();
List<UserInfo> members = new ArrayList<UserInfo>();
members.add(supervisor);
for(UserInfo user : list)
{
String name1 = user.getSupervisor();
String name2 = adAccount;
if(name1 == null)
continue;
if(name1.equals(name2))
{
members.add(user);
}
}
if(members.isEmpty())
{
ret.put("data", "0");
return ret;
}
// 查找每一個下屬今年的每月時間
String[] monthName = {"Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
for(UserInfo m : members)
{
double[] memberTime = new double[12];
Map<String, Object> member = new HashMap<String, Object>();
String ad = m.getAdAccount();
for(int i = 1; i <= 12; i++ )
{
Date firstDate = Helper.getFirstDayOfMonth(yearInt, i);
Date lastDate = Helper.getLastDayOfMonth(yearInt, i);
Date firstMondy = Helper.getMondyDayOfDate(firstDate);
Date lastMondy = Helper.getMondyDayOfDate(lastDate);
List<String> WBSList = taskService.GetWBSList(ad);
Date startDate = firstMondy;
double monthtime = 0;
while(startDate.getTime() <= lastMondy.getTime())
{
double daytime = 0;
for(String wbs:WBSList)
{
List<Map<String, Object>> cats = taskCatsService.GetTimeForSpecialWBS(ad, wbs, startDate, i);
double wbstime = 0;
for(Map item:cats)
{
wbstime += (double)item.get("workingHours");
}
daytime += wbstime;
}
monthtime += daytime;
startDate = Helper.plusDate(7, startDate);
}
member.put(monthName[i-1], monthtime);
memberTime[i-1] = monthtime;
}
data.put(ad, memberTime);
}
ret.put("Data", data);
return ret;
}catch (Exception ex)
{
ret.put("data", "0");
return ret;
}
}
這是自定義類
@PostMapping(path = "/gettimebymembermirror")
public Map<String, Object> getTimeByAdAccountAndMemberMirror(@RequestBody InputDto inputDto){
Map<String, Object> ret = new HashMap<String, Object>();
List<OutputDto> data = new ArrayList<OutputDto>();
try {
// 查出該賬號的所有下屬
String date = inputDto.getDate();
String adAccount = inputDto.getAdAccount();
UserInfo supervisor = userInfoService.GetUserInfoByAdAccount(adAccount);
int yearInt = Integer.parseInt(date.substring(0, date.indexOf('-')));
List<UserInfo> list = userInfoService.ListAllActive();
List<UserInfo> members = new ArrayList<UserInfo>();
members.add(supervisor);
for(UserInfo user : list)
{
String name1 = user.getSupervisor();
String name2 = adAccount;
if(name1 == null)
continue;
if(name1.equals(name2))
{
members.add(user);
}
}
if(members.isEmpty())
{
ret.put("data", "0");
return ret;
}
// 查找每一個下屬今年的每月時間
String[] monthName = {"Jan.","Feb.","Mar.","Apr.","May.","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."};
for(UserInfo m : members)
{
OutputDto outDto = new OutputDto();
double[] memberTime = new double[12];
Map<String, Double> member = new HashMap<String, Double>();
String ad = m.getAdAccount();
for(int i = 1; i <= 12; i++ )
{
Date firstDate = Helper.getFirstDayOfMonth(yearInt, i);
Date lastDate = Helper.getLastDayOfMonth(yearInt, i);
Date firstMondy = Helper.getMondyDayOfDate(firstDate);
Date lastMondy = Helper.getMondyDayOfDate(lastDate);
List<String> WBSList = taskService.GetWBSList(ad);
Date startDate = firstMondy;
double monthtime = 0;
while(startDate.getTime() <= lastMondy.getTime())
{
double daytime = 0;
for(String wbs:WBSList)
{
List<TimeForSpecialWBSDto> cats = taskCatsService.GetTimeForSpecialWBSMirror(ad, wbs, startDate, i);
double wbstime = 0;
for(TimeForSpecialWBSDto item:cats)
{
wbstime += item.getWorkingHours();
}
daytime += wbstime;
}
monthtime += daytime;
startDate = Helper.plusDate(7, startDate);
}
member.put(monthName[i-1], monthtime);
memberTime[i-1] = monthtime;
}
outDto.setMonthHourList(memberTime);
outDto.setName(ad);
data.add(outDto);
}
ret.put("Data", data);
return ret;
}catch (Exception ex)
{
ret.put("data", "0");
return ret;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/227193.html
標籤:Web 開發
