要完全禁用日志,最好调用
log.SetFlags(0)Joril并将输出设置为无 *** 作
io.Writer(例如
log.SetOutput(ioutil.Discard))
但即使在此之后, *** 作仍将闲置约500-600 ns / op 1
这仍然可切断短路(左右 为100 ns / OP )通过使用自定义
Logger的实现,并实现所有功能进行任何 *** 作-
这表现在这里(只覆盖
Println了bervity)。
所有这些的替代方法是使用具有级别的自定义日志记录框架,并将其设置为OFF。
但是请注意,常用的日志记录库之一(logrus)会对性能产生影响 -在基准测试中,它的性能均达到 3K + ns / op ,无论如何。
偏见的意见:从基准,图书馆 去,记录在参数与自定义执行Logger
执行设定时,Level
到-1
,无论后端和格式
(可以在此处找到基准源)
基准的输出如下:
testing: warning: no tests to runPASSBenchmarkGoLogging-4 1000000 2068 ns/opBenchmarkGoLoggingNullBackend-4 5000000308 ns/opBenchmarkGoLoggingNullBackendWithFancyFormatter-4 3000000435 ns/opBenchmarkGoLoggingOffLevel-4 20000000109 ns/opBenchmarkGoLoggingNullBackendAndOffLevel-420000000108 ns/opBenchmarkGoLoggingNullBackendWithFancyFormatterAndOffLevel-4 20000000109 ns/opBenchmarkLog15-4 200000 7359 ns/opBenchmarkLog15WithDiscardHandler-4 2000000922 ns/opBenchmarkLog15WithDiscardHandlerAndOffLevel-4 2000000926 ns/opBenchmarkLog15WithNopLogger-4 20000000108 ns/opBenchmarkLog15WithNopLoggerDiscardHandlerA-4 20000000112 ns/opBenchmarkLog15WithNopLoggerAndDiscardHandlerAndOffLevel-4 20000000112 ns/opBenchmarkLog-4 1000000 1217 ns/opBenchmarkLogIoDiscardWriter-4 2000000724 ns/opBenchmarkLogIoDiscardWriterWithoutFlags-4 3000000543 ns/opBenchmarkLogCustomNullWriter-4 2000000731 ns/opBenchmarkLogCustomNullWriterWithoutFlags-4 3000000549 ns/opBenchmarkNopLogger-420000000113 ns/opBenchmarkNopLoggerWithoutFlags-4 20000000112 ns/opBenchmarkLogrus-4 300000 3832 ns/opBenchmarkLogrusWithDiscardWriter-4 500000 3032 ns/opBenchmarkLogrusWithNullFormatter-4 500000 3814 ns/opBenchmarkLogrusWithPanicLevel-4 500000 3872 ns/opBenchmarkLogrusWithDiscardWriterAndPanicLevel-4 500000 3085 ns/opBenchmarkLogrusWithDiscardWriterAndNullFormatterAndPanicLevel-4 500000 3064 ns/opok log-benchmarks 51.378sgo test -bench . 62.17s user 3.90s system 126% cpu 52.065 total
#1: YMMV ,在i7-4500U CPU @ 1.80GHz上测试
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)