关于java:SimpleDateFormat时间格式化不同模型性能对比

8次阅读

共计 1652 个字符,预计需要花费 5 分钟才能阅读完成。

SimpleDateFormat 三种不同的应用办法

  这里总结一下 SimpleDateFormat 的三种用法不同的性能比照后果。
  尽管 SimpleDateFormat 性能最差,线程不平安,然而一些老旧的零碎还是有那么多的人用,这里就简略比照一下性能差别

代码
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S");

    @ApiOperation("SimpleDateFormat- 获取以后工夫戳")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response= R.class),
    })
    @RequestMapping(value = "/SimpleDateFormat", method = RequestMethod.GET)
    public String SimpleDateFormatCase(){long startTime = System.currentTimeMillis();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S");
        String date = sdf1.format(System.currentTimeMillis());
        log.info("解析工夫耗时:" + (System.currentTimeMillis() - startTime) + "ms");
        return date;
    }

    @ApiOperation("SimpleDateFormat- 获取以后工夫戳 2 - 复用 SimpleDateFormat")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response= R.class),
    })
    @RequestMapping(value = "/SimpleDateFormat2", method = RequestMethod.GET)
    public String SimpleDateFormatCase2(){long startTime = System.currentTimeMillis();
        String date = sdf.format(System.currentTimeMillis());
        log.info("解析工夫耗时:" + (System.currentTimeMillis() - startTime) + "ms");
        return date;
    }

    @ApiOperation("SimpleDateFormat- 获取以后工夫戳 3 - 应用线程锁")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response= R.class),
    })
    @RequestMapping(value = "/SimpleDateFormat3", method = RequestMethod.GET)
    public String SimpleDateFormatCase3(){String date = DateFormatUtil.formatDateTime(new Date());
        return date;
    }
压测形式

  间接用 jmeter 发压,10 并发,发压 100s,服务器监控用 sar 命令简略监控(也不是什么很简单的业务,第三方监控组件没必要)

模型 1 后果

TPS:111.3 笔 /s,RT:88ms,CPU:37.1%

模型 2 后果

TPS:115 笔 /s,RT:86ms,CPU:20.25%

模型 3 后果

TPS:116 笔 /s,RT:85ms,CPU:12.92%

后果统计
模型 TPS 响应工夫 90%Line 95%Line 99%Line 资源耗费
1 111.3 88 121 149 210 37.01%
2 115 86 115 139 204 20.25%
3 116 85 110 125 203 12.92%

能够不言而喻的看到 TPS 和 RT 其实相差不多的,次要还是利用资源耗费相差较大。

正文完
 0