1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPAdvent Calendar 2024

Day 11

PHPの文字列検索でよく使うpreg_match/str_contains/str_starts_withの処理速度調査

Last updated at Posted at 2024-12-11

概要

パフォーマンスを意識するとどれを使えばいいのか調べてみた。

結論

通常ならstr_contains

先頭から検索にマッチするならstr_starts_with

preg_matchはおそらくパフォーマンスが多少落ちてもstr_containsでは対応し切れない検索で使用する

環境

  • MacBookPro M2 Max 12コア 96GB
  • PHP 8.3.13 (cli)

シンプル検索

<?php
// テスト対象の文字列
$haystack = "This is a test string to measure the performance of string functions.";
$needle = "test";

// テスト回数
$iterations = 100000;

// パフォーマンス計測用関数
function measurePerformance(callable $function, $iterations)
{
    $start = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        $function();
    }
    $end = microtime(true);
    return $end - $start;
}

// preg_match テスト
$pregMatchTime = measurePerformance(function () use ($haystack, $needle) {
    preg_match("/" . preg_quote($needle, "/") . "/", $haystack);
}, $iterations);

// str_contains テスト
$containsTime = measurePerformance(function () use ($haystack, $needle) {
    str_contains($haystack, $needle);
}, $iterations);

// str_starts_with テスト
$startsWithTime = measurePerformance(function () use ($haystack) {
    str_starts_with($haystack, "This");
}, $iterations);

// 結果を表示
echo "Performance Results:\n";
echo "preg_match:     {$pregMatchTime} seconds\n";
echo "str_contains:  {$containsTime} seconds\n";
echo "str_starts_with: {$startsWithTime} seconds\n";

結果

Performance Results:
preg_match: 0.013596057891846 seconds
str_contains: 0.0050351619720459 seconds
str_starts_with: 0.0040431022644043 seconds

リスト検索

<?php
// ランダムな文字列リストを生成する関数
function generateRandomStrings($count, $length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $strings = [];
    for ($i = 0; $i < $count; $i++) {
        $strings[] = substr(str_shuffle(str_repeat($characters, $length)), 0, $length);
    }
    return $strings;
}

// テストデータの準備
$list = generateRandomStrings(10000, 20);
$needle = substr($list[array_rand($list)], 0, 5); // ランダムなリストの要素の一部を検索対象に設定

// パフォーマンス計測用関数
function measurePerformance(callable $function, $list)
{
    $start = microtime(true);
    foreach ($list as $item) {
        $function($item);
    }
    $end = microtime(true);
    return $end - $start;
}

// preg_match テスト
$pregMatchTime = measurePerformance(function ($item) use ($needle) {
    preg_match("/" . preg_quote($needle, "/") . "/", $item);
}, $list);

// str_contains テスト
$containsTime = measurePerformance(function ($item) use ($needle) {
    str_contains($item, $needle);
}, $list);

// str_starts_with テスト
$startsWithTime = measurePerformance(function ($item) {
    str_starts_with($item, "abc"); // "abc"は検索対象の固定値
}, $list);

// 結果を表示
echo "Performance Results:\n";
echo "preg_match:     {$pregMatchTime} seconds\n";
echo "str_contains:  {$containsTime} seconds\n";
echo "str_starts_with: {$startsWithTime} seconds\n";

結果

Performance Results:
preg_match: 0.0013799667358398 seconds
str_contains: 0.00051212310791016 seconds
str_starts_with: 0.0003809928894043 seconds

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?