Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
39213c1f
Commit
39213c1f
authored
Dec 13, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strconv: some allocation tests
R=rsc, r CC=golang-dev
https://golang.org/cl/5477084
parent
bcbb2f93
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
0 deletions
+45
-0
ftoa_test.go
src/pkg/strconv/ftoa_test.go
+17
-0
itoa_test.go
src/pkg/strconv/itoa_test.go
+28
-0
No files found.
src/pkg/strconv/ftoa_test.go
View file @
39213c1f
...
...
@@ -149,6 +149,23 @@ func TestFtoa(t *testing.T) {
}
}
func
TestAppendFloatDoesntAllocate
(
t
*
testing
.
T
)
{
n
:=
numAllocations
(
func
()
{
var
buf
[
64
]
byte
AppendFloat
(
buf
[
:
0
],
1.23
,
'g'
,
5
,
64
)
})
want
:=
1
// TODO(bradfitz): this might be 0, once escape analysis is better
if
n
!=
want
{
t
.
Errorf
(
"with local buffer, did %d allocations, want %d"
,
n
,
want
)
}
n
=
numAllocations
(
func
()
{
AppendFloat
(
globalBuf
[
:
0
],
1.23
,
'g'
,
5
,
64
)
})
if
n
!=
0
{
t
.
Errorf
(
"with reused buffer, did %d allocations, want 0"
,
n
)
}
}
func
BenchmarkFormatFloatDecimal
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
FormatFloat
(
33909
,
'g'
,
-
1
,
64
)
...
...
src/pkg/strconv/itoa_test.go
View file @
39213c1f
...
...
@@ -5,6 +5,7 @@
package
strconv_test
import
(
"runtime"
.
"strconv"
"testing"
)
...
...
@@ -125,6 +126,33 @@ func TestUitoa(t *testing.T) {
}
}
func
numAllocations
(
f
func
())
int
{
runtime
.
UpdateMemStats
()
n0
:=
runtime
.
MemStats
.
Mallocs
f
()
runtime
.
UpdateMemStats
()
return
int
(
runtime
.
MemStats
.
Mallocs
-
n0
)
}
var
globalBuf
[
64
]
byte
func
TestAppendUintDoesntAllocate
(
t
*
testing
.
T
)
{
n
:=
numAllocations
(
func
()
{
var
buf
[
64
]
byte
AppendInt
(
buf
[
:
0
],
123
,
10
)
})
want
:=
1
// TODO(bradfitz): this might be 0, once escape analysis is better
if
n
!=
want
{
t
.
Errorf
(
"with local buffer, did %d allocations, want %d"
,
n
,
want
)
}
n
=
numAllocations
(
func
()
{
AppendInt
(
globalBuf
[
:
0
],
123
,
10
)
})
if
n
!=
0
{
t
.
Errorf
(
"with reused buffer, did %d allocations, want 0"
,
n
)
}
}
func
BenchmarkFormatInt
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
for
_
,
test
:=
range
itob64tests
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment