Advertisement
Guest User

Untitled

a guest
May 27th, 2012
2,367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. <?php
  2. // PHP で DateTime オブジェクトをミリ秒つきで取得する各方法のベンチマーク
  3. $funcs = array(
  4. '(overhead)' => function() {
  5. },
  6. // microtime() でなく gettimeofday() を使った例
  7. // list で受け取る→一番遅い
  8. 'gettimeofday+list' => function() {
  9. list($sec, $usec) = array_values(gettimeofday());
  10. return new DateTime(date('Y-m-d H:i:s.', $sec) . sprintf('%06d', $usec));
  11. },
  12. // gettimeofday() を素直に使う例
  13. // 内部で連想配列が作られる&usec が 6 桁あるとは限らないのでゼロ埋めが必要で microtime() より遅い
  14. 'gettimeofday' => function() {
  15. $time = gettimeofday();
  16. return new DateTime(date('Y-m-d H:i:s.', $time['sec']) . sprintf('%06d', $time['usec']));
  17. },
  18. // ドットの連結を date() の外に出してみた→上記とは大差なし
  19. 'gettimeofday2' => function() {
  20. $time = gettimeofday();
  21. return new DateTime(date('Y-m-d H:i:s', $time['sec']) . '.' . sprintf('%06d', $time['usec']));
  22. },
  23. // これ以降しばらく microtime() の文字列版を使用
  24. // trim() を使ってマイクロ秒の文字列表現 0.xxxxxx00 を置換しているのがポイント
  25. 'trim' => function() {
  26. list($usec, $sec) = explode(' ', microtime());
  27. return new DateTime(date('Y-m-d H:i:s', $sec) . trim($usec, '0'));
  28. },
  29. // trim() の代わりに substr() を使ってみた→こちらのほうがやや速い?
  30. 'substr' => function() {
  31. list($usec, $sec) = explode(' ', microtime());
  32. return new DateTime(date('Y-m-d H:i:s', $sec) . substr($usec, 1, 7));
  33. },
  34. // createFromFormat を使う→new するよりは速い
  35. 'substr+createFromFormat' => function() {
  36. list($usec, $sec) = explode(' ', microtime());
  37. return DateTime::createFromFormat('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $sec) . substr($usec, 1, 7));
  38. },
  39. // これ以降は microtime() の float 版を使用。基本的に文字列より速い。
  40. 'float' => function() {
  41. $t = microtime(true);
  42. $micro = sprintf('%06d', ($t - floor($t)) * 1000000);
  43. return new DateTime(date('Y-m-d H:i:s.' . $micro, $t));
  44. },
  45. // マイクロ秒の連結を date() の外に出してみた→フォーマット解析が短くなるため速くなる
  46. 'float2' => function() {
  47. $t = microtime(true);
  48. $micro = sprintf('%06d', ($t - floor($t)) * 1000000);
  49. return new DateTime(date('Y-m-d H:i:s.', $t) . $micro);
  50. },
  51. // さらにドットも外に出してみた→ほんの少し速くなる
  52. 'float3' => function() {
  53. $t = microtime(true);
  54. $micro = sprintf('%06d', ($t - floor($t)) * 1000000);
  55. return new DateTime(date('Y-m-d H:i:s', $t) . '.' . $micro);
  56. },
  57. // new DateTime() のエイリアスである date_create() を利用。こちらのほうがやや速い
  58. 'float3+date_create' => function() {
  59. $t = microtime(true);
  60. $micro = sprintf('%06d', ($t - floor($t)) * 1000000);
  61. return date_create(date('Y-m-d H:i:s', $t) . '.' . $micro);
  62. },
  63. // date_create_from_format を利用。こちらのほうがやや速い。
  64. 'float3+date_create_from_format' => function() {
  65. $t = microtime(true);
  66. $micro = sprintf('%06d', ($t - floor($t)) * 1000000);
  67. return date_create_from_format('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $t) . '.' . $micro);
  68. },
  69. // sprintf の代わりに str_pad でゼロ埋め→こちらのほうが速い?
  70. 'float3+str_pad' => function() {
  71. $t = microtime(true);
  72. $micro = str_pad((int) (($t - floor($t)) * 1000000), 6, '0', STR_PAD_LEFT);
  73. return date_create_from_format('Y-m-d H:i:s.u', date('Y-m-d H:i:s', $t) . '.' . $micro);
  74. },
  75. );
  76.  
  77. foreach ($funcs as $name => $func) {
  78. $time_start = microtime(true);
  79. for ($i = 0; $i < 10000; $i++) {
  80. $func();
  81. }
  82. $time_end = microtime(true); $time = $time_end - $time_start;
  83. printf("%30s: %s\n", $name, $time);
  84. }
  85. echo "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement