Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

perf: eliminate allocations in scanner and formatter#197

Open
wibobm wants to merge 1 commit into
ericlagergren:master from
wibobm:master
Open

perf: eliminate allocations in scanner and formatter #197
wibobm wants to merge 1 commit into
ericlagergren:master from
wibobm:master

Conversation

@wibobm

@wibobm wibobm commented May 14, 2026

Copy link
Copy Markdown
  1. Scanning (SetString):
    • Time: ~15.7% faster.
    • Memory: Allocations were completely eliminated. It went from 32 B/op (1 allocation) down to 0 B/op (0 allocations).
  2. Formatting (String()):
    • Time: ~17.3% faster.
    • Memory: Saved 48 bytes and exactly 1 allocation per operation (from 5 allocs to 4 allocs).
  3. Formatting Large Zeros (fmt.Sprintf("%.100f", &z)):
    • Time: ~6.5% faster.
    • Memory: Saved 48 bytes and 1 allocation per operation.
package decimal_test
import (
	"fmt"
	"testing"
	"github.com/ericlagergren/decimal"
)
func BenchmarkScan(b *testing.B) {
	s := "123456789.987654321E+123"
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		var z decimal.Big
		z.SetString(s)
	}
}
func BenchmarkFormat(b *testing.B) {
	s := "123456789.987654321E+123"
	var z decimal.Big
	z.SetString(s)
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		_ = z.String()
	}
}
func BenchmarkFormatLargeZeros(b *testing.B) {
	s := "1.23"
	var z decimal.Big
	z.SetString(s)
	b.ResetTimer()
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		_ = fmt.Sprintf("%.100f", &z)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /