0

This may be the opposite of most questions, but for testing purposes I want to create a predicate that uses some very slow operations that do not create data races. I need to test a parallel implementation compared to a sequential implementation and would like to do so without creating massively sized vectors. I currently use this predicate:

[](std::size_t& v){
    v = 42; //some random number...
}

but in order to see any real results my vectors need to be massive in terms of size, I would rather have a smaller size and greater predicate execution time... what sort of lamba function should I write that is very time consuming so I can test my implementations?

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

1 Answers1

0

Just an idea, force a number of repeated read-modify-writes:

std::size_t volatile& vv = v;
vv = 0;
for (int i = 0; i != 100; ++i) { vv +=1; }

Since the compiler may not optimize this, it will involve quite a few real reads of v. That said, you'll probably still hit cache.

If that's to be avoided too, you need std::atomic_store

MSalters
  • 173,980
  • 10
  • 155
  • 350