Java转义HTML - 字符串替换慢吗? [英] Java escape HTML - string replace slow?

查看:122
本文介绍了Java转义HTML - 字符串替换慢吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,它大量使用大文件,读取,处理并通过SolrEmbeddedServer( http://lucene.apache.org/solr/

I have a Java application that makes heavy use of a large file, to read, process and give through to SolrEmbeddedServer (http://lucene.apache.org/solr/).

其中一个函数执行基本的HTML转义:

One of the functions does basic HTML escaping:

private String htmlEscape(String input)
{
    return input.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
        .replace("'", "&apos;").replaceAll("\"", "&quot;");
}

在分析应用程序时,该程序在此功能中花费大约58%的时间,替换时总共占47%,替换所有者占11%。

While profiling the application, the program spends roughly 58% of the time in this function, a total of 47% in replace, and 11% in replaceAll.

现在,是Java取代那么慢,或者我是否在正确的道路上,我是否应该认为该程序足够高效,以便在Java中出现瓶颈,而不是在我的代码中?(或者我是否更换错误?)

Now, is the Java replace that slow, or am I on the right path and should I consider the program efficient enough to have its bottleneck in Java and not in my code? (Or am I replacing wrong?)

提前致谢!

推荐答案

对于html转义,你可以使用 StringEscapeUtils.escapeHtml(输入) 的rel = noreferrer>公地琅。据说在那里以更有效的方式实施。

For html escaping you can use StringEscapeUtils.escapeHtml(input) from commons-lang. It is supposedly implemented in a more efficient way there.

这篇关于Java转义HTML - 字符串替换慢吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