golang 浮点数 取精度的效率对比

本文对比了Golang中strconv.FormatFloat与fmt.Sprintf在处理浮点数精度时的效率,发现FormatFloat更为高效。同时,文章指出Golang的浮点数运算不精确及四舍五入方式独特,并提供了一种自定义的四舍五入解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需求

    浮点数取2位精度输出

实现

  代码

package main

import (
    "time"
    "log"
    "strconv"
    "fmt"
)

func main() {
    threadCount := 100000
    fa := 3.233667


    time1 := TestFn(threadCount,fa,func(fa float64) string{
        return strconv.FormatFloat(fa,'f',2,64)
    })
    log.Printf("FormatFloat 耗时:%.4f ms",time1)

    time2 := TestFn(threadCount,fa,func(fa float64) string{
        return fmt.Sprintf("%.2f",fa)
    })
    log.Printf("Sprintf 耗时:%.4f ms",time2)

}

func TestFn(count int,fa float64,fn func(fa float64) string) float64{
    t1 := time.Now()

    for i := 0; i < count; i++ {
        fn(fa)
    }

    t2 := time.Now()
    return t2.Sub(t1).Seconds()*1000
}

 

  效率对比

    测试 100次 (即threadCount赋值100,下面同理)

    

2017/06/15 18:50:17 FormatFloat 耗时:0.0452 ms
2017/06/15 18:50:17 Sprintf 耗时:0.0512 ms

  

     测试 1000次

2017/06/15 18:50:43 FormatFloat 耗时:0.3861 ms
2017/06/15 18:50:43 Sprintf 耗时:0.4903 ms

 

     测试 10000次

2017/06/15 18:50:58 FormatFloat 耗时:3.9688 ms
2017/06/15 18:50:58 Sprintf 耗时:5.2045 ms

 

     测试  100000次    

2017/06/15 18:51:20 FormatFloat 耗时:41.9253 ms
2017/06/15 18:51:20 Sprintf 耗时:51.8639 ms

       测试  10000000次

2017/06/15 18:51:49 FormatFloat 耗时:3917.7585 ms
2017/06/15 18:51:54 Sprintf 耗时:5131.5497 ms

  结论

     strconv下的FormatFloat明显快一些。fmt.Sprintf用到反射,效率不高,建议少用。    

 

注意

  golang下的浮点数存在2个问题:

  1,运算时,计算结果不准

     2,四舍五入时,用的是银行舍入法,和其他语言四舍五入的值对不上

  解决

//四舍五入 取精度
func ToFixed(f float64,places int) float64{
    shift := math.Pow(10, float64(places))
    fv := 0.0000000001 + f        //对浮点数产生.xxx999999999 计算不准进行处理
    return math.Floor(fv * shift + .5) / shift
}

 

  

 

转载于:https://www.cnblogs.com/mominger/p/7019496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值