For-loop efficiency: merging loops

后端 未结 7 535
野趣味
野趣味 2021-01-04 19:46

I have always had the idea that reducing the number of iterations is the way to making programs more efficient. Since I never really confirmed that, I set out to te

7条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 20:11

    This code creates the variables:

        list1 = new int[n]; list2 = new int[n];
        list3 = new int[n]; list4 = new int[n];
        list5 = new int[n]; list6 = new int[n];
        list7 = new int[n]; list8 = new int[n];
        list9 = new int[n]; list10 = new int[n];
    

    but it almost certainly does not create the actual physical page mappings until the memory is actually modified. See Does malloc lazily create the backing pages for an allocation on Linux (and other platforms)? for an example.

    So your func1() has to wait for the creation of the actual physical pages of RAM, whereas your func2() doesn't. Change the order, and the mapping time will be attributed to func2() performance.

    The easiest solution given your code as posted is to run either func1() or func2() before doing your timed runs.

    If you don't ensure that the actual physical memory has been mapped before you do any benchmarking, that mapping will be part of the time you measure when you first modify the memory.

提交回复
热议问题