http://www.slf4j.org/faq.html#logging_performance
第二种方式将比第一种方式有30%的性能提升
但是,第二种写法是怎么实现的性能提升?
What is the fastest way of (not) logging?
SLF4J supports an advanced feature called parameterized logging which can significantly boost logging performance for disabled logging statement.
For some Logger logger, writing,
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
incurs the cost of constructing the message parameter, that is converting both integer i and entry[i] to a String, and concatenating intermediate strings. This, regardless of whether the message will be logged or not.
One possible way to avoid the cost of parameter construction is by surrounding the log statement with a test. Here is an example.
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
This way you will not incur the cost of parameter construction if debugging is disabled for logger. On the other hand, if the logger is enabled for the DEBUG level, you will incur the cost of evaluating whether the logger is enabled or not, twice: once in debugEnabled and once in debug. This is an insignificant overhead because evaluating a logger takes less than 1% of the time it takes to actually log a statement.
Better yet, use parameterized messages
There exists a very convenient alternative based on message formats. Assuming entry is an object, you can write:
Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);
After evaluating whether to log or not, and only if the decision is affirmative, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction in case the log statement is disabled.
The following two lines will yield the exact same output. However, the second form will outperform the first form by a factor of at least 30, in case of a disabled logging statement.
logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);
A two argument variant is also available. For example, you can write:
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
If three or more arguments need to be passed, you can make use of the Object... variant of the printing methods. For example, you can write:
logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);
This form incurs the hidden cost of construction of an Object[] (object array) which is usually very small. The one and two argument variants do not incur this hidden cost and exist solely for this reason (efficiency). The slf4j-api would be smaller/cleaner with only the Object... variant.
Array type arguments, including multi-dimensional arrays, are also supported.
SLF4J uses its own message formatting implementation which differs from that of the Java platform. This is justified by the fact that SLF4J's implementation performs about 10 times faster but at the cost of being non-standard and less flexible.
Escaping the "{}" pair
The "{}" pair is called the formatting anchor. It serves to designate the location where arguments need to be substituted within the message pattern.
SLF4J only cares about the formatting anchor, that is the '{' character immediately followed by '}'. Thus, in case your message contains the '{' or the '}' character, you do not have to do anything special unless the '}' character immediately follows '}'. For example,
logger.debug("Set {1,2} differs from {}", "3");
which will print as "Set {1,2} differs from 3".
You could have even written,
logger.debug("Set {1,2} differs from {{}}", "3");
which would have printed as "Set {1,2} differs from {3}".
In the extremely rare case where the the "{}" pair occurs naturally within your text and you wish to disable the special meaning of the formatting anchor, then you need to escape the '{' character with '\', that is the backslash character. Only the '{' character should be escaped. There is no need to escape the '}' character. For example,
logger.debug("Set \\{} differs from {}", "3");
will print as "Set {} differs from 3". Note that within Java code, the backslash character needs to be written as '\\'.
In the rare case where the "\{}" occurs naturally in the message, you can double escape the formatting anchor so that it retains its original meaning. For example,
logger.debug("File name is C:\\\\{}.", "file.zip");
will print as "File name is C:\file.zip".
logger.debug("The new entry is "+entry+"."); logger.debug("The new entry is {}.", entry);
第二种方式将比第一种方式有30%的性能提升
但是,第二种写法是怎么实现的性能提升?
引用
What is the fastest way of (not) logging?
SLF4J supports an advanced feature called parameterized logging which can significantly boost logging performance for disabled logging statement.
For some Logger logger, writing,
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
incurs the cost of constructing the message parameter, that is converting both integer i and entry[i] to a String, and concatenating intermediate strings. This, regardless of whether the message will be logged or not.
One possible way to avoid the cost of parameter construction is by surrounding the log statement with a test. Here is an example.
if(logger.isDebugEnabled()) {
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
}
This way you will not incur the cost of parameter construction if debugging is disabled for logger. On the other hand, if the logger is enabled for the DEBUG level, you will incur the cost of evaluating whether the logger is enabled or not, twice: once in debugEnabled and once in debug. This is an insignificant overhead because evaluating a logger takes less than 1% of the time it takes to actually log a statement.
Better yet, use parameterized messages
There exists a very convenient alternative based on message formats. Assuming entry is an object, you can write:
Object entry = new SomeObject();
logger.debug("The entry is {}.", entry);
After evaluating whether to log or not, and only if the decision is affirmative, will the logger implementation format the message and replace the '{}' pair with the string value of entry. In other words, this form does not incur the cost of parameter construction in case the log statement is disabled.
The following two lines will yield the exact same output. However, the second form will outperform the first form by a factor of at least 30, in case of a disabled logging statement.
logger.debug("The new entry is "+entry+".");
logger.debug("The new entry is {}.", entry);
A two argument variant is also available. For example, you can write:
logger.debug("The new entry is {}. It replaces {}.", entry, oldEntry);
If three or more arguments need to be passed, you can make use of the Object... variant of the printing methods. For example, you can write:
logger.debug("Value {} was inserted between {} and {}.", newVal, below, above);
This form incurs the hidden cost of construction of an Object[] (object array) which is usually very small. The one and two argument variants do not incur this hidden cost and exist solely for this reason (efficiency). The slf4j-api would be smaller/cleaner with only the Object... variant.
Array type arguments, including multi-dimensional arrays, are also supported.
SLF4J uses its own message formatting implementation which differs from that of the Java platform. This is justified by the fact that SLF4J's implementation performs about 10 times faster but at the cost of being non-standard and less flexible.
Escaping the "{}" pair
The "{}" pair is called the formatting anchor. It serves to designate the location where arguments need to be substituted within the message pattern.
SLF4J only cares about the formatting anchor, that is the '{' character immediately followed by '}'. Thus, in case your message contains the '{' or the '}' character, you do not have to do anything special unless the '}' character immediately follows '}'. For example,
logger.debug("Set {1,2} differs from {}", "3");
which will print as "Set {1,2} differs from 3".
You could have even written,
logger.debug("Set {1,2} differs from {{}}", "3");
which would have printed as "Set {1,2} differs from {3}".
In the extremely rare case where the the "{}" pair occurs naturally within your text and you wish to disable the special meaning of the formatting anchor, then you need to escape the '{' character with '\', that is the backslash character. Only the '{' character should be escaped. There is no need to escape the '}' character. For example,
logger.debug("Set \\{} differs from {}", "3");
will print as "Set {} differs from 3". Note that within Java code, the backslash character needs to be written as '\\'.
In the rare case where the "\{}" occurs naturally in the message, you can double escape the formatting anchor so that it retains its original meaning. For example,
logger.debug("File name is C:\\\\{}.", "file.zip");
will print as "File name is C:\file.zip".
发表评论
-
charles4.2下载与破解方法以及配置https
2020-02-26 09:03 2有两个抓包工具 一个是fidder,一个是charles,两个 ... -
序列号批量生成算法
2019-12-05 14:11 0业务处理过程当中,经常需要生成订单号、序列号等,简单的可 ... -
使用ANTLR处理文本
2019-08-28 17:32 767引用 使用 Antlr 处理文本 https://www.ib ... -
解决maven-metadata.xml文件下载卡死问题
2019-04-11 14:02 3986http://192.168.1.110:8081/nexus ... -
rsync备份和删除指定文件
2018-01-02 10:23 2051文件异地备份时,需要将本地文件合并到服务器上,且不能删除服务器 ... -
javaLocale格式化日期和数字
2017-08-25 09:26 870public static void main(Strin ... -
centos6 tomcat 启动脚本 tomcat服务
2017-08-23 11:24 1441系统自动启动tomcat 复制该脚本到/etc/init.d/ ... -
win7 命令行改IP和DNS
2016-12-21 18:35 738使用管理员权限运行CMD //改DNS netsh ... -
jenkins中集成sonar,使用findbug、pmd、checkstyle提升代码质量
2016-09-29 14:58 6175实际上jenkins单独也 ... -
jenkins 集成sonar
2016-09-18 10:14 0jenkins集成sonar可以从插件中心直接更新安装 son ... -
activeMQ5.14权限配置
2016-08-17 13:47 2673activeMQ默认的消息队列没有用户名和密码,可以直接通过T ... -
solaris 使用解压版的jdk
2016-07-27 15:17 764solaris上配置jdk其实也很简单 由于solaris有 ... -
solaris tomcat开机启动
2016-07-27 16:17 621创建文件夹/var/svc/manifes ... -
HibernateTemplate Vs HibernateDaoSupport Vs Direct Hibernate Access
2016-07-26 11:07 734http://forum.spring.io/forum/sp ... -
spring mvc mybatis will not be managed by Spring
2016-07-20 17:30 9887项目运行时发现事务提交不完整,回滚时只能回滚一半。 系统配置 ... -
java里判断一点是否在某个区域
2016-06-03 17:47 1845import java.awt.geom.Path2D ... -
12306的技术升级
2016-04-20 16:17 1034升级的核心是余票查询的升级,余票查询使用存储过程,sybase ... -
工作流的123
2016-04-20 12:58 577三分钟了解Activity工作流 工作流一般会给开发人员提供流 ... -
sping mvc 使用@Value注解为controller注入值
2016-04-17 17:39 10836spring mvc 里有两个配置文件, 第一个,为sprin ... -
googleapis.com域名访问慢的解决办法
2016-04-13 12:09 9721、安装火狐 2、安装插件ReplaceGoogleCDN
相关推荐
• What is the best way to produce cars to maximize profit? • What is the best way to carry groceries home: paper or plastic? • Which is the best school for my kid? • Which is the best fuel to ...
The Fastest and Easiest Way for Beginners to Develop Android Apps What’s Stopping You From Making Your Own Android Apps? The mobile phone app market is worth more than $25 billion and is expected ...
- **你可以以最快的速度在市电影院买到票.**:You can buy tickets the fastest at the city cinema. - **到目前为止你认为它怎样?**:How do you think of it so far? - **我仍然对周围的情况不是很清楚.**:I'm...
This book is the fastest way to get comfortable with C, one incredibly clear and easy step at a time. You’ll learn all the basics: how to organize programs, store and display data, work with ...
- 句型2:用于对比,如:“Town Cinema is closer to home and you can buy tickets the fastest there.” 【自研自探】 - 句子翻译: 1. "What's the best movie theater to go to?" The answer is Town Cinema. ...
DISCLAIMER: This is not an official Google product. Google's production ...It is the fastest! Not faster, but the fastest! I have tried it on a big project with ~200 modules. While it takes hundreds of
Another way to say this is that the data itself is part of the clustered index. A clustered index keeps the data in a table ordered around the key. The data pages in the table are kept in a doubly ...
Windows 10 for the Internet of Things is for developers and enthusiasts wanting to take their skills in Windows development and jump on board one of the largest and fastest growing trends to hit the ...
My intention was to create a collection... Also, if you would like to share some other codes with us, you can contact me to add them in this help file (reusing code is the fastest way to solve problems).
上帝王牌 Description Farmer John has been informed of the location ...The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
Exploring Swift Playgrounds: The Fastest and Most Effective Way to Learn to Code and to Teach Others to Use Your Code by Jesse Feiler English | 26 May 2017 | ISBN: 1484226461 | 192 Pages | PDF | 7.27 ...
The Internet of Things (IoT) is the fastest growing technology market. Industries are embracing IoT technologies to improve operational expenses, product life, and people’s well-being. An ...
A: What do you think is the best way of travelling is? B: I think aeroplane is by far the best way. A: Why do you think so? B: Because it is the fastest and the safest. A: Of course it is when you are...
This book is the fastest way to take control of Windows 10, and use it to create, connect, and discover… simplify and organize your whole life… learn more, play more, do more, live better!...
than locality sensitive hashing, which is the fastest current method. By using semantic hashing to filter the documents given to TF-IDF, we achieve higher accuracy than applying TF-IDF to the entire...
Machine learning is one of the fastest growing areas of computer science, with far-reaching applications. The aim of this textbook is to introduce machine learning, and the algorithmic paradigms it ...
This book is the fastest way to get comfortable with C, one incredibly clear and easy step at a time. You’ll learn all the basics: how to organize programs, store and display data, work with ...
It is parsed every time a book record is retrieved, so the output rendering is the fastest possible. http://www.php9.com/php/source/amazon_layout.phps Here you have the full package zipped. ...
C# 2010 offers powerful new features, and this book is the fastest path to mastering them—and the rest of C#—for both experienced C# programmers moving to C# 2010 and programmers moving to C# from ...