douzhuan1432 2017-06-25 00:25
浏览 32

基准不良结果

so I have the Quicksort algorithm implemented with concurrency (the one without as well). Now I wanted to compare the times. I wrote this:

func benchmarkConcurrentQuickSort(size int, b *testing.B) {
    A := RandomArray(size)
    var wg sync.WaitGroup
    b.ResetTimer()
    ConcurrentQuicksort(A, 0, len(A)-1, &wg)
    wg.Wait()
}

func BenchmarkConcurrentQuickSort500(b *testing.B) {
    benchmarkConcurrentQuickSort(500, b)
}
func BenchmarkConcurrentQuickSort1000(b *testing.B) {
    benchmarkConcurrentQuickSort(1000, b)
}
func BenchmarkConcurrentQuickSort5000(b *testing.B) {
    benchmarkConcurrentQuickSort(5000, b)
}
func BenchmarkConcurrentQuickSort10000(b *testing.B) {
    benchmarkConcurrentQuickSort(10000, b)
}
func BenchmarkConcurrentQuickSort20000(b *testing.B) {
    benchmarkConcurrentQuickSort(20000, b)
}
func BenchmarkConcurrentQuickSort1000000(b *testing.B) {
    benchmarkConcurrentQuickSort(1000000, b)
}

The results are like this:

C:\projects\go\src\github.com\frynio\mysort>go test -bench=.
BenchmarkConcurrentQuickSort500-4               2000000000               0.00 ns/op
BenchmarkConcurrentQuickSort1000-4              2000000000               0.00 ns/op
BenchmarkConcurrentQuickSort5000-4              2000000000               0.00 ns/op
BenchmarkConcurrentQuickSort10000-4             2000000000               0.00 ns/op
BenchmarkConcurrentQuickSort20000-4             2000000000               0.00 ns/op
BenchmarkConcurrentQuickSort1000000-4                 30          49635266 ns/op
PASS
ok      github.com/frynio/mysort        8.342s

I can believe the last one, but I definitely think that sorting 500-element array takes longer than 1ns. What am i doing wrong? I am pretty sure that RandomArray returns array of wanted size, as we can see in the last benchmark. Why does it print out the 0.00 ns?

func RandomArray(n int) []int {
    a := []int{}
    for i := 0; i < n; i++ {
        a = append(a, rand.Intn(500))
    }
    return a
}

// ConcurrentPartition - ConcurrentQuicksort function for partitioning the array (randomized choice of a pivot)
func ConcurrentPartition(A []int, p int, r int) int {
    index := rand.Intn(r-p) + p
    pivot := A[index]
    A[index] = A[r]
    A[r] = pivot
    x := A[r]
    j := p - 1
    i := p
    for i < r {
        if A[i] <= x {
            j++
            tmp := A[j]
            A[j] = A[i]
            A[i] = tmp
        }
        i++
    }
    temp := A[j+1]
    A[j+1] = A[r]
    A[r] = temp
    return j + 1
}

// ConcurrentQuicksort - a concurrent version of a quicksort algorithm
func ConcurrentQuicksort(A []int, p int, r int, wg *sync.WaitGroup) {
    if p < r {
        q := ConcurrentPartition(A, p, r)
        wg.Add(2)
        go func() {
            ConcurrentQuicksort(A, p, q-1, wg)
            wg.Done()
        }()
        go func() {
            ConcurrentQuicksort(A, q+1, r, wg)
            wg.Done()
        }()
    }
}
  • 写回答

1条回答 默认 最新

  • dongyulian5801 2017-06-25 02:03
    关注

    Package testing

    A sample benchmark function looks like this:

    func BenchmarkHello(b *testing.B) {
        for i := 0; i < b.N; i++ {
            fmt.Sprintf("hello")
        }
    }
    

    The benchmark function must run the target code b.N times. During benchmark execution, b.N is adjusted until the benchmark function lasts long enough to be timed reliably.

    I don't see a benchmark loop in your code. Try

    func benchmarkConcurrentQuickSort(size int, b *testing.B) {
        A := RandomArray(size)
        var wg sync.WaitGroup
        b.ResetTimer()
        for i := 0; i < b.N; i++ {
            ConcurrentQuicksort(A, 0, len(A)-1, &wg)
            wg.Wait()
        }
    }
    

    Output:

    BenchmarkConcurrentQuickSort500-4              10000        122291 ns/op
    BenchmarkConcurrentQuickSort1000-4              5000        221154 ns/op
    BenchmarkConcurrentQuickSort5000-4              1000       1225230 ns/op
    BenchmarkConcurrentQuickSort10000-4              500       2568024 ns/op
    BenchmarkConcurrentQuickSort20000-4              300       5808130 ns/op
    BenchmarkConcurrentQuickSort1000000-4              1    1371751710 ns/op
    
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。