Created
October 27, 2011 18:40
-
-
Save miau/1320426 to your computer and use it in GitHub Desktop.
benchmark of creating PHP DateTime with microseconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(overhead): 0.012856006622314 | |
gettimeofday+list: 0.79171109199524 | |
gettimeofday: 0.78512001037598 | |
gettimeofday2: 0.77574801445007 | |
trim: 0.62337398529053 | |
substr: 0.63026404380798 | |
substr+createFromFormat: 0.60127902030945 | |
float: 0.59996008872986 | |
float2: 0.60790085792542 | |
float3: 0.60153198242188 | |
float3+date_create: 0.59856009483337 | |
float3+date_create_from_format: 0.56997299194336 | |
float3+str_pad: 0.56047797203064 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// PHP で DateTime オブジェクトをミリ秒つきで取得する各方法のベンチマーク | |
$funcs = array( | |
'(overhead)' => function() { | |
}, | |
// microtime() でなく gettimeofday() を使った例 | |
// list で受け取る→一番遅い | |
'gettimeofday+list' => function() { | |
list($sec, $usec) = array_values(gettimeofday()); | |
return new DateTime(date('Y-m-d H:i:s.', $sec) . sprintf('%06d', $usec)); | |
}, | |
// gettimeofday() を素直に使う例 | |
// 内部で連想配列が作られる&usec が 6 桁あるとは限らないのでゼロ埋めが必要で microtime() より遅い | |
'gettimeofday' => function() { | |
$time = gettimeofday(); | |
return new DateTime(date('Y-m-d H:i:s.', $time['sec']) . sprintf('%06d', $time['usec'])); | |
}, | |
// ドットの連結を date() の外に出してみた→上記とは大差なし | |
'gettimeofday2' => function() { | |
$time = gettimeofday(); | |
return new DateTime(date('Y-m-d H:i:s', $time['sec']) . '.' . sprintf('%06d', $time['usec'])); | |
}, | |
// これ以降しばらく microtime() の文字列版を使用 | |
// trim() を使ってマイクロ秒の文字列表現 0.xxxxxx00 を置換しているのがポイント | |
'trim' => function() { | |
list($usec, $sec) = explode(' ', microtime()); | |
return new DateTime(date('Y-m-d H:i:s', $sec) . trim($usec, '0')); | |
}, | |
// trim() の代わりに substr() を使ってみた→こちらのほうがやや速い? | |
'substr' => function() { | |
list($usec, $sec) = explode(' ', microtime()); | |
return new DateTime(date('Y-m-d H:i:s', $sec) . substr($usec, 1, 7)); | |
}, | |
// createFromFormat を使う→new するよりは速い | |
'substr+createFromFormat' => function() { | |
list($usec, $sec) = explode(' ', microtime()); | |
return DateTime::createFromFormat('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $sec) . substr($usec, 1, 7)); | |
}, | |
// これ以降は microtime() の float 版を使用。基本的に文字列より速い。 | |
'float' => function() { | |
$t = microtime(true); | |
$micro = sprintf('%06d', ($t - floor($t)) * 1000000); | |
return new DateTime(date('Y-m-d H:i:s.' . $micro, $t)); | |
}, | |
// マイクロ秒の連結を date() の外に出してみた→フォーマット解析が短くなるため速くなる | |
'float2' => function() { | |
$t = microtime(true); | |
$micro = sprintf('%06d', ($t - floor($t)) * 1000000); | |
return new DateTime(date('Y-m-d H:i:s.', $t) . $micro); | |
}, | |
// さらにドットも外に出してみた→ほんの少し速くなる | |
'float3' => function() { | |
$t = microtime(true); | |
$micro = sprintf('%06d', ($t - floor($t)) * 1000000); | |
return new DateTime(date('Y-m-d H:i:s', $t) . '.' . $micro); | |
}, | |
// new DateTime() のエイリアスである date_create() を利用。こちらのほうがやや速い | |
'float3+date_create' => function() { | |
$t = microtime(true); | |
$micro = sprintf('%06d', ($t - floor($t)) * 1000000); | |
return date_create(date('Y-m-d H:i:s', $t) . '.' . $micro); | |
}, | |
// date_create_from_format を利用。こちらのほうがやや速い。 | |
'float3+date_create_from_format' => function() { | |
$t = microtime(true); | |
$micro = sprintf('%06d', ($t - floor($t)) * 1000000); | |
return date_create_from_format('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $t) . '.' . $micro); | |
}, | |
// sprintf の代わりに str_pad でゼロ埋め→こちらのほうが速い? | |
'float3+str_pad' => function() { | |
$t = microtime(true); | |
$micro = str_pad((int) (($t - floor($t)) * 1000000), 6, '0', STR_PAD_LEFT); | |
return date_create_from_format('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $t) . '.' . $micro); | |
}, | |
); | |
foreach ($funcs as $name => $func) { | |
$time_start = microtime(true); | |
for ($i = 0; $i < 10000; $i++) { | |
$func(); | |
} | |
$time_end = microtime(true); $time = $time_end - $time_start; | |
printf("%30s: %s\n", $name, $time); | |
} | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment