0

I did some simple test: reading csv file with 100 000 rows (10 columns) filled up by random English words. Script opened file, and put every rows to an array variable.

ab -n 100 -l http://localhost:8000

I sent 100 request by Apache Benchmark command to measure how much PHP8.2 is faster than PHP7.4, but... result surprised me. PHP7 was faster with score 66,9 seconds. For PHP8 it tooks 71 seconds. I did tests few times, same result. Why?

Tasks for both PHP versions were ran on indentical environment: Docker, Ubuntu 20.02, default PHP configuration.

In previous tests about calculating prime numbers, PHP8 was much faster. PHP8 is promoted as the fastest version from all of them.

Source code below:

<?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();
}

Dockerfile (same for PHP8 - with replaced php version to 8.2 and 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 (same for PHP8 - with replaced php7 to php8)

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 (rows like below)

"sides","opportunity","thin","remove","mud","this","appearance","proud","bad","round"

piweu
  • 1
  • 1
  • make the CVS files available somewhere, or post generation instructions. could be many reasons though, maybe your 7.4 was built with -O2 and 8.2 was built with -O0 -g ? or maybe your 7.4 is over unix sockets and your 8.2 is over TCP? – hanshenrik Jan 01 '23 at 21:03
  • also share the exact source code you're reading the csv's in – hanshenrik Jan 01 '23 at 21:04
  • I added code to question. – piweu Jan 01 '23 at 21:19
  • A 5% time difference is not very significant (especially in a docker container). What is the std-dev? Did you tried to change the order? It can also be a regression (not all things are faster from one old version to a more recent one). A JIT is meant to speed up computationally intensive operation but does not help much for IOs (or even object allocations). It can actually often introduce some additional latency. – Jérôme Richard Jan 02 '23 at 14:38

1 Answers1

0

the "Geographic units, by industry and statistical area: 2000–2022 descending order – CSV" from https://www.stats.govt.nz/large-datasets/csv-files-for-download/ is 135MB and has 5.9 million rows starting with

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

downloading that and stripping out the first 100,000 lines:

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

and i have both php8.2.0-cli and php7.4.33-cli installed side-by-side (courtesy of https://deb.sury.org/ ) , and running this code

<?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);

running them both through 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

enter image description here

my conclusion is that PHP8.2 is about 2% faster than PHP7.4 in this particular task...

hanshenrik
  • 19,904
  • 4
  • 43
  • 89