Skip to content

Instantly share code, notes, and snippets.

@taiyoh
Created September 15, 2010 07:35
Show Gist options
  • Save taiyoh/580376 to your computer and use it in GitHub Desktop.
Save taiyoh/580376 to your computer and use it in GitHub Desktop.
<?php
$tel_reg = '/^(\d| |-|\+|\(|\))*$/';
$tel_reg2 = '/^[0-9()+\- ]*$/';
$tel = '+81 (03) 5562-9540';
foreach (array($tel_reg, $tel_reg2) as $reg) {
$start_m = microtime();
$start = time();
foreach (range(1, 10000000) as $n) {
preg_match($reg, $tel);
}
$end_m = microtime();
$end = time();
$bench = ($end - $start) + ($end_m - $start_m);
var_dump(array($reg => $bench));
}
/**
*
* result
array(1) {
["/^(\d| |-|\+|\(|\))*$/"]=>
float(365.280684)
}
array(1) {
["/^[0-9()+\- ]*$/"]=>
float(57.13032)
}
*/
#!/usr/bin/env perl
use common::sense;
use utf8;
use Benchmark qw(:all) ;
my $tel = '+81 (03) 5562-9540';
timethese(10000000, {
'^(\d| |-|\+|\(|\))*$' => sub { $tel =~ /^(\d| |-|\+|\(|\))*$/ },
'^[0-9()+\- ]*$' => sub { $tel =~ /^[0-9()+\- ]*$/ },
});
__END__
result:
Benchmark: timing 10000000 iterations of ^(\d| |-|\+|\(|\))*$, ^[0-9()+\- ]*$...
^(\d| |-|\+|\(|\))*$: 27 wallclock secs (27.92 usr + 0.03 sys = 27.95 CPU) @ 357781.75/s (n=10000000)
^[0-9()+\- ]*$: 4 wallclock secs ( 3.37 usr + 0.00 sys = 3.37 CPU) @ 2967359.05/s (n=10000000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment