Skip to content

ctype_alnum 5 times slower in PHP 8.1 or greater #11997

Description

@joanhey

Description

The following code:

<?php

$foo = '112312312312323';

$start_time = microtime(true);

for ($i = 0; $i < 10000; $i++) {
    $foo = $foo.'1';
    preg_match('/^[a-zA-Z0-9]+$/', $foo);
}

$end_time = microtime(true);

echo 'preg_match Result: ' . ($end_time - $start_time).PHP_EOL;

$foo = '112312312312323';

$start_time = microtime(true);

for ($i = 0; $i < 10000; $i++) {
    $foo = $foo.'1';
    ctype_alnum($foo);
}

$end_time = microtime(true);

echo 'ctype Result: ' . ($end_time - $start_time).PHP_EOL;

Resulted in this output:
A lot slower in 8.1 and 8.2

But I expected this output instead:
The same that in 8.0 or smaller.

Here start the performance degradation:
image

https://3v4l.org/fjSaZ
https://onlinephp.io/code/e3b9ddc370fdb0f00ff40447eadc24def0dd1abe

PD: I didn't test other ctype_* functions

PHP Version

8.1

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions