Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement