论坛首页 编程语言技术论坛

Spads 出品字符串高效提取数值方法 Shane 末日圣诞奉献

浏览 1807 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2012-12-26  

Spads 出品,作者 WangXP 与 Shane


目录
---------- ---------- ---------- ----------
1 【综述】
2 【程序】
3 【功能】
4 【性能】
5 【附录】



1 【综述】
---------- ---------- ---------- ----------
最近因为继续编写公式处理和 Json - Java 互转组件,所以专门制作、优化了字符串变成数值的工具方法,并在性能上取得了领先于阿帕奇工具和爪哇类库等方式的成就。本方法基本思路是逐字符扫描。目前关于将字符串转为数值,市面上常见的资料,我搜索总结了十几篇吧,大体是重复的,包括这么几种方法。我看过的资料网址收集在附录中。

1、通过 Java 系统提供的 Character#isDigit(char) 判断是否为数值,然后转换。
2、通过正则表达式判断是否为数值,然后转换。
3、直接转换,捕捉异常。
4、阿帕奇提供了 isNumeric(String) 和 isNumericSpace(String) 方法用以判断。如果是数值再转换。

目前我这边的需求是,需要支持整数、数字间的空格、空白头尾、小数以及直接小数点开头的小数。

JavaLib 的 isDigit(char) 只能判断数字,所以需要事先去除空格、首位负号和第一个小数点。
正则表达式在转换前需要去除空格
直接转换前也需要去除空格
阿帕奇工具方法能判断数字和空格,所以需要事先去除首位负号和第一个小数点。

Spads 编写程序是直接利用了字符特点,扫描字符串。也可以认为是一种结合了预处理与字符 ASCII 判断的方法。本方法会将能够解析成数值的字符串解析成数值,否则返回 null 。



2 【程序】
---------- ---------- ---------- ----------
Spads 直接编写了适用的程序,经过很长时间性能优化,达到了优于上述所有方法的效果。代码如下。

/**
 * <b>抽取字符串的数字值</b><br/>
 * 
 * 本方法用来将传入字符串的内容理解为十进制数值。如果传入字符串的内容无法理解为
 * 数值,则返回 <code>null</code> 。数值字符串是指包括正、负的整数、浮点数;其中
 * 允许出现分割用空格,允许直接使用小数点起头以表示 0.x 样的小数。<br/>
 * 方法使用示例如下。
 * <pre>
 * StringTool.fetchNumberValue("15") = 15 (java.lang.Integer)
 * StringTool.fetchNumberValue("300 1500 1010")
 * = 30015001010 (java.lang.Long)
 * StringTool.fetchNumberValue("	28 ") = 28 (java.lang.Integer)
 * StringTool.fetchNumberValue("3.8") = 3.8 (java.lang.Double)
 * StringTool.fetchNumberValue("-99") = -99 (java.lang.Integer)
 * StringTool.fetchNumberValue("+15") = null
 * StringTool.fetchNumberValue(".45") = 0.45 (java.lang.Double)
 * StringTool.fetchNumberValue("-.1") = -0.1 (java.lang.Double)
 * StringTool.fetchNumberValue("0x3AFF0") = null
 * StringTool.fetchNumberValue("042") = 42 (java.lang.Integer)
 * StringTool.fetchNumberValue("Spads") = null
 * StringTool.fetchNumberValue("") = null
 * StringTool.fetchNumberValue(null) = null
 * StringTool.fetchNumberValue("5 - 3") = null
 * StringTool.fetchNumberValue("3.1415926.5358") = null
 * StringTool.fetchNumberValue("13205972138597109830758190748937102794")
 * = 1.320597213859711E37 (java.lang.Double)
 * </pre>
 * 
 * 本方法不涉及正则表达式,同时未避免生成异常的巨大系统消耗,通过遍历字符串每
 * 一个字符来判断其是否符合十进制数值格式,格式不符则直接返回
 * <code>null</code> 。如果格式符合,浮点数按 {@link Double} 识别,整数按
 * {@link Long} 识别。如果数值范围符合 {@link Integer} ,则转换。
 * 
 * @see			StringTool#removeChar(String, char)
 * @param 		numStr	准备提取数值的字符串
 * @return		传入字符串按十进制理解的数值对象,其类型有可能为
 * <code>Long</code> 、 <code>Integer</code> 或者 <code>Double</code>。
 * @exception	NumberFormatException	当字符串是整数但超过了
 * {@link Long#MIN_VALUE} - {@link Long#MAX_VALUE} 范围同时其位数并不大于 20,
 * 或者字符串是浮点数 但超过了
 * {@link Double#MIN_VALUE} - {@link Double#MAX_VALUE} 范围。
 */
static public Number parseNumber(String numStr)
{
	if (numStr == null || (numStr = numStr.trim()).length() == 0)
		return null;
	final StringBuilder numBuilder = new StringBuilder();
	boolean doubleFlag = false;
	final int length = numStr.length();
	char numChar;
	for (int index = 0; index != length; index++)
	{
		numChar = numStr.charAt(index);
		if (numChar > 57) return null;
		switch (StringTool.charType[numChar])
		{
			// 在之前没有小数点的情况下出现小数点,记录,同时认为此字符串是浮点数
			case 3:
				if (doubleFlag || index == length - 1) return null;
				doubleFlag = true;
				// 此处通过

			// 如果是数字,认为此字符串还是数值,检查下一个字符
			case 4: numBuilder.append(numChar);
				// 此处通过

			// 如果是空格,直接检查下一个字符
			case 1: continue;

			// 如果是负号,又不是首位,则按非数值字符串进行返回
			case 2:
				if (index == 0)
				{
					numBuilder.append(numChar);
					continue;
				}
				// 此处通过

			// 如果字符不符合数值格式,方法按非数值字符串进行返回
			case 0: return null;
		}
	}

	if (doubleFlag || length > 20)
		return Double.valueOf(numBuilder.toString());

	// 非浮点数情况,如果数值在整数范围内,则按整数返回。否则按长整数返回。
	final long num = Long.parseLong(numBuilder.toString());
	final int finalNum = (int) num;
	if (finalNum != num) return num;
	return finalNum;
}

/**
 * <b>字符类型数组</b><br/>
 * 为字符串抽取数值含义提供的字符类型获取器。通过将字符本身作为此数组下标获取对应
 * 类型。类型用整数表示,含义如下。<br/>
 * 1 为空白间隔,对应 <code>' '</code> 和 <code>'\t'</code><br/>
 * 2 为负号,对应 <code>'-'</code><br/>
 * 3 为小数点,对应 <code>'.'</code><br/>
 * 4 为数字,对应 <code>'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'</code>
 */
static private int[] charType = {
	0,0,0,0,0, 0,0,0,0,1,
	0,0,0,0,0, 0,0,0,0,0,
	0,0,0,0,0, 0,0,0,0,0,
	0,0,1,0,0, 0,0,0,0,0,
	0,0,0,0,0, 2,3,0,4,4,
	4,4,4,4,4, 4,4,4
};
 经过测试,发现本方法可以完美地达到需要的处理效果。以下是测试报告。
