Begin iterator takes too much time
While doing some micro benchmarks I noticed that the begin()
iterator of leaf grid view takes a lot of time compared to the grid loop. This only affects grid loops that have little work per entity. In my measurements, auto it = gv.template begin<0>();
takes between 10% and 27% of the loop time. I have seen this on grids with any global refinement. I don't know how this affects other kind of refinements.
The following code can be used to test this. I tried this on test-ug
volatile void const * do_not_optimize;
template<typename GridView>
void iteratorTest(GridView gv) {
std::cout << "***********************************" << std::endl;
std::cout << "gv.size(0) := " << gv.size(0) << std::endl;
Dune::Timer timer;
auto it = gv.template begin<0>();
auto begin_time = timer.elapsed();
timer.reset();
auto end_it = gv.template end<0>();
auto end_time = timer.elapsed();
timer.reset();
for (; it != end_it; it++) {
do_not_optimize = &*it;
}
auto loop_time = timer.elapsed();
auto total_time = begin_time + end_time + loop_time;
std::cout.precision(2);
std::cout << "auto it = gv.template begin<0>(); | " << std::scientific << begin_time << "s | " << std::fixed << 100*begin_time/total_time << " %" << std::endl;
std::cout << "auto end gv.template end<0>(); | " << std::scientific << end_time << "s | " << std::fixed << 100*end_time/total_time << " %" << std::endl;
std::cout << "for (; it != end_it; it++) {} | " << std::scientific << loop_time << "s | " << std::fixed << 100*loop_time/total_time << " %" << std::endl;
}