2014-10-03 49 views
0

我發現這個代碼庫下面,有人這樣評價我們的方法,象這樣創建蘭特結構

// TODO avoid using rand.Float64 method. it uses a singleton lock and may cause 
    // performance issues. Instead, instantiate a rand struct and use that to call 
    // Float64() 

    func standardStrategy(l *ledger) bool { 
     return rand.Float64() <= probabilitySend(l.Accounting.Value()) 
    } 

func probabilitySend(ratio float64) float64 { 
    x := 1 + math.Exp(6-3*ratio) 
    y := 1/x 
    return 1 - y 
} 

一個TODO這是什麼意思?

回答

0

我認爲這意味着這個:rand包有一些東西叫做Rand結構,它具有隨機生成函數,可能不會鎖定全局鎖,因此可能是註釋的作者意味着使用此結構。即:

r := rand.New(rand.NewSource(1234)) 

fmt.Println(r.Float64()) 

在此代碼中使用的功能是全局的包,使用全球初始化Rand結構,內部globalRand調用,它有一個內部互斥。所以避免使用它可以節省這個鎖定。