github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/bench_iteration_test.go (about)

     1  package goquery
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func BenchmarkEach(b *testing.B) {
     8  	var tmp, n int
     9  
    10  	b.StopTimer()
    11  	sel := DocW().Find("td")
    12  	f := func(i int, s *Selection) {
    13  		tmp++
    14  	}
    15  	b.StartTimer()
    16  	for i := 0; i < b.N; i++ {
    17  		sel.Each(f)
    18  		if n == 0 {
    19  			n = tmp
    20  		}
    21  	}
    22  	b.Logf("Each=%d", n)
    23  }
    24  
    25  func BenchmarkMap(b *testing.B) {
    26  	var tmp, n int
    27  
    28  	b.StopTimer()
    29  	sel := DocW().Find("td")
    30  	f := func(i int, s *Selection) string {
    31  		tmp++
    32  		return string(tmp)
    33  	}
    34  	b.StartTimer()
    35  	for i := 0; i < b.N; i++ {
    36  		sel.Map(f)
    37  		if n == 0 {
    38  			n = tmp
    39  		}
    40  	}
    41  	b.Logf("Map=%d", n)
    42  }
    43  
    44  func BenchmarkEachWithBreak(b *testing.B) {
    45  	var tmp, n int
    46  
    47  	b.StopTimer()
    48  	sel := DocW().Find("td")
    49  	f := func(i int, s *Selection) bool {
    50  		tmp++
    51  		return tmp < 10
    52  	}
    53  	b.StartTimer()
    54  	for i := 0; i < b.N; i++ {
    55  		tmp = 0
    56  		sel.EachWithBreak(f)
    57  		if n == 0 {
    58  			n = tmp
    59  		}
    60  	}
    61  	b.Logf("Each=%d", n)
    62  }