mirror of
https://github.com/json-iterator/go.git
synced 2024-11-27 08:30:57 +02:00
optimize read float
This commit is contained in:
parent
44a65aa13a
commit
ec19f6de6a
58
jsoniter.go
58
jsoniter.go
@ -133,6 +133,8 @@ func (iter *Iterator) readByte() (ret byte) {
|
||||
ret = iter.buf[iter.head]
|
||||
iter.head++
|
||||
return ret
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
ret = iter.buf[iter.head]
|
||||
@ -557,16 +559,26 @@ func (iter *Iterator) readObjectField() (ret string) {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||
str := make([]byte, 0, 4)
|
||||
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
|
||||
switch c {
|
||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
iter.unreadByte()
|
||||
strBuf := [8]byte{}
|
||||
str := strBuf[0:0]
|
||||
hasMore := true
|
||||
for(hasMore) {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
hasMore = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasMore {
|
||||
if !iter.loadMore() {
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return
|
||||
@ -580,16 +592,26 @@ func (iter *Iterator) ReadFloat32() (ret float32) {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadFloat64() (ret float64) {
|
||||
str := make([]byte, 0, 4)
|
||||
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() {
|
||||
switch c {
|
||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
iter.unreadByte()
|
||||
strBuf := [8]byte{}
|
||||
str := strBuf[0:0]
|
||||
hasMore := true
|
||||
for(hasMore) {
|
||||
for i := iter.head; i < iter.tail; i++ {
|
||||
c := iter.buf[i]
|
||||
switch c {
|
||||
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||
str = append(str, c)
|
||||
continue
|
||||
default:
|
||||
hasMore = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasMore {
|
||||
if !iter.loadMore() {
|
||||
break
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
if iter.Error != nil && iter.Error != io.EOF {
|
||||
return
|
||||
|
@ -3,6 +3,7 @@ package jsoniter
|
||||
import (
|
||||
"testing"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Test_float64_0(t *testing.T) {
|
||||
@ -25,13 +26,15 @@ func Test_float32_1_dot_1_comma(t *testing.T) {
|
||||
iter := ParseString(`1.1,`)
|
||||
val := iter.ReadFloat32()
|
||||
if val != 1.1 {
|
||||
fmt.Println(iter.Error)
|
||||
t.Fatal(val)
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_jsoniter_float(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for n := 0; n < b.N; n++ {
|
||||
iter := ParseString(`1.1`)
|
||||
iter := ParseString(`1.1111111111`)
|
||||
iter.ReadFloat64()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user