最快的是StringBuilder的方式
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.text.MessageFormat;
import java.util.Date;
/**
* Created by weixiang.wu on 2017/7/4.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class FormatTest extends AbstractJUnit4SpringContextTests {
private static final Logger logger = LoggerFactory.getLogger(FormatTest.class);
@Test
public void formatTest() {
String username = "Jack";
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
StringBuilder sb = new StringBuilder();
sb.append(username)
.append(" login system at ")
.append(new Date());
}
long end1 = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
String.format("login system at %s", new Date());
}
long end2 = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
MessageFormat.format("login system at {0,date,yyyy-MM-dd HH:mm:ss}", new Date());
}
long end3 = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
logger.info("login system at {}",new Date());
}
long end4 = System.currentTimeMillis();
System.out.println("Stringbuilder cost: " + (end1 - start));
System.out.println("String.format cost: " + (end2 - end1));
System.out.println("MessageFormat cost: " + (end3 - end2));
System.out.println("logger{} cost: " + (end4 - end3));
}
}
Stringbuilder cost: 3729
String.format cost: 11989
MessageFormat cost: 22400
logger{} cost: 887559
0.slf4j有一个common logger没有的功能,字符串中的{}会被替换,如下:
logger.info("Hello {}","world");
在很多项目中经常打印Log,返回操作之后的响应消息给客户端等都会涉及到消息的格式化,一般都是会有一个消息模板,然后传入一些特定的参数值达到项目的需要。在Java中处理方式一般有以下三种:
1. 使用StringBuilder
使用一个StringBuilder对象进行封装,然后toString传给终端,OK,这个是很普遍的做法,只是在代码上不是很美观,但是性能在本文中三个方案中应该是最高的。假设有这样一个场景,需要对用户登陆消息进行Log。
String username = "Jack";
StringBuilder sb = new StringBuilder();
sb.append(username)
.append(" login system at ")
.append(new Date());
logger.info(sb.toString());
2. 使用String.format
String类也提供了format()方法可以对消息进行格式化,这种方式是依赖通配符完成的,一般操作是这样的:
String username = "Jack";
logger.info(String.format("%s login system at %s", username, new Date()));
3. 使用MessageFormat
String username = "Jack";
logger.info(MessageFormat.format("{0} login system at {1,date,yyyy-MM-dd HH:mm:ss}", username, new Date()));
MessageFormat则使用的是占位符,占位符可以配置的元素有以下几种,应该很好理解,可以对日期和Number类型的参数做格式化:
{ ArgumentIndex } { ArgumentIndex , FormatType } { ArgumentIndex , FormatType , FormatStyle }
在不是特别care性能可以使用MessageFormat更加灵活,代码也会更加美观。
然后看了一下CPU,MessageFormat也是要高一点的
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有