zoukankan      html  css  js  c++  java
  • 分享两个在开发中需注意的小点

    不要使用 + 和 fmt.Sprintf 操作字符串

    不要使用 +fmt.Sprintf 操作字符串,虽然很方便,但是真的很慢!

    我们要使用 bytes.NewBufferString 进行处理。

    基准测试如下:

    +

    func BenchmarkStringOperation1(b *testing.B)  {
    	b.ResetTimer()
    	str := ""
    	for i := 0; i < b.N; i++ {
    		str += "golang"
    	}
    }
    
    // 输出
    goos: darwin
    goarch: amd64
    pkg: demo/stringoperation
    cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    BenchmarkStringOperation1
    BenchmarkStringOperation1-12    	  353318	    114135 ns/op
    PASS
    
    Process finished with the exit code 0
    

    fmt.Sprintf

    func BenchmarkStringOperation2(b *testing.B)  {
    	b.ResetTimer()
    	str := ""
    	for i := 0; i < b.N; i++ {
    		str = fmt.Sprintf("%s%s", str, "golang")
    	}
    }
    
    // 输出
    goos: darwin
    goarch: amd64
    pkg: demo/stringoperation
    cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    BenchmarkStringOperation2
    BenchmarkStringOperation2-12    	  280140	    214098 ns/op
    PASS
    
    Process finished with the exit code 0
    

    bytes.NewBufferString

    func BenchmarkStringOperation3(b *testing.B)  {
    	b.ResetTimer()
    	strBuf := bytes.NewBufferString("")
    	for i := 0; i < b.N; i++ {
    		strBuf.WriteString("golang")
    	}
    }
    
    // 输出
    goos: darwin
    goarch: amd64
    pkg: demo/stringoperation
    cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    BenchmarkStringOperation3
    BenchmarkStringOperation3-12    	161292136	         8.582 ns/op
    PASS
    
    Process finished with the exit code 0
    

    对于固定字段的键值对,不要使用 map[string]interface{}

    对于固定字段的键值对,不要使用 map[string]interface{}!

    我们要使用临时 Struct

    基准测试如下:

    map[string]interface{}

    func BenchmarkStructOperation1(b *testing.B) {
    	b.ResetTimer()
    	for i := 0; i < b.N; i++ {
    		var demo = map[string]interface{}{}
    		demo["Name"] = "Tom"
    		demo["Age"] = 30
    	}
    }
    
    // 输出
    goos: darwin
    goarch: amd64
    pkg: demo/structoperation
    cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    BenchmarkStructOperation1
    BenchmarkStructOperation1-12    	43300134	        27.97 ns/op
    PASS
    
    Process finished with the exit code 0
    

    临时 Struct

    func BenchmarkStructOperation2(b *testing.B) {
    	b.ResetTimer()
    	for i := 0; i < b.N; i++ {
    		var demo struct {
    			Name string
    			Age  int
    		}
    		demo.Name = "Tom"
    		demo.Age = 30
    	}
    }
    
    // 输出
    oos: darwin
    goarch: amd64
    pkg: demo/structoperation
    cpu: Intel(R) Core(TM) i7-8700B CPU @ 3.20GHz
    BenchmarkStructOperation2
    BenchmarkStructOperation2-12    	1000000000	         0.2388 ns/op
    PASS
    
    Process finished with the exit code 0
    

    小结

    你有类似这样的注意点吗,欢迎留言~

    下面推荐阅读的这几篇文章也是关于开发中需要知道的小技术点,更多技术细节和代码讨论,可以加入到我的星球:https://t.zsxq.com/iIUVVnA

    作者:新亮笔记(关注公众号,可申请添加微信好友)
    出处:https://www.cnblogs.com/xinliangcoder
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    Python基础06 循环
    Python基础04 运算
    Python基础02 基本数据类型
    Python基础03 序列
    C++ ------ 引用
    C++ ------ 互斥锁、原子操作的性能测试
    Qt ------ 断开某对信号与槽的connect
    5种网络通信设计模型(也称IO模型)
    Qt ------ 主事件循环与 QEventLoop
    Qt ------ 再论事件循环
  • 原文地址:https://www.cnblogs.com/xinliangcoder/p/14966169.html
Copyright © 2011-2022 走看看