Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved parseDouble & parseFloat perforamnce
- Loading branch information
Showing
4 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/test/java/com/alibaba/json/bvt/parser/TypeUtils_parseDouble_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.alibaba.json.bvt.parser; | ||
|
||
import com.alibaba.fastjson.util.TypeUtils; | ||
import junit.framework.TestCase; | ||
|
||
import java.util.Random; | ||
|
||
public class TypeUtils_parseDouble_Test extends TestCase { | ||
public void test_0() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Float.toString(r.nextFloat()); | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
|
||
public void test_0_d() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Double.toString(r.nextDouble()); | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
|
||
|
||
public void test_1() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Integer.toString(r.nextInt()); | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
|
||
public void test_2() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Integer.toString(r.nextInt(1000000000)); | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
|
||
public void test_3() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Long.toString(r.nextLong()); | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
|
||
public void test_4() throws Exception { | ||
String[] array = new String[] { | ||
"0.34856254", | ||
"1", | ||
"12", | ||
"123", | ||
"1234", | ||
"12345", | ||
"123456", | ||
"1234567", | ||
"12345678", | ||
"123456789", | ||
"1234567890", | ||
".1" | ||
,"1.1" | ||
,"12.1" | ||
, "123.1" | ||
, "1234.1" | ||
, "12345.1" | ||
, "123456.1" | ||
, "1234567.1" | ||
, "12345678.1" | ||
, "0.1" | ||
, "0.12" | ||
, "0.123" | ||
, "0.1234" | ||
, "0.12345" | ||
, "0.123456" | ||
, "0.1234567" | ||
, "0.12345678" | ||
, "0.123456789" | ||
, "0.1234567891" | ||
, "0.12345678901" | ||
, "0.123456789012" | ||
}; | ||
|
||
for (String str : array) { | ||
assertEquals(Double.parseDouble(str), TypeUtils.parseDouble(str)); | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/test/java/com/alibaba/json/bvt/parser/TypeUtils_parseFloat_Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.alibaba.json.bvt.parser; | ||
|
||
import com.alibaba.fastjson.util.TypeUtils; | ||
import junit.framework.TestCase; | ||
|
||
import java.util.Random; | ||
|
||
public class TypeUtils_parseFloat_Test extends TestCase { | ||
public void test_0() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Float.toString(r.nextFloat()); | ||
assertEquals(Float.parseFloat(str), TypeUtils.parseFloat(str)); | ||
} | ||
} | ||
|
||
public void test_1() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Integer.toString(r.nextInt()); | ||
assertEquals(Float.parseFloat(str), TypeUtils.parseFloat(str)); | ||
} | ||
} | ||
|
||
public void test_2() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Integer.toString(r.nextInt(1000000000)); | ||
assertEquals(Float.parseFloat(str), TypeUtils.parseFloat(str)); | ||
} | ||
} | ||
|
||
public void test_3() throws Exception { | ||
Random r = new Random(); | ||
|
||
for (int i = 0; i < 1000 * 1000; ++i) { | ||
String str = Long.toString(r.nextLong()); | ||
assertEquals(Float.parseFloat(str), TypeUtils.parseFloat(str)); | ||
} | ||
} | ||
|
||
public void test_4() throws Exception { | ||
String[] array = new String[] { | ||
"0.34856254", | ||
"1", | ||
"12", | ||
"123", | ||
"1234", | ||
"12345", | ||
"123456", | ||
"1234567", | ||
"12345678", | ||
"123456789", | ||
"1234567890", | ||
".1" | ||
,"1.1" | ||
,"12.1" | ||
, "123.1" | ||
, "1234.1" | ||
, "12345.1" | ||
, "123456.1" | ||
, "1234567.1" | ||
, "12345678.1" | ||
, "0.1" | ||
, "0.12" | ||
, "0.123" | ||
, "0.1234" | ||
, "0.12345" | ||
, "0.123456" | ||
, "0.1234567" | ||
, "0.12345678" | ||
, "0.123456789" | ||
, "0.1234567891" | ||
, "0.12345678901" | ||
, "0.123456789012" | ||
}; | ||
|
||
for (String str : array) { | ||
assertEquals(Float.parseFloat(str), TypeUtils.parseFloat(str)); | ||
} | ||
} | ||
} |