pgregory.net/rand@v1.0.3-0.20230808192358-a0b8ce02f4da/rand_bench_test.go (about) 1 // Copyright 2022 Gregory Petrosyan <gregory.petrosyan@gmail.com> 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, v. 2.0. If a copy of the MPL was not distributed with this 5 // file, You can obtain one at https://mozilla.org/MPL/2.0/. 6 7 //go:build !benchx && !benchstd && !benchfast 8 9 package rand_test 10 11 import ( 12 "math" 13 "pgregory.net/rand" 14 "testing" 15 ) 16 17 var ( 18 sinkRand *rand.Rand 19 ) 20 21 func BenchmarkExpFloat64(b *testing.B) { 22 b.RunParallel(func(pb *testing.PB) { 23 var s float64 24 for pb.Next() { 25 s = rand.ExpFloat64() 26 } 27 sinkFloat64 = s 28 }) 29 } 30 31 func BenchmarkFloat32(b *testing.B) { 32 b.RunParallel(func(pb *testing.PB) { 33 var s float32 34 for pb.Next() { 35 s = rand.Float32() 36 } 37 sinkFloat32 = s 38 }) 39 } 40 41 func BenchmarkFloat64(b *testing.B) { 42 b.RunParallel(func(pb *testing.PB) { 43 var s float64 44 for pb.Next() { 45 s = rand.Float64() 46 } 47 sinkFloat64 = s 48 }) 49 } 50 51 func BenchmarkInt(b *testing.B) { 52 b.RunParallel(func(pb *testing.PB) { 53 var s int 54 for pb.Next() { 55 s = rand.Int() 56 } 57 sinkInt = s 58 }) 59 } 60 61 func BenchmarkInt31(b *testing.B) { 62 b.RunParallel(func(pb *testing.PB) { 63 var s int32 64 for pb.Next() { 65 s = rand.Int31() 66 } 67 sinkInt32 = s 68 }) 69 } 70 71 func BenchmarkInt31n(b *testing.B) { 72 b.RunParallel(func(pb *testing.PB) { 73 var s int32 74 for pb.Next() { 75 s = rand.Int31n(small) 76 } 77 sinkInt32 = s 78 }) 79 } 80 81 func BenchmarkInt31n_Big(b *testing.B) { 82 b.RunParallel(func(pb *testing.PB) { 83 var s int32 84 for pb.Next() { 85 s = rand.Int31n(math.MaxInt32 - small) 86 } 87 sinkInt32 = s 88 }) 89 } 90 91 func BenchmarkInt63(b *testing.B) { 92 b.RunParallel(func(pb *testing.PB) { 93 var s int64 94 for pb.Next() { 95 s = rand.Int63() 96 } 97 sinkInt64 = s 98 }) 99 } 100 101 func BenchmarkInt63n(b *testing.B) { 102 b.RunParallel(func(pb *testing.PB) { 103 var s int64 104 for pb.Next() { 105 s = rand.Int63n(small) 106 } 107 sinkInt64 = s 108 }) 109 } 110 111 func BenchmarkInt63n_Big(b *testing.B) { 112 b.RunParallel(func(pb *testing.PB) { 113 var s int64 114 for pb.Next() { 115 s = rand.Int63n(math.MaxInt64 - small) 116 } 117 sinkInt64 = s 118 }) 119 } 120 121 func BenchmarkIntn(b *testing.B) { 122 b.RunParallel(func(pb *testing.PB) { 123 var s int 124 for pb.Next() { 125 s = rand.Intn(small) 126 } 127 sinkInt = s 128 }) 129 } 130 131 func BenchmarkIntn_Big(b *testing.B) { 132 b.RunParallel(func(pb *testing.PB) { 133 var s int 134 for pb.Next() { 135 s = rand.Intn(math.MaxInt - small) 136 } 137 sinkInt = s 138 }) 139 } 140 141 func BenchmarkNormFloat64(b *testing.B) { 142 b.RunParallel(func(pb *testing.PB) { 143 var s float64 144 for pb.Next() { 145 s = rand.NormFloat64() 146 } 147 sinkFloat64 = s 148 }) 149 } 150 151 func BenchmarkPerm(b *testing.B) { 152 b.ReportAllocs() 153 b.RunParallel(func(pb *testing.PB) { 154 for pb.Next() { 155 rand.Perm(tiny) 156 } 157 }) 158 } 159 160 func BenchmarkRead(b *testing.B) { 161 b.RunParallel(func(pb *testing.PB) { 162 p := make([]byte, 256) 163 b.SetBytes(int64(len(p))) 164 for pb.Next() { 165 _, _ = rand.Read(p[:]) 166 } 167 }) 168 } 169 170 func BenchmarkShuffle(b *testing.B) { 171 b.RunParallel(func(pb *testing.PB) { 172 a := make([]int, tiny) 173 for pb.Next() { 174 rand.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] }) 175 } 176 }) 177 } 178 179 func BenchmarkShuffleOverhead(b *testing.B) { 180 b.RunParallel(func(pb *testing.PB) { 181 a := make([]int, tiny) 182 for pb.Next() { 183 rand.Shuffle(len(a), func(i, j int) {}) 184 } 185 }) 186 } 187 188 func BenchmarkUint32(b *testing.B) { 189 b.RunParallel(func(pb *testing.PB) { 190 var s uint32 191 b.SetBytes(4) 192 for pb.Next() { 193 s = rand.Uint32() 194 } 195 sinkUint32 = s 196 }) 197 } 198 199 func BenchmarkUint32n(b *testing.B) { 200 b.RunParallel(func(pb *testing.PB) { 201 var s uint32 202 for pb.Next() { 203 s = rand.Uint32n(small) 204 } 205 sinkUint32 = s 206 }) 207 } 208 209 func BenchmarkUint32n_Big(b *testing.B) { 210 b.RunParallel(func(pb *testing.PB) { 211 var s uint32 212 for pb.Next() { 213 s = rand.Uint32n(math.MaxUint32 - small) 214 } 215 sinkUint32 = s 216 }) 217 } 218 219 func BenchmarkUint64(b *testing.B) { 220 b.RunParallel(func(pb *testing.PB) { 221 var s uint64 222 b.SetBytes(8) 223 for pb.Next() { 224 s = rand.Uint64() 225 } 226 sinkUint64 = s 227 }) 228 } 229 230 func BenchmarkUint64n(b *testing.B) { 231 b.RunParallel(func(pb *testing.PB) { 232 var s uint64 233 for pb.Next() { 234 s = rand.Uint64n(small) 235 } 236 sinkUint64 = s 237 }) 238 } 239 240 func BenchmarkUint64n_Big(b *testing.B) { 241 b.RunParallel(func(pb *testing.PB) { 242 var s uint64 243 for pb.Next() { 244 s = rand.Uint64n(math.MaxUint64 - small) 245 } 246 sinkUint64 = s 247 }) 248 } 249 250 func BenchmarkRand_New(b *testing.B) { 251 var s *rand.Rand 252 b.ReportAllocs() 253 for i := 0; i < b.N; i++ { 254 s = rand.New(uint64(i)) 255 } 256 sinkRand = s 257 } 258 259 func BenchmarkRand_New0(b *testing.B) { 260 var s *rand.Rand 261 b.ReportAllocs() 262 for i := 0; i < b.N; i++ { 263 s = rand.New() 264 } 265 sinkRand = s 266 } 267 268 func BenchmarkRand_New3(b *testing.B) { 269 var s *rand.Rand 270 b.ReportAllocs() 271 for i := 0; i < b.N; i++ { 272 s = rand.New(uint64(i), uint64(i), uint64(i)) 273 } 274 sinkRand = s 275 } 276 277 func BenchmarkRand_NewInt(b *testing.B) { 278 var s int 279 b.ReportAllocs() 280 for i := 0; i < b.N; i++ { 281 s = rand.New().Int() 282 } 283 sinkInt = s 284 } 285 286 func BenchmarkRand_ExpFloat64(b *testing.B) { 287 var s float64 288 r := rand.New(1) 289 for i := 0; i < b.N; i++ { 290 s = r.ExpFloat64() 291 } 292 sinkFloat64 = s 293 } 294 295 func BenchmarkRand_Float32(b *testing.B) { 296 var s float32 297 r := rand.New(1) 298 for i := 0; i < b.N; i++ { 299 s = r.Float32() 300 } 301 sinkFloat32 = s 302 } 303 304 func BenchmarkRand_Float64(b *testing.B) { 305 var s float64 306 r := rand.New(1) 307 for i := 0; i < b.N; i++ { 308 s = r.Float64() 309 } 310 sinkFloat64 = s 311 } 312 313 func BenchmarkRand_Int(b *testing.B) { 314 var s int 315 r := rand.New(1) 316 for i := 0; i < b.N; i++ { 317 s = r.Int() 318 } 319 sinkInt = s 320 } 321 322 func BenchmarkRand_Int31(b *testing.B) { 323 var s int32 324 r := rand.New(1) 325 for i := 0; i < b.N; i++ { 326 s = r.Int31() 327 } 328 sinkInt32 = s 329 } 330 331 func BenchmarkRand_Int31n(b *testing.B) { 332 var s int32 333 r := rand.New(1) 334 for i := 0; i < b.N; i++ { 335 s = r.Int31n(small) 336 } 337 sinkInt32 = s 338 } 339 340 func BenchmarkRand_Int31n_Big(b *testing.B) { 341 var s int32 342 r := rand.New(1) 343 for i := 0; i < b.N; i++ { 344 s = r.Int31n(math.MaxInt32 - small) 345 } 346 sinkInt32 = s 347 } 348 349 func BenchmarkRand_Int63(b *testing.B) { 350 var s int64 351 r := rand.New(1) 352 for i := 0; i < b.N; i++ { 353 s = r.Int63() 354 } 355 sinkInt64 = s 356 } 357 358 func BenchmarkRand_Int63n(b *testing.B) { 359 var s int64 360 r := rand.New(1) 361 for i := 0; i < b.N; i++ { 362 s = r.Int63n(small) 363 } 364 sinkInt64 = s 365 } 366 367 func BenchmarkRand_Int63n_Big(b *testing.B) { 368 var s int64 369 r := rand.New(1) 370 for i := 0; i < b.N; i++ { 371 s = r.Int63n(math.MaxInt64 - small) 372 } 373 sinkInt64 = s 374 } 375 376 func BenchmarkRand_Intn(b *testing.B) { 377 var s int 378 r := rand.New(1) 379 for i := 0; i < b.N; i++ { 380 s = r.Intn(small) 381 } 382 sinkInt = s 383 } 384 385 func BenchmarkRand_Intn_Big(b *testing.B) { 386 var s int 387 r := rand.New(1) 388 for i := 0; i < b.N; i++ { 389 s = r.Intn(math.MaxInt - small) 390 } 391 sinkInt = s 392 } 393 394 func BenchmarkRand_NormFloat64(b *testing.B) { 395 var s float64 396 r := rand.New(1) 397 for i := 0; i < b.N; i++ { 398 s = r.NormFloat64() 399 } 400 sinkFloat64 = s 401 } 402 403 func BenchmarkRand_Perm(b *testing.B) { 404 b.ReportAllocs() 405 r := rand.New(1) 406 for i := 0; i < b.N; i++ { 407 r.Perm(tiny) 408 } 409 } 410 411 func BenchmarkRand_Read(b *testing.B) { 412 r := rand.New(1) 413 p := make([]byte, 256) 414 b.SetBytes(int64(len(p))) 415 for i := 0; i < b.N; i++ { 416 _, _ = r.Read(p[:]) 417 } 418 } 419 420 func BenchmarkRand_Seed(b *testing.B) { 421 r := rand.New(1) 422 for i := 0; i < b.N; i++ { 423 r.Seed(uint64(i)) 424 } 425 } 426 427 func BenchmarkRand_Shuffle(b *testing.B) { 428 r := rand.New(1) 429 a := make([]int, tiny) 430 for i := 0; i < b.N; i++ { 431 r.Shuffle(len(a), func(i, j int) { a[i], a[j] = a[j], a[i] }) 432 } 433 } 434 435 func BenchmarkRand_ShuffleOverhead(b *testing.B) { 436 r := rand.New(1) 437 a := make([]int, tiny) 438 for i := 0; i < b.N; i++ { 439 r.Shuffle(len(a), func(i, j int) {}) 440 } 441 } 442 443 func BenchmarkRand_Uint32(b *testing.B) { 444 var s uint32 445 r := rand.New(1) 446 b.SetBytes(4) 447 for i := 0; i < b.N; i++ { 448 s = r.Uint32() 449 } 450 sinkUint32 = s 451 } 452 453 func BenchmarkRand_Uint32n(b *testing.B) { 454 var s uint32 455 r := rand.New(1) 456 for i := 0; i < b.N; i++ { 457 s = r.Uint32n(small) 458 } 459 sinkUint32 = s 460 } 461 462 func BenchmarkRand_Uint32n_Big(b *testing.B) { 463 var s uint32 464 r := rand.New(1) 465 for i := 0; i < b.N; i++ { 466 s = r.Uint32n(math.MaxUint32 - small) 467 } 468 sinkUint32 = s 469 } 470 471 func BenchmarkRand_Uint64(b *testing.B) { 472 var s uint64 473 r := rand.New(1) 474 b.SetBytes(8) 475 for i := 0; i < b.N; i++ { 476 s = r.Uint64() 477 } 478 sinkUint64 = s 479 } 480 481 func BenchmarkRand_Uint64n(b *testing.B) { 482 var s uint64 483 r := rand.New(1) 484 for i := 0; i < b.N; i++ { 485 s = r.Uint64n(small) 486 } 487 sinkUint64 = s 488 } 489 490 func BenchmarkRand_Uint64n_Big(b *testing.B) { 491 var s uint64 492 r := rand.New(1) 493 for i := 0; i < b.N; i++ { 494 s = r.Uint64n(math.MaxUint64 - small) 495 } 496 sinkUint64 = s 497 } 498 499 func BenchmarkRand_MarshalBinary(b *testing.B) { 500 b.ReportAllocs() 501 r := rand.New(1) 502 for i := 0; i < b.N; i++ { 503 _, _ = r.MarshalBinary() 504 } 505 } 506 507 func BenchmarkRand_UnmarshalBinary(b *testing.B) { 508 b.ReportAllocs() 509 r := rand.New(1) 510 buf, _ := r.MarshalBinary() 511 for i := 0; i < b.N; i++ { 512 _ = r.UnmarshalBinary(buf) 513 } 514 }