50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
		
		
			
		
	
	
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 
								 | 
							
								package utility
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import (
							 | 
						||
| 
								 | 
							
									"fmt"
							 | 
						||
| 
								 | 
							
									"math/rand"
							 | 
						||
| 
								 | 
							
									"strconv"
							 | 
						||
| 
								 | 
							
									"testing"
							 | 
						||
| 
								 | 
							
								)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func Test_(t *testing.T) {
							 | 
						||
| 
								 | 
							
									// t.Log(decimal.NewFromFloat(0))
							 | 
						||
| 
								 | 
							
									t.Log(strconv.FormatFloat(1.04, 'f', 1, 64))
							 | 
						||
| 
								 | 
							
									t.Log(strconv.FormatFloat(1.05, 'f', 1, 64))
							 | 
						||
| 
								 | 
							
									t.Log(strconv.FormatFloat(1.06, 'f', 1, 64))
							 | 
						||
| 
								 | 
							
									t.Log(strconv.FormatFloat(1.006, 'f', 2, 64))
							 | 
						||
| 
								 | 
							
									// t.Log(strconv.FormatFloat(`1.006`, 64))
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// i := int64(32)
							 | 
						||
| 
								 | 
							
								// s := strconv.FormatInt(i, 16)
							 | 
						||
| 
								 | 
							
								// println(s)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// 对比下 Float64ToString 和 FloatCutStr 的效率
							 | 
						||
| 
								 | 
							
								// go test -bench=_QE_ -benchmem
							 | 
						||
| 
								 | 
							
								// -benchtime 默认为1秒  -benchmem 获得内存分配的统计数据
							 | 
						||
| 
								 | 
							
								func Benchmark_QE_1(b *testing.B) {
							 | 
						||
| 
								 | 
							
									for i := 0; i < b.N; i++ {
							 | 
						||
| 
								 | 
							
										Float64ToString(getRandData(), 2)
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func Benchmark_QE_2(b *testing.B) {
							 | 
						||
| 
								 | 
							
									for i := 0; i < b.N; i++ {
							 | 
						||
| 
								 | 
							
										FloatCutStr(getRandData(), 2) // 已废弃
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func getRandData() float64 {
							 | 
						||
| 
								 | 
							
									return float64(rand.Intn(1000000)) / 1000
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								func TestTrimHtml(t *testing.T) {
							 | 
						||
| 
								 | 
							
									a := `sff` // `<script>alert('ab')</script>`
							 | 
						||
| 
								 | 
							
									x := TrimHtml(a)
							 | 
						||
| 
								 | 
							
									fmt.Print(x)
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								// Benchmark_QE_1-6         4902826               243.3 ns/op            31 B/op          2 allocs/op
							 | 
						||
| 
								 | 
							
								// Benchmark_QE_2-6         1275004               940.6 ns/op           137 B/op         11 allocs/op
							 | 
						||
| 
								 | 
							
								// 故此将优先使用 Float64ToString  并将已使用的 FloatCutStr 进行替换
							 |