diff options
author | Albert Nigmatzianov <albertnigma@gmail.com> | 2017-05-22 21:39:38 +0200 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2017-05-22 21:59:34 +0000 |
commit | 4cf19fb5a73a33f838efe21b3baa14167469ac83 (patch) | |
tree | 755f51a56263f401d2e7c28868d39a9f33241b95 /src/log | |
parent | d433de6e86ce08e1b33e5d604080d3f504529006 (diff) | |
download | go-4cf19fb5a73a33f838efe21b3baa14167469ac83.tar.gz go-4cf19fb5a73a33f838efe21b3baa14167469ac83.zip |
log: Prevent getting time if it's unnecessary
Small performance gain:
name old time/op new time/op delta
Itoa-4 95.4ns ± 4% 95.6ns ± 3% ~ (p=0.256 n=45+46)
Println-4 480ns ± 4% 476ns ± 5% -0.87% (p=0.003 n=45+45)
PrintlnNoFlags-4 316ns ± 3% 299ns ± 4% -5.38% (p=0.000 n=42+44)
name old alloc/op new alloc/op delta
Itoa-4 0.00B 0.00B ~ (all equal)
Println-4 21.0B ± 0% 21.0B ± 0% ~ (all equal)
PrintlnNoFlags-4 21.0B ± 0% 21.0B ± 0% ~ (all equal)
name old allocs/op new allocs/op delta
Itoa-4 0.00 0.00 ~ (all equal)
Println-4 2.00 ± 0% 2.00 ± 0% ~ (all equal)
PrintlnNoFlags-4 2.00 ± 0% 2.00 ± 0% ~ (all equal)
Change-Id: Idcd03609a5a437a69ffa7004a673bf0b8d22e7ad
Reviewed-on: https://go-review.googlesource.com/38056
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/log')
-rw-r--r-- | src/log/log.go | 18 | ||||
-rw-r--r-- | src/log/log_test.go | 10 |
2 files changed, 23 insertions, 5 deletions
diff --git a/src/log/log.go b/src/log/log.go index 58b8788be4..0ea4b89658 100644 --- a/src/log/log.go +++ b/src/log/log.go @@ -72,7 +72,7 @@ func (l *Logger) SetOutput(w io.Writer) { var std = New(os.Stderr, "", LstdFlags) -// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. +// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. func itoa(buf *[]byte, i int, wid int) { // Assemble decimal in reverse order. var b [20]byte @@ -89,12 +89,16 @@ func itoa(buf *[]byte, i int, wid int) { *buf = append(*buf, b[bp:]...) } +// formatHeader writes log header to buf in following order: +// * l.prefix (if it's not blank), +// * date and/or time (if corresponding flags are provided), +// * file and line number (if corresponding flags are provided). func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { *buf = append(*buf, l.prefix...) - if l.flag&LUTC != 0 { - t = t.UTC() - } if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + if l.flag&LUTC != 0 { + t = t.UTC() + } if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) @@ -143,7 +147,11 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { // provided for generality, although at the moment on all pre-defined // paths it will be 2. func (l *Logger) Output(calldepth int, s string) error { - now := time.Now() // get this early. + // Get time early if we need it. + var now time.Time + if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + now = time.Now() + } var file string var line int l.mu.Lock() diff --git a/src/log/log_test.go b/src/log/log_test.go index dd16c9d3e1..966fdf306b 100644 --- a/src/log/log_test.go +++ b/src/log/log_test.go @@ -182,3 +182,13 @@ func BenchmarkPrintln(b *testing.B) { l.Println(testString) } } + +func BenchmarkPrintlnNoFlags(b *testing.B) { + const testString = "test" + var buf bytes.Buffer + l := New(&buf, "", 0) + for i := 0; i < b.N; i++ { + buf.Reset() + l.Println(testString) + } +} |