1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-25 05:16:09 -04: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:
Chinsyo 2020-11-14 13:00:25 +08:00 committed by GitHub
parent 42f86fc6b8
commit bdf715afa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -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 {

View File

@ -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))
}
}