php做控制下载速度代码 怎么又生带宽,又省cpu 和内存?

 <?php
/* Source: http://www.apphp.com/index.php?snippet=php-download-file-with-speed-limit */
    /* set here a limit of downloading rate (e.g. 10.20 Kb/s) */
   // $download_rate = 10.20;
   $download_rate = 200;
   set_time_limit(0);

    $download_file = 'myfile.rar';  
    $target_file = 'PHP敏捷开发CodeIgniter框架.rar';

    if(file_exists($download_file)){
        /* headers */
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
        header('Cache-control: private');
        header('Content-Type: application/octet-stream'); //HTTP Content-type ( 二进制流,不知道下载文件类型)
        header('Content-Length: '.filesize($download_file));
        header('Content-Disposition: filename='.$target_file);

        /* flush content */
        flush();

        /* open file */
        $fh = @fopen($download_file, 'r');
        while(!feof($fh)){
            /* send only current part of the file to browser */
            print fread($fh, round($download_rate * 1024));
            /* flush the content to the browser */
            flush();
            /* sleep for 1 sec */
            sleep(1);
        }
        /* close file */
        @fclose($fh);
    }else{
        die('Fatal error: the '.$download_file.' file does not exist!');
    }
    ?>

怎么又生带宽,又省cpu 和内存?
1.是不sleep 控制下载速度好呢,还是sleep一下省下带宽呢?
2,每次读取2进制多少个字节合适呢,还是越大越好,还是小点分多次读取好?

本人小白,先谢过各位大侠了。。

slepp会让出CPU时间片,从而可以达到降低CPU,下载带宽都会降低。
一般下载缓冲区控制在一个MTU的附近,然后再微调,达到一个最优结果。

没必要省cpu,因为带宽最先不够,可以用gzip压缩下。

sleep 1秒 时间太长了吧
你可以用 usleep(微秒) 来控制睡眠时间,这样一来$download_rate 就可以设置小一些了
比如:

 $download_rate = 0.02;
....

while(!feof($fh)){
            print fread($fh, round($download_rate * 1024));
            flush();
            usleep(100);
}