Function test...
---------- ---------- ---------- ----------
String: 15
In StringTool way, get: 15 (Integer)
---------- ---------- ---------- ----------
String: 	28 
In StringTool way, get: 28 (Integer)
---------- ---------- ---------- ----------
String: 3.8
In StringTool way, get: 3.8 (Double)
---------- ---------- ---------- ----------
String: -99
In StringTool way, get: -99 (Integer)
---------- ---------- ---------- ----------
String: .45
In StringTool way, get: 0.45 (Double)
---------- ---------- ---------- ----------
String: -.1
In StringTool way, get: -0.1 (Double)
---------- ---------- ---------- ----------
String: null
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 0x3AFF0
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 042
In StringTool way, get: 42 (Integer)
---------- ---------- ---------- ----------
String: Spads
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String:       
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 5 - 3
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 三十八
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 3.1415926.5358
In StringTool way, get: null (null)
---------- ---------- ---------- ----------
String: 39501.50
In StringTool way, get: 39501.5 (Double)
---------- ---------- ---------- ----------
String: 0.000385427
In StringTool way, get: 3.85427E-4 (Double)
---------- ---------- ---------- ----------
String: .33796678
In StringTool way, get: 0.33796678 (Double)
---------- ---------- ---------- ----------
String: 300011110000
In StringTool way, get: 300011110000 (Long)
---------- ---------- ---------- ----------
String: 300 1500 1010
In StringTool way, get: 30015001010 (Long)
---------- ---------- ---------- ----------
String: -570 0015 6726
In StringTool way, get: -57000156726 (Long)
---------- ---------- ---------- ----------
String: -2378957832975
In StringTool way, get: -2378957832975 (Long)
---------- ---------- ---------- ----------
String: 1370 0000
In StringTool way, get: 13700000 (Integer)
---------- ---------- ---------- ----------
String: 2015773
In StringTool way, get: 2015773 (Integer)
---------- ---------- ---------- ----------
String: 3850
In StringTool way, get: 3850 (Integer)
---------- ---------- ---------- ----------
String: -80058
In StringTool way, get: -80058 (Integer)
---------- ---------- ---------- ----------
String: -1 2853 2781
In StringTool way, get: -128532781 (Integer)
---------- ---------- ---------- ----------
String: 3
In StringTool way, get: 3 (Integer)
---------- ---------- ---------- ----------
String: 0
In StringTool way, get: 0 (Integer)
---------- ---------- ---------- ----------
String: -597213859710830758190748937102794
In StringTool way, get: -5.972138597108308E32 (Double)
---------- ---------- ---------- ----------
String: 78 9254 3253 2452 9572 8532
In StringTool way, get: 7.892543253245296E21 (Double)
---------- ---------- ---------- ----------
String: 13205972138597109830758190748937102794
In StringTool way, get: 1.320597213859711E37 (Double)
---------- ---------- ---------- ----------
 


3 【功能】
---------- ---------- ---------- ----------
Spads 推出的 parseNumber(String) 方法能够完整地实现解读数值字符串的功能。那么其它几种方法表现如何呢?

其它方法的程序代码经过 Shane 认真处理,依然有 200 多行。我就不每行都贴出来了。这里简述一下各种方法所使用的方式。

A、简单爪哇类库方法 SimpleJavaLib
B
、爪哇类库方法 JavaLib
核心判断方法
private boolean isNumericByJavaLib(CharSequence numSequence)
{
	for (int i = numSequence.length(); --i >= 0; )
		if (!Character.isDigit(numSequence.charAt(i)))
			 return false;
	return true;
}
 SimpleJavaLib 将字符串直接传入此检测方法进行判断,如果检验通过则直接按 Long 类型转换。
JavaLib 方法首先给字符串去除空格、首位负号和第一个小数点,然后再传入此检测方法进行判断;如果检验通过则根据有无小数点决定用 Long 还是 Double ;如果是 Long ,得到结果之后会试图转换为 int ,已决定是否可以用整形。

C、简单正则表达式 SimpleRegex
用 \d* 来判断,然后按长整数转换。

D、完整正则表达式 WholeRegex
用 ^[-]?(((\s|\d)*\d(\s|\d)*(\.(\s|\d)*\d(\s|\d)*)?)|(\.(\s|\d)*[1-9](\s|\d)*)) 来判断。如果检验通过则根据有无小数点决定用 Long 还是 Double ;如果是 Long ,得到结果之后会试图转换为 int ,已决定是否可以用整形。

E、直接转换捕捉异常 Direct
根据字符串是否含有 '.' 字符,决定用 Double 转换还是用 Long 转换。如果转换发生异常,则返回 null 。如果是 Long ,得到结果之后会试图转换为 int ,已决定是否可以用整形。

F、直接判断字符 CharAscii
private Number fetchNumberByCharAscii(String numStr)
{
	for (int i = numStr.length(); --i >= 0; )
	{
		int chr = numStr.charAt(i);
		if (chr < 48 || chr > 57) return null;
	}
	return Long.valueOf(numStr);
}
 G、简单阿帕奇工具方法 SimpleApacheUtils
核心判断方法为 org.apache.commons.lang3.StringUtils.isNumeric(CharSequence)
其余部分与 A 类似

H、完整阿帕奇工具方法 WholeApacheUtils
核心判断方法为 org.apache.commons.lang3.StringUtils.isNumericSpace(CharSequence)
首先给字符串去除首位负号和第一个小数点,然后再传入此检测方法进行判断;如果检验通过则先消除空格,然后根据有无小数点决定用 Long 还是 Double ;如果是 Long ,得到结果之后会试图转换为 int ,已决定是否可以用整形。

我设计了这些测试用字符串,以检测各种方法的功能
private String[] numStrs = {
		"15", "\t28 ", "3.8", "-99", ".45", "-.1", null,
		"0x3AFF0", "042", "Spads", "", "      ", "5 - 3", "三十八",
		"3.1415926.5358", "39501.50", "0.000385427", ".33796678",
		"300011110000", "300 1500 1010", "-570 0015 6726", "-2378957832975",
		"1370 0000", "2015773", "3850", "-80058", "-1 2853 2781", "3", "0",
		"-597213859710830758190748937102794", "78 9254 3253 2452 9572 8532",
		"13205972138597109830758190748937102794"
	};
 检测结果如下。
