Pass Randomly Generated Test Data Between Two Bechmark

824 views
Skip to first unread message

Bob Fang

unread,
Sep 10, 2017, 3:48:48 PM9/10/17
to benchmark-discuss
Hi, I am just wondering if it is possible to achieve the following set up.

I usually have two functions that do the same job and I want to compare their performance, let's say for example I have two functions bubble_sort and merge_sort.

To do this I have to automatically generate an array of numbers to sort, So I have another function called rand_array(n) where n is the size of the array.

Then my test set up looks like:

static void BM_bubbleSort(benchmark::State &state)
{
    auto a = rand_array(state.range(0));

    while(state.keepRunning())
    {
       benchmark::DoNotOptimize(bubble_sort(a));l
    }
   state.SetCompexity(state.range(0));
}


static void BM_mergeSort(benchmark::State &state)
{
    auto a = rand_array(state.range(0));

    while(state.keepRunning())
    {
       benchmark::DoNotOptimize(merge_sort(a));l
    }
   state.SetCompexity(state.range(0));
}


Then I register these two benchmarks to run with a range of parameters, to test their big O.

But here is the thing, I want to test them against the same, but randomly generated data. So I am just wondering if I can only generate the array once and then use them in both these cases rather generate two sets of test cases, one for each benchmark.

Is that possible?

Thanks a lot!

Best,
Bob


Dominic Hamon

unread,
Sep 11, 2017, 9:50:59 AM9/11/17
to Bob Fang, benchmark-discuss
https://github.com/google/benchmark#fixtures might help you here, but i don't think you need it. You could just do something like:

```
static std::array<double>* rand_array(int x) {
  static std::array<double>* arr = nullptr;
  if (arr == nullptr) {
    arr = new std::array<double>();
    // fill arr
  }
  return arr;
}
```

Then the first time it's called it'll fill randomly and later calls will just return the same vector.




--
You received this message because you are subscribed to the Google Groups "benchmark-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to benchmark-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Peter Steinbach

unread,
Sep 11, 2017, 9:53:13 AM9/11/17
to benchmar...@googlegroups.com


On 09/11/2017 03:50 PM, 'Dominic Hamon' via benchmark-discuss wrote:
> https://github.com/google/benchmark#fixtures might help you here, but i
> don't think you need it. You could just do something like:
>
> ```
> static std::array<double>* rand_array(int x) {
> static std::array<double>* arr = nullptr;
> if (arr == nullptr) {
> arr = new std::array<double>();
> // fill arr
> }
> return arr;
> }
> ```
>

Just wondering: AFAIK, this implementation is not thread-safe ... would
that be an issue with libbenchmark ?

Dominic Hamon

unread,
Sep 11, 2017, 9:55:35 AM9/11/17
to Peter Steinbach, benchmark-discuss
It would be fine for the defaults, but if you're running threaded benchmarks you'd want to add some sort of locking in there. 
Reply all
Reply to author
Forward
0 new messages