{"id":16190605,"url":"https://github.com/devmount/faster-php","last_synced_at":"2025-03-19T03:30:57.943Z","repository":{"id":49369812,"uuid":"236970947","full_name":"devmount/faster-php","owner":"devmount","description":"Testing different approaches to improve PHP script performance","archived":false,"fork":false,"pushed_at":"2023-08-17T17:34:54.000Z","size":39,"stargazers_count":55,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-17T03:21:28.640Z","etag":null,"topics":["benchmark","php","php-benchmark","php-performance","php-testing"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devmount.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["devmount"],"custom":["https://paypal.me/devmount"]}},"created_at":"2020-01-29T11:53:10.000Z","updated_at":"2025-01-23T12:10:40.000Z","dependencies_parsed_at":"2024-06-11T20:34:59.741Z","dependency_job_id":"8f32259d-26d9-488c-a0db-01af23d733d2","html_url":"https://github.com/devmount/faster-php","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmount%2Ffaster-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmount%2Ffaster-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmount%2Ffaster-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devmount%2Ffaster-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devmount","download_url":"https://codeload.github.com/devmount/faster-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244350939,"owners_count":20439290,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["benchmark","php","php-benchmark","php-performance","php-testing"],"created_at":"2024-10-10T07:43:42.835Z","updated_at":"2025-03-19T03:30:57.666Z","avatar_url":"https://github.com/devmount.png","language":"PHP","readme":"# faster-php ⚡\n\nDifferent approaches to improve PHP script performance. For discussion or additional methods [create an issue](https://github.com/devmount/faster-php/issues/new) or comment on the corresponding [DEV article](https://dev.to/devmount/4-php-tricks-to-boost-script-performance-ol1).\n\n## Get started\n\nTo test the methods yourself, first make sure PHP 7.4 is installed:\n\n```bash\n$ php -v\nPHP 7.4.4 (cli) (built: Mar 20 2020 13:47:17) ...\n```\n\nThen clone this repo and install dependencies using [Composer](https://getcomposer.org):\n\n```bash\ngit clone https://github.com/devmount/faster-php\ncd faster-php\ncomposer install\n```\n\nNow you are ready to call the one of the test scripts presented below.\n\n## List of methods\n\n### 1. Removing duplicates in array\n\nMethod:\n\n```php\n// using\narray_keys(array_flip($array));\n// instead of\narray_unique($array);\n```\n\nTest:\n\n```bash\nphp test_array_unique.php\n```\n\nResult:\n\n| method | execution time |\n|--------|---------------:|\n| `array_unique` | 787.31 ms |\n| `array_keys` `array_flip` | 434.03 ms |\n\nThe alternative approach is **1.8x** (44.87%) faster in this measurement. On average, it was ~1.5x (30%) faster. Tested on an array with 4166667 numeric elements having 3333333 unique entries.\n\nNote: This is only applicable for simple, one-dimensional arrays since `array_flip` replaces keys by values.\n\n### 2. Get random array element\n\nMethod:\n\n```php\n// using\n$array[mt_rand(0, count($array) - 1)];\n// instead of\narray_rand($array);\n```\n\nTest:\n\n```bash\nphp test_array_rand.php\n```\n\nResult:\n\n| method | execution time |\n|--------|---------------:|\n| `array_rand` | 25.99 μs |\n| `mt_rand` | 0.95 μs |\n\nThe alternative approach is **27.3x** (96.33%) faster in this measurement. On average, it was ~8x (87%) faster. Tested on an array with 5000000 random numeric elements.\n\n### 3. Test for alphanumeric characters\n\nMethod:\n\n```php\n// using\nctype_alnum($string);\n// instead of\npreg_match('/[a-zA-Z0-9]+/', $string);\n```\n\nTest:\n\n```bash\nphp test_preg_match.php\n```\n\nResult:\n\n| method | execution time |\n|--------|---------------:|\n| `preg_match` | 15.39 ms |\n| `ctype_alnum` | 2.06 ms |\n\nThe alternative approach is **7.5x** (86.59%) faster in this measurement. On average, it was ~4x (76%) faster. Tested on an array of alphanumeric and non-alphanumeric strings with 104448 elements.\n\nThe same applies to `ctype_alpha()` (check for alphabetic characters) and `ctype_digit()` (check for numeric characters)\n\n### 4. Replace substrings\n\nMethod:\n\n```php\n// using\nstrtr($string, 'a', 'b');\n// instead of\nstr_replace('a', 'b', $string);\n```\n\nTest:\n\n```bash\nphp test_string_replace.php\n```\n\nResult:\n\n| method | execution time |\n|--------|---------------:|\n| `str_replace` | 676.59 ms |\n| `strtr` | 305.59 ms |\n\nThe alternative approach is **2.2x** (54.83%) faster in this measurement. On average, it was ~2x (51%) faster. Tested on an array of random strings with 5000000 elements.\n\n### Additional performance improvements\n\nHere are some additional points I integrated into my coding convention that I found to improve perfomance slightly (if applicable):\n\n- Prefer JSON over XML\n- Declare variables before, not in every iteration of the loop\n- Avoid function calls in the loop header (in `for ($i=0; $i\u003ccount($array); $i)` the `count()` method gets called in every iteration)\n- Unset memory consuming variables\n- Prefer select statement over multiple if statements\n- Prefer require/include over require_once/include_once (ensure proper opcode caching)\n\n## Sources\n\n- \u003chttps://gist.github.com/bsalim/4442047\u003e\n- \u003chttps://stackoverflow.com/questions/4195937/what-are-some-good-php-performance-tips\u003e\n","funding_links":["https://github.com/sponsors/devmount","https://paypal.me/devmount"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevmount%2Ffaster-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevmount%2Ffaster-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevmount%2Ffaster-php/lists"}