Function test...
---------- ---------- ---------- ----------
String: 15
In StringTool way, get: 15 (Integer)
In SimpleJavaLib way, get: 15 (Long)
In JavaLib way, get: 15 (Integer)
In SimpleRegex way, get: 15 (Long)
In WholeRegex way, get: 15 (Integer)
In Direct way, get: 15 (Integer)
In CharAscii way, get: 15 (Long)
In SimpleApacheUtils way, get: 15 (Long)
In WholeApacheUtils way, get: 15 (Integer)
---------- ---------- ---------- ----------
String: 	28 
In StringTool way, get: 28 (Integer)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 28 (Integer)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 28 (Integer)
In Direct way, get: 28 (Integer)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 28 (Integer)
---------- ---------- ---------- ----------
String: 3.8
In StringTool way, get: 3.8 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 3.8 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 3.8 (Double)
In Direct way, get: 3.8 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 3.8 (Double)
---------- ---------- ---------- ----------
String: -99
In StringTool way, get: -99 (Integer)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -99 (Integer)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -99 (Integer)
In Direct way, get: -99 (Integer)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -99 (Integer)
---------- ---------- ---------- ----------
String: .45
In StringTool way, get: 0.45 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 0.45 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 0.45 (Double)
In Direct way, get: 0.45 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 0.45 (Double)
---------- ---------- ---------- ----------
String: -.1
In StringTool way, get: -0.1 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -0.1 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -0.1 (Double)
In Direct way, get: -0.1 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -0.1 (Double)
---------- ---------- ---------- ----------
String: null
In StringTool way, get: null (null)
In SimpleJavaLib way, get EXCEPTION: java.lang.NullPointerException
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get EXCEPTION: java.lang.NullPointerException
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 0x3AFF0
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 042
In StringTool way, get: 42 (Integer)
In SimpleJavaLib way, get: 42 (Long)
In JavaLib way, get: 42 (Integer)
In SimpleRegex way, get: 42 (Long)
In WholeRegex way, get: 42 (Integer)
In Direct way, get: 42 (Integer)
In CharAscii way, get: 42 (Long)
In SimpleApacheUtils way, get: 42 (Long)
In WholeApacheUtils way, get: 42 (Integer)
---------- ---------- ---------- ----------
String: Spads
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 
In StringTool way, get: null (null)
In SimpleJavaLib way, get EXCEPTION: java.lang.NumberFormatException: For input string: ""
In JavaLib way, get: null (null)
In SimpleRegex way, get EXCEPTION: java.lang.NumberFormatException: For input string: ""
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get EXCEPTION: java.lang.NumberFormatException: For input string: ""
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: ""
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String:       
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: "      "
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 5 - 3
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 三十八
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 3.1415926.5358
In StringTool way, get: null (null)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: null (null)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: null (null)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: null (null)
---------- ---------- ---------- ----------
String: 39501.50
In StringTool way, get: 39501.5 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 39501.5 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 39501.5 (Double)
In Direct way, get: 39501.5 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 39501.5 (Double)
---------- ---------- ---------- ----------
String: 0.000385427
In StringTool way, get: 3.85427E-4 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 3.85427E-4 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 3.85427E-4 (Double)
In Direct way, get: 3.85427E-4 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 3.85427E-4 (Double)
---------- ---------- ---------- ----------
String: .33796678
In StringTool way, get: 0.33796678 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 0.33796678 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 0.33796678 (Double)
In Direct way, get: 0.33796678 (Double)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: 0.33796678 (Double)
---------- ---------- ---------- ----------
String: 300011110000
In StringTool way, get: 300011110000 (Long)
In SimpleJavaLib way, get: 300011110000 (Long)
In JavaLib way, get: 300011110000 (Long)
In SimpleRegex way, get: 300011110000 (Long)
In WholeRegex way, get: 300011110000 (Long)
In Direct way, get: 300011110000 (Long)
In CharAscii way, get: 300011110000 (Long)
In SimpleApacheUtils way, get: 300011110000 (Long)
In WholeApacheUtils way, get: 300011110000 (Long)
---------- ---------- ---------- ----------
String: 300 1500 1010
In StringTool way, get: 30015001010 (Long)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 30015001010 (Long)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 30015001010 (Long)
In Direct way, get: 30015001010 (Long)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: "300 1500 1010"
In WholeApacheUtils way, get: 30015001010 (Long)
---------- ---------- ---------- ----------
String: -570 0015 6726
In StringTool way, get: -57000156726 (Long)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -57000156726 (Long)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -57000156726 (Long)
In Direct way, get: -57000156726 (Long)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -57000156726 (Long)
---------- ---------- ---------- ----------
String: -2378957832975
In StringTool way, get: -2378957832975 (Long)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -2378957832975 (Long)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -2378957832975 (Long)
In Direct way, get: -2378957832975 (Long)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -2378957832975 (Long)
---------- ---------- ---------- ----------
String: 1370 0000
In StringTool way, get: 13700000 (Integer)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 13700000 (Integer)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 13700000 (Integer)
In Direct way, get: 13700000 (Integer)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: "1370 0000"
In WholeApacheUtils way, get: 13700000 (Integer)
---------- ---------- ---------- ----------
String: 2015773
In StringTool way, get: 2015773 (Integer)
In SimpleJavaLib way, get: 2015773 (Long)
In JavaLib way, get: 2015773 (Integer)
In SimpleRegex way, get: 2015773 (Long)
In WholeRegex way, get: 2015773 (Integer)
In Direct way, get: 2015773 (Integer)
In CharAscii way, get: 2015773 (Long)
In SimpleApacheUtils way, get: 2015773 (Long)
In WholeApacheUtils way, get: 2015773 (Integer)
---------- ---------- ---------- ----------
String: 3850
In StringTool way, get: 3850 (Integer)
In SimpleJavaLib way, get: 3850 (Long)
In JavaLib way, get: 3850 (Integer)
In SimpleRegex way, get: 3850 (Long)
In WholeRegex way, get: 3850 (Integer)
In Direct way, get: 3850 (Integer)
In CharAscii way, get: 3850 (Long)
In SimpleApacheUtils way, get: 3850 (Long)
In WholeApacheUtils way, get: 3850 (Integer)
---------- ---------- ---------- ----------
String: -80058
In StringTool way, get: -80058 (Integer)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -80058 (Integer)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -80058 (Integer)
In Direct way, get: -80058 (Integer)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -80058 (Integer)
---------- ---------- ---------- ----------
String: -1 2853 2781
In StringTool way, get: -128532781 (Integer)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -128532781 (Integer)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -128532781 (Integer)
In Direct way, get: -128532781 (Integer)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -128532781 (Integer)
---------- ---------- ---------- ----------
String: 3
In StringTool way, get: 3 (Integer)
In SimpleJavaLib way, get: 3 (Long)
In JavaLib way, get: 3 (Integer)
In SimpleRegex way, get: 3 (Long)
In WholeRegex way, get: 3 (Integer)
In Direct way, get: 3 (Integer)
In CharAscii way, get: 3 (Long)
In SimpleApacheUtils way, get: 3 (Long)
In WholeApacheUtils way, get: 3 (Integer)
---------- ---------- ---------- ----------
String: 0
In StringTool way, get: 0 (Integer)
In SimpleJavaLib way, get: 0 (Long)
In JavaLib way, get: 0 (Integer)
In SimpleRegex way, get: 0 (Long)
In WholeRegex way, get: 0 (Integer)
In Direct way, get: 0 (Integer)
In CharAscii way, get: 0 (Long)
In SimpleApacheUtils way, get: 0 (Long)
In WholeApacheUtils way, get: 0 (Integer)
---------- ---------- ---------- ----------
String: -597213859710830758190748937102794
In StringTool way, get: -5.972138597108308E32 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: -5.972138597108308E32 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: -5.972138597108308E32 (Double)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get: null (null)
In WholeApacheUtils way, get: -5.972138597108308E32 (Double)
---------- ---------- ---------- ----------
String: 78 9254 3253 2452 9572 8532
In StringTool way, get: 7.892543253245296E21 (Double)
In SimpleJavaLib way, get: null (null)
In JavaLib way, get: 7.892543253245296E21 (Double)
In SimpleRegex way, get: null (null)
In WholeRegex way, get: 7.892543253245296E21 (Double)
In Direct way, get: null (null)
In CharAscii way, get: null (null)
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: "78 9254 3253 2452 9572 8532"
In WholeApacheUtils way, get: 7.892543253245296E21 (Double)
---------- ---------- ---------- ----------
String: 13205972138597109830758190748937102794
In StringTool way, get: 1.320597213859711E37 (Double)
In SimpleJavaLib way, get EXCEPTION: java.lang.NumberFormatException: For input string: "13205972138597109830758190748937102794"
In JavaLib way, get: 1.320597213859711E37 (Double)
In SimpleRegex way, get EXCEPTION: java.lang.NumberFormatException: For input string: "13205972138597109830758190748937102794"
In WholeRegex way, get: 1.320597213859711E37 (Double)
In Direct way, get: null (null)
In CharAscii way, get EXCEPTION: java.lang.NumberFormatException: For input string: "13205972138597109830758190748937102794"
In SimpleApacheUtils way, get EXCEPTION: java.lang.NumberFormatException: For input string: "13205972138597109830758190748937102794"
In WholeApacheUtils way, get: 1.320597213859711E37 (Double)
---------- ---------- ---------- ----------
 


4 【性能】
---------- ---------- ---------- ----------
根据功能测试,我们可以看到,各种简易方法都不能满足需要。接下来对几种能够满足需要的“完整”方法来进行性能测评对比。测试程序通过多线程,每个线程执行一种方案若干次,来直观地表现各种方法的快慢。这种测试被重复了七轮,基本消除了线程调度起伏带来的影响。每轮执行的时间都收录并累加,作为最终评判各方法性能的依据。线程控制使用了原子操作类。测试代码如下。
public void testFetchNumberPerformance()
{
	Runnable r;
	List<SortedMap<Long, String>> timeUsed =
			new ArrayList<SortedMap<Long, String>>(this.numStrs.length);
	System.out.println("Loop times = " + this.loopNum);
	System.out.println("---------- ---------- ---------- ----------");
	for (final String numStr: this.numStrs)
	{
		final Map<String, Long> timeUsedCount =
				Collections.synchronizedMap(new HashMap<String, Long>());

		System.out.println("String: " + numStr);

		for (int loop = -1; ++loop != 7; )
		{
			final AtomicInteger runThreadNum = new AtomicInteger(0);

			// StringTool
			r = new Runnable()
			{
				public void run()
				{
					boolean hasEx = false;
					if (timeUsedCount.get("StringTool") == null)
						timeUsedCount.put("StringTool", 0L);
					long start = System.currentTimeMillis();
					for (int index = -1; ++index != FetchNumberTest.this.loopNum; )
					{
						try { StringTool.parseNumber(numStr); }
						catch (Exception ex) { hasEx = true; }
					}
					long use = System.currentTimeMillis() - start;
					System.out.print("\ttool:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
					use += timeUsedCount.get("StringTool");
					timeUsedCount.put("StringTool", use);
					runThreadNum.addAndGet(-1);
					Thread.yield();
				}
			};
			runThreadNum.addAndGet(1);
			new Thread(r).start();

			// JavaLib
			r = new Runnable()
			{
				public void run()
				{
					boolean hasEx = false;
					if (timeUsedCount.get("JavaLib") == null)
						timeUsedCount.put("JavaLib", 0L);
					long start = System.currentTimeMillis();
					for (int index = -1; ++index != FetchNumberTest.this.loopNum; )
					{
						try { FetchNumberTest.this.fetchNumberByJavaLib(numStr); }
						catch (Exception ex) { hasEx = true; }
					}
					long use = System.currentTimeMillis() - start;
					System.out.print("\tlib:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
					use += timeUsedCount.get("JavaLib");
					timeUsedCount.put("JavaLib", use);
					runThreadNum.addAndGet(-1);
					Thread.yield();
				}
			};
			runThreadNum.addAndGet(1);
			new Thread(r).start();

			// WholeRegex
			r = new Runnable()
			{
				public void run()
				{
					boolean hasEx = false;
					if (timeUsedCount.get("WholeRegex") == null)
						timeUsedCount.put("WholeRegex", 0L);
					long start = System.currentTimeMillis();
					for (int index = -1; ++index != FetchNumberTest.this.loopNum; )
					{
						try { FetchNumberTest.this.fetchNumberByRegex(numStr, false); }
						catch (Exception ex) { hasEx = true; }
					}
					long use = System.currentTimeMillis() - start;
					System.out.print("\treg:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
					use += timeUsedCount.get("WholeRegex");
					timeUsedCount.put("WholeRegex", use);
					runThreadNum.addAndGet(-1);
					Thread.yield();
				}
			};
			runThreadNum.addAndGet(1);
			new Thread(r).start();

			// Direct
			r = new Runnable()
			{
				public void run()
				{
					boolean hasEx = false;
					if (timeUsedCount.get("Direct") == null)
						timeUsedCount.put("Direct", 0L);
					long start = System.currentTimeMillis();
					for (int index = -1; ++index != FetchNumberTest.this.loopNum; )
					{
						try { FetchNumberTest.this.fetchNumberByDirect(numStr); }
						catch (Exception ex) { hasEx = true; }
					}
					long use = System.currentTimeMillis() - start;
					System.out.print("\t--:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
					use += timeUsedCount.get("Direct");
					timeUsedCount.put("Direct", use);
					runThreadNum.addAndGet(-1);
					Thread.yield();
				}
			};
			runThreadNum.addAndGet(1);
			new Thread(r).start();

			// WholeApacheUtils
			r = new Runnable()
			{
				public void run()
				{
					boolean hasEx = false;
					if (timeUsedCount.get("WholeApacheUtils") == null)
						timeUsedCount.put("WholeApacheUtils", 0L);
					long start = System.currentTimeMillis();
					for (int index = -1; ++index != FetchNumberTest.this.loopNum; )
					{
						try { FetchNumberTest.this.fetchNumberByApacheUtils(numStr); }
						catch (Exception ex) { hasEx = true; }
					}
					long use = System.currentTimeMillis() - start;
					System.out.print("\tap:" + use + "ms" + (hasEx ? "(EX)" : "") + '.');
					use += timeUsedCount.get("WholeApacheUtils");
					timeUsedCount.put("WholeApacheUtils", use);
					runThreadNum.addAndGet(-1);
					Thread.yield();
				}
			};
			runThreadNum.addAndGet(1);
			new Thread(r).start();

			while (runThreadNum.get() != 0)
			{
				try { Thread.sleep(10); } catch (Exception ex) { }
				Thread.yield();
			}

			for (int index = -1; ++index != 3; )
			{
				try { Thread.sleep(100); } catch (Exception ex) { }
				Thread.yield();
			}
			System.out.println();
		}

		SortedMap<Long, String> thisNumStrTimeUsed =
				Collections.synchronizedSortedMap(new TreeMap<Long, String>());
		for (String name: timeUsedCount.keySet())
			thisNumStrTimeUsed.put(timeUsedCount.get(name), name);
		timeUsed.add(thisNumStrTimeUsed);

		for (Long time: thisNumStrTimeUsed.keySet())
			System.out.println(thisNumStrTimeUsed.get(time) + ": " + time + " ms.");

		System.out.println("---------- ---------- ---------- ----------");
	}
}
 每种方法循环量 loopNum 设为 12001000 ,各种情况性能表现如下。
Performance test...
Loop times = 12001000
---------- ---------- ---------- ----------
String: 15
	tool:1201ms.	--:1810ms.	ap:2014ms.	lib:3058ms.	reg:5508ms.
	tool:982ms.	--:1577ms.	ap:1780ms.	lib:2746ms.	reg:5166ms.
	tool:967ms.	--:1577ms.	ap:1812ms.	lib:2810ms.	reg:5136ms.
	tool:983ms.	--:1498ms.	ap:1779ms.	lib:2590ms.	reg:5009ms.
	tool:920ms.	--:1452ms.	ap:1701ms.	lib:2590ms.	reg:5118ms.
	tool:998ms.	--:1514ms.	ap:1795ms.	lib:2668ms.	reg:5165ms.
	tool:998ms.	--:1545ms.	ap:1764ms.	lib:2700ms.	reg:5088ms.
StringTool: 7049 ms.
Direct: 10973 ms.
WholeApacheUtils: 12645 ms.
JavaLib: 19162 ms.
WholeRegex: 36190 ms.
---------- ---------- ---------- ----------
String: 	28 
	tool:1154ms.	--:1654ms.	ap:1966ms.	lib:2684ms.	reg:5353ms.
	tool:1123ms.	--:1670ms.	ap:1889ms.	lib:2762ms.	reg:5259ms.
	tool:1139ms.	--:1764ms.	ap:1889ms.	lib:2747ms.	reg:5460ms.
	tool:1168ms.	--:1715ms.	ap:1996ms.	lib:2776ms.	reg:5413ms.
	tool:1092ms.	--:1842ms.	ap:1842ms.	lib:2794ms.	reg:5447ms.
	tool:1170ms.	--:1655ms.	ap:1998ms.	lib:2793ms.	reg:5415ms.
	tool:1170ms.	--:1671ms.	ap:1858ms.	lib:2685ms.	reg:5338ms.
StringTool: 8016 ms.
Direct: 11971 ms.
WholeApacheUtils: 13438 ms.
JavaLib: 19241 ms.
WholeRegex: 37685 ms.
---------- ---------- ---------- ----------
String: 3.8
	tool:2121ms.	--:2793ms.	ap:3885ms.	lib:4775ms.	reg:7942ms.
	tool:2075ms.	--:2778ms.	ap:3808ms.	lib:4760ms.	reg:7724ms.
	tool:2137ms.	--:2840ms.	ap:4135ms.	lib:5230ms.	reg:8803ms.
	tool:2044ms.	--:2606ms.	ap:3480ms.	lib:4713ms.	reg:7959ms.
	tool:2013ms.	--:2451ms.	ap:3620ms.	lib:4760ms.	reg:8193ms.
	tool:1856ms.	--:2777ms.	ap:4150ms.	lib:5009ms.	reg:8223ms.
	tool:1872ms.	--:2403ms.	ap:3729ms.	lib:4887ms.	reg:8382ms.
StringTool: 14118 ms.
Direct: 18648 ms.
WholeApacheUtils: 26807 ms.
JavaLib: 34134 ms.
WholeRegex: 57226 ms.
---------- ---------- ---------- ----------
String: -99
	tool:1186ms.	--:1780ms.	ap:2248ms.	lib:3325ms.	reg:6649ms.
	tool:1108ms.	--:1826ms.	ap:2107ms.	lib:2981ms.	reg:6102ms.
	tool:1139ms.	--:1717ms.	ap:2170ms.	lib:3247ms.	reg:6446ms.
	tool:1154ms.	--:1701ms.	ap:1998ms.	lib:3074ms.	reg:6039ms.
	tool:1139ms.	--:1686ms.	ap:2029ms.	lib:3199ms.	reg:6508ms.
	tool:1123ms.	--:1655ms.	ap:1920ms.	lib:2902ms.	reg:5914ms.
	tool:1154ms.	--:1748ms.	ap:2123ms.	lib:3215ms.	reg:6773ms.
StringTool: 8003 ms.
Direct: 12113 ms.
WholeApacheUtils: 14595 ms.
JavaLib: 21943 ms.
WholeRegex: 44431 ms.
---------- ---------- ---------- ----------
String: .45
	tool:1763ms.	--:2435ms.	ap:3777ms.	lib:4651ms.	reg:7662ms.
	tool:1888ms.	--:2263ms.	ap:3823ms.	lib:4963ms.	reg:8131ms.
	tool:2153ms.	--:2965ms.	ap:4182ms.	lib:5321ms.	reg:8271ms.
	tool:1903ms.	--:2513ms.	ap:3667ms.	lib:4713ms.	reg:8130ms.
	tool:2075ms.	--:2684ms.	ap:3948ms.	lib:5041ms.	reg:8396ms.
	tool:2231ms.	--:2809ms.	ap:3995ms.	lib:5041ms.	reg:8208ms.
	tool:2230ms.	--:3183ms.	ap:4197ms.	lib:5306ms.	reg:8193ms.
StringTool: 14243 ms.
Direct: 18852 ms.
WholeApacheUtils: 27589 ms.
JavaLib: 35036 ms.
WholeRegex: 56991 ms.
---------- ---------- ---------- ----------
String: -.1
	tool:1825ms.	--:2434ms.	ap:4088ms.	lib:4963ms.	reg:7661ms.
	tool:2481ms.	--:3028ms.	ap:4525ms.	lib:5665ms.	reg:8084ms.
	tool:2153ms.	--:2529ms.	ap:4338ms.	lib:5072ms.	reg:7131ms.
	tool:2308ms.	--:3292ms.	ap:4322ms.	lib:5446ms.	reg:8224ms.
	tool:2543ms.	--:2918ms.	ap:4385ms.	lib:5415ms.	reg:8209ms.
	tool:2418ms.	--:3012ms.	ap:4213ms.	lib:5446ms.	reg:7959ms.
	tool:2247ms.	--:2700ms.	ap:3745ms.	lib:4823ms.	reg:7554ms.
StringTool: 15975 ms.
Direct: 19913 ms.
WholeApacheUtils: 29616 ms.
JavaLib: 36830 ms.
WholeRegex: 54822 ms.
---------- ---------- ---------- ----------
String: null
	--:47ms.	tool:125ms.	lib:140ms.	reg:156ms.	ap:156ms.
	--:31ms.	lib:94ms.	tool:94ms.	ap:78ms.	reg:125ms.
	--:16ms.	ap:95ms.	tool:95ms.	reg:126ms.	lib:126ms.
	--:31ms.	lib:94ms.	tool:94ms.	ap:94ms.	reg:109ms.
	--:47ms.	reg:94ms.	lib:94ms.	tool:94ms.	ap:94ms.
	--:47ms.	lib:94ms.	reg:110ms.	ap:63ms.	tool:125ms.
	--:15ms.	reg:78ms.	tool:78ms.	lib:110ms.	ap:110ms.
Direct: 234 ms.
WholeApacheUtils: 690 ms.
StringTool: 705 ms.
JavaLib: 752 ms.
WholeRegex: 798 ms.
---------- ---------- ---------- ----------
String: 0x3AFF0
	tool:406ms.	ap:579ms.	lib:1608ms.	reg:4760ms.	--:18146ms.
	tool:359ms.	ap:578ms.	lib:1763ms.	reg:5180ms.	--:17146ms.
	tool:328ms.	ap:530ms.	lib:1685ms.	reg:4775ms.	--:17303ms.
	tool:390ms.	ap:562ms.	lib:1607ms.	reg:5009ms.	--:15977ms.
	tool:359ms.	ap:546ms.	lib:1700ms.	reg:4728ms.	--:18519ms.
	tool:437ms.	ap:702ms.	lib:2107ms.	reg:5494ms.	--:16086ms.
	tool:374ms.	ap:515ms.	lib:1653ms.	reg:5055ms.	--:17224ms.
StringTool: 2653 ms.
WholeApacheUtils: 4012 ms.
JavaLib: 12123 ms.
WholeRegex: 35001 ms.
Direct: 120401 ms.
---------- ---------- ---------- ----------
String: 042
	tool:1170ms.	--:1795ms.	ap:2014ms.	lib:2934ms.	reg:7100ms.
	tool:1482ms.	--:2154ms.	ap:2481ms.	lib:3559ms.	reg:7788ms.
	tool:1373ms.	--:2029ms.	ap:2154ms.	lib:3261ms.	reg:7381ms.
	tool:1279ms.	--:1935ms.	ap:2201ms.	lib:3339ms.	reg:7459ms.
	tool:1279ms.	--:1936ms.	ap:2294ms.	lib:3450ms.	reg:7507ms.
	tool:1373ms.	--:1826ms.	ap:2122ms.	lib:3402ms.	reg:7583ms.
	tool:1513ms.	--:2356ms.	ap:2529ms.	lib:3668ms.	reg:7912ms.
StringTool: 9469 ms.
Direct: 14031 ms.
WholeApacheUtils: 15795 ms.
JavaLib: 23613 ms.
WholeRegex: 52730 ms.
---------- ---------- ---------- ----------
String: Spads
	tool:374ms.	ap:577ms.	lib:1825ms.	reg:3480ms.	--:15945ms.
	tool:281ms.	ap:405ms.	lib:1545ms.	reg:3090ms.	--:15930ms.
	tool:343ms.	ap:655ms.	lib:1716ms.	reg:3324ms.	--:16023ms.
	tool:265ms.	ap:546ms.	lib:1450ms.	reg:3136ms.	--:16148ms.
	tool:296ms.	ap:452ms.	lib:1482ms.	reg:3012ms.	--:18254ms.
	tool:297ms.	ap:468ms.	lib:1514ms.	reg:2903ms.	--:17131ms.
	tool:297ms.	ap:484ms.	lib:1482ms.	reg:2981ms.	--:15072ms.
StringTool: 2153 ms.
WholeApacheUtils: 3587 ms.
JavaLib: 11014 ms.
WholeRegex: 21926 ms.
Direct: 114503 ms.
---------- ---------- ---------- ----------
String: 
	tool:93ms.	lib:125ms.	ap:110ms.	reg:2231ms.	--:16334ms.
	tool:125ms.	lib:156ms.	ap:140ms.	reg:2683ms.	--:15539ms.
	tool:156ms.	lib:172ms.	ap:156ms.	reg:2746ms.	--:15570ms.
	tool:125ms.	ap:140ms.	lib:172ms.	reg:2418ms.	--:16272ms.
	tool:125ms.	lib:125ms.	ap:141ms.	reg:2153ms.	--:14431ms.
	lib:140ms.	tool:140ms.	ap:125ms.	reg:2589ms.	--:18627ms.
	tool:141ms.	ap:156ms.	lib:172ms.	reg:2777ms.	--:17909ms.
StringTool: 905 ms.
WholeApacheUtils: 968 ms.
JavaLib: 1062 ms.
WholeRegex: 17597 ms.
Direct: 114682 ms.
---------- ---------- ---------- ----------
String:       
	lib:405ms.	tool:421ms.	ap:406ms.	reg:2854ms.	--:18175ms.
	tool:452ms.	ap:468ms.	lib:530ms.	reg:3182ms.	--:18833ms.
	lib:499ms.	tool:499ms.	ap:530ms.	reg:2964ms.	--:15086ms.
	lib:406ms.	tool:421ms.	ap:405ms.	reg:2683ms.	--:17005ms.
	ap:375ms.	tool:421ms.	lib:453ms.	reg:3089ms.	--:18752ms.
	tool:514ms.	lib:500ms.	ap:546ms.	reg:3151ms.	--:17254ms.
	lib:484ms.	tool:484ms.	ap:546ms.	reg:3183ms.	--:14962ms.
StringTool: 3212 ms.
WholeApacheUtils: 3276 ms.
JavaLib: 3277 ms.
WholeRegex: 21106 ms.
Direct: 120067 ms.
---------- ---------- ---------- ----------
String: 5 - 3
	tool:530ms.	ap:905ms.	lib:1904ms.	reg:8487ms.	--:17490ms.
	tool:484ms.	ap:733ms.	lib:1483ms.	reg:7583ms.	--:18051ms.
	tool:514ms.	ap:686ms.	lib:1467ms.	reg:7707ms.	--:18285ms.
	tool:640ms.	ap:905ms.	lib:1904ms.	reg:8519ms.	--:17474ms.
	tool:437ms.	ap:702ms.	lib:1546ms.	reg:7942ms.	--:16881ms.
	tool:453ms.	ap:702ms.	lib:1546ms.	reg:8332ms.	--:18941ms.
	tool:593ms.	ap:749ms.	lib:1857ms.	reg:8582ms.	--:17335ms.
StringTool: 3651 ms.
WholeApacheUtils: 5382 ms.
JavaLib: 11707 ms.
WholeRegex: 57152 ms.
Direct: 124457 ms.
---------- ---------- ---------- ----------
String: 三十八
	tool:328ms.	ap:655ms.	lib:1857ms.	reg:3449ms.	--:17537ms.
	tool:265ms.	ap:483ms.	lib:1466ms.	reg:2731ms.	--:18020ms.
	tool:265ms.	ap:593ms.	lib:1685ms.	reg:3230ms.	--:16101ms.
	tool:265ms.	ap:468ms.	lib:1420ms.	reg:2981ms.	--:17287ms.
	tool:328ms.	ap:546ms.	lib:1591ms.	reg:3137ms.	--:17318ms.
	tool:390ms.	ap:624ms.	lib:1685ms.	reg:3012ms.	--:17568ms.
	tool:265ms.	ap:515ms.	lib:1513ms.	reg:2996ms.	--:18005ms.
StringTool: 2106 ms.
WholeApacheUtils: 3884 ms.
JavaLib: 11217 ms.
WholeRegex: 21536 ms.
Direct: 121836 ms.
---------- ---------- ---------- ----------
String: 3.1415926.5358
	tool:1248ms.	ap:1951ms.	lib:3558ms.	--:21202ms.	reg:46397ms.
	tool:1092ms.	ap:1873ms.	lib:3245ms.	--:20578ms.	reg:44931ms.
	tool:1310ms.	ap:2247ms.	lib:3963ms.	--:22497ms.	reg:46475ms.
	tool:1154ms.	ap:2013ms.	lib:3791ms.	--:21280ms.	reg:46428ms.
	tool:1186ms.	ap:1951ms.	lib:3371ms.	--:22139ms.	reg:49457ms.
	tool:1404ms.	ap:2310ms.	lib:3776ms.	--:21233ms.	reg:46881ms.
	tool:1108ms.	ap:1889ms.	lib:3277ms.	--:20968ms.	reg:43433ms.
StringTool: 8502 ms.
WholeApacheUtils: 14234 ms.
JavaLib: 24981 ms.
Direct: 149897 ms.
WholeRegex: 324002 ms.
---------- ---------- ---------- ----------
String: 39501.50
	tool:3744ms.	--:4088ms.	ap:5929ms.	lib:7318ms.	reg:15728ms.
	tool:3728ms.	--:4353ms.	ap:5835ms.	lib:7240ms.	reg:14511ms.
	tool:3292ms.	--:4104ms.	ap:5914ms.	lib:7225ms.	reg:15166ms.
	tool:3198ms.	--:3994ms.	ap:5773ms.	lib:6991ms.	reg:15775ms.
	tool:3417ms.	--:3698ms.	ap:5633ms.	lib:7038ms.	reg:15198ms.
	tool:3260ms.	--:4151ms.	ap:6132ms.	lib:7474ms.	reg:15993ms.
	tool:3214ms.	--:3620ms.	ap:5399ms.	lib:6788ms.	reg:15603ms.
StringTool: 23853 ms.
Direct: 28008 ms.
WholeApacheUtils: 40615 ms.
JavaLib: 50074 ms.
WholeRegex: 107974 ms.
---------- ---------- ---------- ----------
String: 0.000385427
	tool:4228ms.	--:4603ms.	ap:6444ms.	lib:8177ms.	reg:17944ms.
	tool:4087ms.	--:4431ms.	ap:6678ms.	lib:8301ms.	reg:17834ms.
	tool:3775ms.	--:4306ms.	ap:6241ms.	lib:7849ms.	reg:18801ms.
	tool:3994ms.	--:4307ms.	ap:6709ms.	lib:7771ms.	reg:19269ms.
	tool:3526ms.	--:4307ms.	ap:6053ms.	lib:7693ms.	reg:18177ms.
	tool:3884ms.	--:4073ms.	ap:6381ms.	lib:7880ms.	reg:19269ms.
	tool:3447ms.	--:3760ms.	ap:5711ms.	lib:7225ms.	reg:17959ms.
StringTool: 26941 ms.
Direct: 29787 ms.
WholeApacheUtils: 44217 ms.
JavaLib: 54896 ms.
WholeRegex: 129253 ms.
---------- ---------- ---------- ----------
String: .33796678
	tool:3525ms.	--:3916ms.	ap:5648ms.	lib:6928ms.	reg:15712ms.
	tool:3292ms.	--:3745ms.	ap:5711ms.	lib:7178ms.	reg:15556ms.
	tool:3775ms.	--:4119ms.	ap:6007ms.	lib:7287ms.	reg:16071ms.
	tool:3463ms.	--:3823ms.	ap:5601ms.	lib:7069ms.	reg:14246ms.
	tool:3401ms.	--:4026ms.	ap:5991ms.	lib:7162ms.	reg:15884ms.
	tool:3806ms.	--:4291ms.	ap:5898ms.	lib:7396ms.	reg:15915ms.
	tool:3884ms.	--:4088ms.	ap:5960ms.	lib:7553ms.	reg:15946ms.
StringTool: 25146 ms.
Direct: 28008 ms.
WholeApacheUtils: 40816 ms.
JavaLib: 50573 ms.
WholeRegex: 109330 ms.
---------- ---------- ---------- ----------
String: 300011110000
	tool:3213ms.	--:4322ms.	ap:4869ms.	lib:6522ms.	reg:16897ms.
	tool:3354ms.	--:4010ms.	ap:4899ms.	lib:6647ms.	reg:18614ms.
	tool:3260ms.	--:3792ms.	ap:4463ms.	lib:6117ms.	reg:17178ms.
	tool:3276ms.	--:3776ms.	ap:4696ms.	lib:6444ms.	reg:17678ms.
	tool:3057ms.	--:3838ms.	ap:4853ms.	lib:6382ms.	reg:17351ms.
	tool:3448ms.	--:4197ms.	ap:5040ms.	lib:6492ms.	reg:18489ms.
	tool:3588ms.	--:4322ms.	ap:5727ms.	lib:7880ms.	reg:20377ms.
StringTool: 23196 ms.
Direct: 28257 ms.
WholeApacheUtils: 34547 ms.
JavaLib: 46484 ms.
WholeRegex: 126584 ms.
---------- ---------- ---------- ----------
String: 300 1500 1010
	tool:3432ms.	--:3855ms.	ap:5134ms.	lib:6476ms.	reg:18739ms.
	tool:3604ms.	--:3963ms.	ap:5055ms.	lib:6367ms.	reg:19409ms.
	tool:3432ms.	--:3526ms.	ap:4822ms.	lib:5945ms.	reg:18754ms.
	tool:2683ms.	--:3371ms.	ap:4712ms.	lib:6258ms.	reg:18239ms.
	tool:3198ms.	--:3604ms.	ap:4900ms.	lib:6132ms.	reg:19472ms.
	tool:3213ms.	--:3620ms.	ap:4790ms.	lib:6133ms.	reg:17444ms.
	tool:3135ms.	--:3823ms.	ap:5367ms.	lib:6679ms.	reg:18941ms.
StringTool: 22697 ms.
Direct: 25762 ms.
WholeApacheUtils: 34780 ms.
JavaLib: 43990 ms.
WholeRegex: 130998 ms.
---------- ---------- ---------- ----------
String: -570 0015 6726
	tool:3323ms.	--:3527ms.	ap:5290ms.	lib:6508ms.	reg:18911ms.
	--:3588ms.	tool:3667ms.	ap:5273ms.	lib:6554ms.	reg:18224ms.
	tool:3588ms.	--:4072ms.	ap:5071ms.	lib:6616ms.	reg:18926ms.
	tool:3697ms.	--:3948ms.	ap:5196ms.	lib:6304ms.	reg:18910ms.
	tool:3698ms.	--:4135ms.	ap:5086ms.	lib:6632ms.	reg:17382ms.
	tool:3760ms.	--:3932ms.	ap:5445ms.	lib:6726ms.	reg:19612ms.
	tool:3448ms.	--:3776ms.	ap:5103ms.	lib:6227ms.	reg:18365ms.
StringTool: 25181 ms.
Direct: 26978 ms.
WholeApacheUtils: 36464 ms.
JavaLib: 45567 ms.
WholeRegex: 130330 ms.
---------- ---------- ---------- ----------
String: -2378957832975
	tool:3354ms.	--:4041ms.	ap:5243ms.	lib:6897ms.	reg:18660ms.
	tool:3338ms.	--:3886ms.	ap:5086ms.	lib:7068ms.	reg:19347ms.
	tool:3978ms.	--:3962ms.	ap:5414ms.	lib:7006ms.	reg:18848ms.
	tool:3884ms.	--:4525ms.	ap:5352ms.	lib:6928ms.	reg:18551ms.
	tool:3962ms.	--:4525ms.	ap:5508ms.	lib:6959ms.	reg:19159ms.
	tool:4196ms.	--:4306ms.	ap:5164ms.	lib:7161ms.	reg:19018ms.
	tool:3931ms.	--:3979ms.	ap:5118ms.	lib:6913ms.	reg:17943ms.
StringTool: 26643 ms.
Direct: 29224 ms.
WholeApacheUtils: 36885 ms.
JavaLib: 48932 ms.
WholeRegex: 131526 ms.
---------- ---------- ---------- ----------
String: 1370 0000
	tool:2262ms.	--:2825ms.	ap:3621ms.	lib:5041ms.	reg:13778ms.
	tool:2559ms.	--:3184ms.	ap:4010ms.	lib:5213ms.	reg:15432ms.
	tool:2434ms.	--:3199ms.	ap:4057ms.	lib:5259ms.	reg:14028ms.
	tool:2153ms.	--:2778ms.	ap:3964ms.	lib:5228ms.	reg:13778ms.
	tool:2480ms.	--:2965ms.	ap:3979ms.	lib:4947ms.	reg:15009ms.
	tool:2309ms.	--:2918ms.	ap:3761ms.	lib:4916ms.	reg:13872ms.
	tool:2465ms.	--:2965ms.	ap:3714ms.	lib:5041ms.	reg:14760ms.
StringTool: 16662 ms.
Direct: 20834 ms.
WholeApacheUtils: 27106 ms.
JavaLib: 35645 ms.
WholeRegex: 100657 ms.
---------- ---------- ---------- ----------
String: 2015773
	tool:1872ms.	--:2341ms.	ap:3012ms.	lib:4463ms.	reg:11422ms.
	tool:2387ms.	--:3449ms.	ap:3466ms.	lib:4854ms.	reg:11641ms.
	tool:1919ms.	--:2840ms.	ap:3121ms.	lib:4401ms.	reg:12264ms.
	tool:1685ms.	--:2325ms.	ap:2934ms.	lib:4635ms.	reg:12030ms.
	tool:2012ms.	--:2497ms.	ap:3214ms.	lib:4666ms.	reg:11172ms.
	tool:2184ms.	--:3043ms.	ap:3495ms.	lib:4854ms.	reg:12780ms.
	tool:2496ms.	--:3152ms.	ap:3667ms.	lib:4932ms.	reg:13076ms.
StringTool: 14555 ms.
Direct: 19647 ms.
WholeApacheUtils: 22909 ms.
JavaLib: 32805 ms.
WholeRegex: 84385 ms.
---------- ---------- ---------- ----------
String: 3850
	tool:1778ms.	--:2653ms.	ap:3137ms.	lib:4401ms.	reg:9878ms.
	tool:1669ms.	--:2466ms.	ap:2824ms.	lib:4105ms.	reg:8739ms.
	tool:1326ms.	--:2107ms.	ap:2513ms.	lib:3621ms.	reg:8817ms.
	tool:1794ms.	--:2501ms.	ap:2844ms.	lib:4000ms.	reg:8603ms.
	tool:1326ms.	--:1904ms.	ap:2326ms.	lib:3527ms.	reg:8957ms.
	tool:1731ms.	--:2342ms.	ap:2810ms.	lib:4059ms.	reg:9116ms.
	tool:1623ms.	--:2560ms.	ap:2746ms.	lib:3840ms.	reg:9364ms.
StringTool: 11247 ms.
Direct: 16533 ms.
WholeApacheUtils: 19200 ms.
JavaLib: 27553 ms.
WholeRegex: 63474 ms.
---------- ---------- ---------- ----------
String: -80058
	tool:2199ms.	--:2871ms.	ap:3370ms.	lib:4510ms.	reg:9768ms.
	tool:1809ms.	--:2232ms.	ap:2809ms.	lib:4337ms.	reg:10516ms.
	tool:2200ms.	--:2669ms.	ap:3371ms.	lib:4433ms.	reg:10611ms.
	tool:1779ms.	--:2326ms.	ap:2684ms.	lib:3761ms.	reg:9643ms.
	tool:2106ms.	--:2809ms.	ap:3370ms.	lib:4729ms.	reg:10424ms.
	tool:1654ms.	--:2326ms.	ap:3215ms.	lib:4651ms.	reg:10362ms.
	tool:1716ms.	--:1966ms.	ap:2762ms.	lib:4261ms.	reg:10393ms.
StringTool: 13463 ms.
Direct: 17199 ms.
WholeApacheUtils: 21581 ms.
JavaLib: 30682 ms.
WholeRegex: 71717 ms.
---------- ---------- ---------- ----------
String: -1 2853 2781
	tool:3073ms.	--:3370ms.	ap:4525ms.	lib:5633ms.	reg:17475ms.
	tool:2558ms.	--:3027ms.	ap:4634ms.	lib:5556ms.	reg:16101ms.
	tool:3292ms.	--:3590ms.	ap:4478ms.	lib:5915ms.	reg:16009ms.
	tool:2762ms.	--:2918ms.	ap:4728ms.	lib:5821ms.	reg:16727ms.
	tool:2730ms.	--:2996ms.	ap:4650ms.	lib:5758ms.	reg:16102ms.
	tool:2949ms.	--:3386ms.	ap:4354ms.	lib:5743ms.	reg:15447ms.
	tool:2762ms.	--:3792ms.	ap:5634ms.	lib:6743ms.	reg:16696ms.
StringTool: 20126 ms.
Direct: 23079 ms.
WholeApacheUtils: 33003 ms.
JavaLib: 41169 ms.
WholeRegex: 114557 ms.
---------- ---------- ---------- ----------
String: 3
	tool:795ms.	--:1483ms.	ap:1935ms.	lib:2810ms.	reg:6179ms.
	tool:874ms.	--:1733ms.	ap:1810ms.	lib:2638ms.	reg:6102ms.
	tool:921ms.	--:1733ms.	ap:1810ms.	lib:2903ms.	reg:6476ms.
	tool:983ms.	--:1717ms.	ap:1858ms.	lib:2716ms.	reg:5978ms.
	tool:1030ms.	--:1702ms.	ap:1764ms.	lib:2778ms.	reg:6040ms.
	tool:842ms.	--:1842ms.	ap:1904ms.	lib:2966ms.	reg:6274ms.
	tool:858ms.	--:1515ms.	ap:1655ms.	lib:2513ms.	reg:5884ms.
StringTool: 6303 ms.
Direct: 11725 ms.
WholeApacheUtils: 12736 ms.
JavaLib: 19324 ms.
WholeRegex: 42933 ms.
---------- ---------- ---------- ----------
String: 0
	tool:967ms.	--:1795ms.	ap:1966ms.	lib:3043ms.	reg:6226ms.
	tool:921ms.	--:1733ms.	ap:2014ms.	lib:3029ms.	reg:6212ms.
	tool:764ms.	--:1452ms.	ap:1608ms.	lib:2357ms.	reg:5883ms.
	tool:921ms.	--:1515ms.	ap:1701ms.	lib:2575ms.	reg:6040ms.
	tool:764ms.	--:1499ms.	ap:1576ms.	lib:2762ms.	reg:5980ms.
	tool:811ms.	--:1452ms.	ap:1671ms.	lib:2466ms.	reg:5696ms.
	tool:842ms.	--:1576ms.	ap:1733ms.	lib:2747ms.	reg:6070ms.
StringTool: 5990 ms.
Direct: 11022 ms.
WholeApacheUtils: 12269 ms.
JavaLib: 18979 ms.
WholeRegex: 42107 ms.
---------- ---------- ---------- ----------
String: -597213859710830758190748937102794
	tool:24039ms.	ap:26162ms.	lib:28955ms.	--:31283ms.	reg:56618ms.
	tool:23681ms.	ap:25897ms.	lib:29876ms.	--:31235ms.	reg:56960ms.
	tool:25194ms.	ap:26927ms.	--:29237ms.	lib:29441ms.	reg:57038ms.
	tool:24367ms.	ap:25367ms.	lib:29034ms.	--:31250ms.	reg:55431ms.
	tool:26567ms.	ap:28689ms.	lib:30859ms.	--:34152ms.	reg:61110ms.
	tool:25225ms.	ap:26085ms.	lib:29657ms.	--:31266ms.	reg:58692ms.
	tool:25366ms.	ap:27332ms.	lib:29611ms.	--:30813ms.	reg:58005ms.
StringTool: 174439 ms.
WholeApacheUtils: 186459 ms.
JavaLib: 207433 ms.
Direct: 219236 ms.
WholeRegex: 403854 ms.
---------- ---------- ---------- ----------
String: 78 9254 3253 2452 9572 8532
	tool:21731ms.	ap:23136ms.	lib:23949ms.	--:28848ms.	reg:47928ms.
	tool:20467ms.	ap:22403ms.	lib:23636ms.	--:27942ms.	reg:49237ms.
	tool:19625ms.	ap:22964ms.	lib:24385ms.	--:27849ms.	reg:48177ms.
	tool:21653ms.	ap:23775ms.	lib:24650ms.	--:27614ms.	reg:49236ms.
	tool:20795ms.	ap:23073ms.	lib:24088ms.	--:29283ms.	reg:48347ms.
	tool:21279ms.	ap:23167ms.	lib:23761ms.	--:29143ms.	reg:48925ms.
	tool:20795ms.	ap:22247ms.	lib:24619ms.	--:27725ms.	reg:48864ms.
StringTool: 146345 ms.
WholeApacheUtils: 160765 ms.
JavaLib: 169088 ms.
Direct: 198404 ms.
WholeRegex: 340714 ms.
---------- ---------- ---------- ----------
String: 13205972138597109830758190748937102794
	--:29078ms.	tool:47706ms.	ap:49001ms.	lib:49564ms.	reg:80750ms.
	--:34102ms.	tool:49079ms.	ap:49392ms.	lib:51841ms.	reg:82761ms.
	--:28828ms.	tool:45677ms.	ap:47051ms.	lib:49782ms.	reg:81139ms.
	--:28407ms.	tool:45553ms.	ap:48003ms.	lib:49829ms.	reg:79642ms.
	--:27175ms.	tool:46208ms.	ap:47800ms.	lib:48956ms.	reg:79704ms.
	--:34757ms.	tool:54679ms.	ap:56864ms.	lib:57816ms.	reg:88221ms.
	--:28486ms.	tool:47721ms.	ap:48892ms.	lib:49065ms.	reg:81014ms.
Direct: 210833 ms.
StringTool: 336623 ms.
WholeApacheUtils: 347003 ms.
JavaLib: 356853 ms.
WholeRegex: 573231 ms.
---------- ---------- ---------- ----------
 我们可以看到,除了 null 以及被转换数字太长的情况(30位数字以上)外,Spads 出品的 StringTool 都有非常明显的速度优势。 null 本身处理速度远远快于其它情况,即便是 StringTool ,也没有耽误多少执行时间。



5 【附录】
---------- ---------- ---------- ----------
Google 搜索 “Java 判断字符串是否为数值”
头条 http://www.blogjava.net/Javaphua/archive/2012/05/18/122131.html
显得方法最多 http://javapub.iteye.com/blog/666544
直转法 http://zhidao.baidu.com/question/38323159
无新意 http://www.ideagrace.com/html/doc/2006/04/28/00784.html
阿帕奇工具 http://blog.csdn.net/jojoy_828/article/details/2837784
比较全 http://blog.csdn.net/bluesuperman/article/details/1840952
无新意 http://www.cnblogs.com/ztf2008/archive/2009/02/19/1393744.html
无新意 http://wenwen.soso.com/z/q198258851.htm

Google 搜索 “Java 从字符串中提取数值”
头条 http://blog.csdn.net/wangjinyu501/article/details/7625636
无新意 http://bbs.csdn.net/topics/340240275
复数,但我这边需求不涉及 http://www.bcw52.com/JAVA/2471.html

Google 搜索“Java 字符串转化为数值”
http://dongdong1314.blog.51cto.com/389953/79385

CSDN 搜索 “Java 从字符串中提取数值”
对于判断为数值后的转换有详细描述 http://blog.csdn.net/wowwow_cai/article/details/4590553
同 Google http://blog.csdn.net/wangjinyu501/article/details/7625636


本文还发表在我的其它技术日志
CSDN : http://blog.csdn.net/shanelooli/article/details/8432250
中国开源社区: http://my.oschina.net/shane1984/blog/98088
51CTO :

【综述】 http://shanelooli.blog.51cto.com/5523233/1100063

【程序】 http://shanelooli.blog.51cto.com/5523233/1100074

【功能】 http://shanelooli.blog.51cto.com/blog/5523233/1100356

【性能】甲 http://shanelooli.blog.51cto.com/5523233/1100417

【性能】乙 51CTO - Spads字符

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics