Skip to content

Instantly share code, notes, and snippets.

@ikuwow
Created April 4, 2016 07:16
Show Gist options
  • Save ikuwow/5c223fc72f94d1949cd048ad2492e499 to your computer and use it in GitHub Desktop.
Save ikuwow/5c223fc72f94d1949cd048ad2492e499 to your computer and use it in GitHub Desktop.
<?php
$input = 'hoge00';
$trials = 100000;
$tic = microtime(true);
for ($i = 0; $i < $trials; $i++) {
preg_match("/^[0-9]+$/", $input);
}
echo (string)(microtime(true) - $tic) . "\n";
$tic = microtime(true);
for ($i = 0; $i < $trials; $i++) {
ctype_digit($input);
}
echo (string)(microtime(true) - $tic) . "\n";
@ikuwow
Copy link
Author

ikuwow commented Apr 4, 2016

In this trial, ctype_digit() is 3 times faster than preg_match().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment