mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-10 03:17:11 -05:00
rand.Intn(65536) -> rand.Int63() >> 47 (#417)
* Optimize rand.Intn(65536) to rand.Int31() >> 15, with ~20% performance improvement. * Optimize rand.Intn(65536) to rand.rand.Int63() >> 47 * Remove rand.Seed call duplicate with original source code Co-authored-by: Chinsyo <chinsyo@sina.cn>
This commit is contained in:
parent
42f86fc6b8
commit
bdf715afa9
common/dice
@ -25,7 +25,7 @@ func RollDeterministic(n int, seed int64) int {
|
||||
|
||||
// RollUint16 returns a random uint16 value.
|
||||
func RollUint16() uint16 {
|
||||
return uint16(rand.Intn(65536))
|
||||
return uint16(rand.Int63() >> 47)
|
||||
}
|
||||
|
||||
func RollUint64() uint64 {
|
||||
|
@ -30,3 +30,21 @@ func BenchmarkIntn20(b *testing.B) {
|
||||
rand.Intn(20)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkInt31(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = uint16(rand.Int31() >> 15)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkInt63(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = uint16(rand.Int63() >> 47)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkIntn(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = uint16(rand.Intn(65536))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user