为什么PHP7在阅读CSV文件时比PHP8快?

jm81lzqq  于 2023-01-06  发布在  PHP
关注(0)|答案(1)|浏览(162)

我做了一些简单的测试:阅读一个100000行(10列)的csv文件,其中随机填充了英语单词。用脚本打开文件,并将每行放入一个数组变量。
第一个月
我通过Apache Benchmark命令发送了100个请求来测试PHP8.2比PHP7.4快多少,但是...结果让我很惊讶。PHP7比PHP7快66.9秒。PHP 8比PHP7快71秒。我做了几次测试,结果都一样。为什么?
两个PHP版本的任务都在相同的环境中运行:Docker,Ubuntu 20.02,默认PHP配置。
在以前的素数计算测试中,PHP 8的速度要快得多,PHP 8被提升为所有测试中最快的版本。
源代码如下:

<?php

if (!isset($_GET['phpinfo'])) {
    $csvData = [];

    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        $row = 0;

        while (($data = fgetcsv($handle)) !== FALSE) {
            $csvData[] = $data;
            $row++;
        }

        fclose($handle);

        if ($row === 100000) {
            http_response_code(200);
        } else {
            http_response_code(400);
        }
    }
} else {
    phpinfo();
}

停靠文件(与PHP 8相同-将php版本替换为8.2和workdir)

FROM ubuntu:20.04

ARG PHP_VERSION="7.4"

RUN apt update && \
    apt -y install --no-install-recommends && \
    apt -y install software-properties-common && \
    add-apt-repository ppa:ondrej/php && \
    apt update && \
    apt -y install --no-install-recommends && \
    apt -y install php${PHP_VERSION}

WORKDIR /var/www/html/php7

COPY . /var/www/html/php7

docker-compose.yml(与PHP 8相同-将php7替换为php 8)

version: "3.9"
services:
  php7:
    container_name: php7
    build: ./php7
    ports:
      - "8000:8000"
    volumes:
      - ./php7:/var/www/html/php7
    stdin_open: true
    tty: true
    restart: always
    command: php -S 0.0.0.0:8000 -t .

test.csv(行如下)
“侧面”、“机会”、“瘦”、“去掉”、“泥”、“这个”、“样子”、“骄傲”、“坏”、“圆”

uujelgoq

uujelgoq1#

“按行业和统计领域分列的地理单位:2000-2022降序排列-https://www.stats.govt.nz/large-datasets/csv-files-for-download/中的CSV”为135 MB,具有590万行,以

anzsic06,Area,year,geo_count,ec_count
A,A100100,2022,93,190
A,A100200,2022,138,190
A,A100300,2022,6,25

下载并去掉前10万行

cat Data7602DescendingYearOrder.csv | head -n100000 > 100000.csv

我同时安装了php8.2.0-cli和php7.4.33-cli(由https://deb.sury.org/提供),运行以下代码

<?php
declare (strict_types = 1);
$csvData = [];
$handle = fopen("100000.csv", "r");
if (!$handle) {
    throw new Exception("Could not open file");
}
$row = 0;
while (($data = fgetcsv($handle)) !== false) {
    $csvData[] = $data;
    $row++;
}

fclose($handle);

通过hyperfine benchmark运行它们:

hans@devad22:/temp2/csv$ php7.4 --version
PHP 7.4.33 (cli) (built: Nov  8 2022 11:33:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies
hans@devad22:/temp2/csv$ php8.2 --version
PHP 8.2.0 (cli) (built: Dec 10 2022 10:53:01) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.0, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.0, Copyright (c), by Zend Technologies
hans@devad22:/temp2/csv$ cat 100000.csv | head 
anzsic06,Area,year,geo_count,ec_count
A,A100100,2022,93,190
A,A100200,2022,138,190
A,A100300,2022,6,25
A,A100400,2022,57,50
A,A100500,2022,57,95
A,A100600,2022,12,30
A,A100700,2022,15,30
A,A100800,2022,30,85
A,A100900,2022,54,30
hans@devad22:/temp2/csv$ cat 100000.csv | wc -l
100000
hans@devad22:/temp2/csv$ cat csv.php 
<?php
declare (strict_types = 1);
$csvData = [];
$handle = fopen("100000.csv", "r");
if (!$handle) {
    throw new Exception("Could not open file");
}
$row = 0;
while (($data = fgetcsv($handle)) !== false) {
    $csvData[] = $data;
    $row++;
}

fclose($handle);
hans@devad22:/temp2/csv$ hyperfine --warmup 10 'php7.4 csv.php'
Benchmark 1: php7.4 csv.php
  Time (mean ± σ):     345.5 ms ±  16.3 ms    [User: 306.3 ms, System: 29.7 ms]
  Range (min … max):   319.2 ms … 378.7 ms    10 runs
 
hans@devad22:/temp2/csv$ hyperfine --warmup 10 'php8.2 csv.php'
Benchmark 1: php8.2 csv.php
  Time (mean ± σ):     337.7 ms ±  22.0 ms    [User: 299.7 ms, System: 29.5 ms]
  Range (min … max):   306.6 ms … 381.3 ms    10 runs

我的结论是,在这个特定的任务中,PHP 8. 2比PHP7. 4快大约2%...

相关问题