github.com/remyoudompheng/bigfft@v0.0.0-20230129092748-24d4a6f8daec/scan_test.go (about)

     1  package bigfft
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestScan(t *testing.T) {
    10  	for size := 10; size <= 1e5; size += 191 {
    11  		s := rndStr(size)
    12  		x, ok := new(big.Int).SetString(s, 10)
    13  		if !ok {
    14  			t.Fatal("cannot parse", s)
    15  		}
    16  		t0 := time.Now()
    17  		y := FromDecimalString(s)
    18  		if x.Cmp(y) != 0 {
    19  			t.Errorf("failed at size %d", size)
    20  		} else {
    21  			t.Logf("OK for size %d in %s", size, time.Since(t0))
    22  		}
    23  	}
    24  }
    25  
    26  func BenchmarkScanFast1k(b *testing.B)   { benchmarkScanFast(1e3, b) }
    27  func BenchmarkScanFast10k(b *testing.B)  { benchmarkScanFast(10e3, b) }
    28  func BenchmarkScanFast100k(b *testing.B) { benchmarkScanFast(100e3, b) }
    29  func BenchmarkScanFast1M(b *testing.B)   { benchmarkScanFast(1e6, b) }
    30  func BenchmarkScanFast2M(b *testing.B)   { benchmarkScanFast(2e6, b) }
    31  func BenchmarkScanFast5M(b *testing.B)   { benchmarkScanFast(5e6, b) }
    32  func BenchmarkScanFast10M(b *testing.B)  { benchmarkScanFast(10e6, b) }
    33  
    34  //func BenchmarkScanFast100M(b *testing.B) { benchmarkScanFast(100e6, b) }
    35  
    36  func benchmarkScanFast(n int, b *testing.B) {
    37  	s := rndStr(n)
    38  	var x *big.Int
    39  	for i := 0; i < b.N; i++ {
    40  		x = FromDecimalString(s)
    41  	}
    42  	_ = x
    43  }
    44  
    45  func BenchmarkScanBig1k(b *testing.B)   { benchmarkScanBig(1e3, b) }
    46  func BenchmarkScanBig10k(b *testing.B)  { benchmarkScanBig(10e3, b) }
    47  func BenchmarkScanBig100k(b *testing.B) { benchmarkScanBig(100e3, b) }
    48  func BenchmarkScanBig1M(b *testing.B)   { benchmarkScanBig(1e6, b) }
    49  func BenchmarkScanBig2M(b *testing.B)   { benchmarkScanBig(2e6, b) }
    50  func BenchmarkScanBig5M(b *testing.B)   { benchmarkScanBig(5e6, b) }
    51  func BenchmarkScanBig10M(b *testing.B)  { benchmarkScanBig(10e6, b) }
    52  
    53  func benchmarkScanBig(n int, b *testing.B) {
    54  	s := rndStr(n)
    55  	var x big.Int
    56  	for i := 0; i < b.N; i++ {
    57  		x.SetString(s, 10)
    58  	}
    59  }
    60  
    61  func rndStr(n int) string {
    62  	x := make([]byte, n)
    63  	for i := 0; i < n; i++ {
    64  		x[i] = '0' + byte(rnd.Intn(10))
    65  	}
    66  	return string(x)
    67  }