缺页引发的性能下降测试

缺页是什么?

当CPU请求一个不在RAM中的内存页时, 会发生缺页。

发生缺页时会进行页面置换,这个过程比较慢。

在比较大的循环中, 每次按列操作, 如果一行的数据太多,

就有可能引发缺页, 导致性能下降。

测试代码

代码来自代码大全(code complete):

function test_page($max_columns, $max_rows) {
    $table = array();
    for ($column = 0; $column < $max_columns; $column++) {
        for ($row = 0; $row < $max_rows; $row++) {
            $table[$row][$column] = 0;
        }
    }
}

function test_page_row($max_columns, $max_rows) {
    $table = array();
    for ($row = 0; $row < $max_rows; $row++) {
        for ($column = 0; $column < $max_columns; $column++) {
            $table[$row][$column] = 0;
        }
    }
}

$max_columns = 10000;
$max_rows = 200;

//test_page($max_columns, $max_rows);
test_page_row($max_columns, $max_rows);

测试结果

test_page()测试结果:

real    0m4.343s
user    0m3.817s
sys     0m0.525s

test_page_row()测试结果:

real    0m2.384s
user    0m1.991s
sys     0m0.394s

前者大概是后者的两倍。

Leave a Reply

Your email address will not be published. Required fields are marked *


To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax