0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

日本情報クリエイト Engineer'sAdvent Calendar 2017

Day 11

【php】serialize() VS var_export() VS json_encode() VS http_build_query()

Posted at

【php】serialize() VS var_export() VS json_encode() VS http_build_query()

結論:json_encodeが早い。

php7環境で下記コードを実行

<?php
$a = array_fill(0, 1000, array_fill(0, 1000, 1));
$max = 1000;

echo "serialize " . microtime() . "\n";
for ($i=0; $i<$max; $i++) {
    if (!($i % 100)) echo $i . "\n";
    $b = serialize($a);
}
echo microtime() . "\n\n";

echo "var_export " . microtime() . "\n";
for ($i=0; $i<$max; $i++) {
    if (!($i % 100)) echo $i . "\n";
    $b = var_export($a, true);
}
echo microtime() . "\n\n";

echo "json_encode " . microtime() . "\n";
for ($i=0; $i<$max; $i++) {
    if (!($i % 100)) echo $i . "\n";
    $b = json_encode($a);
}
echo microtime() . "\n\n";

echo "http_build_query " . microtime() . "\n";
for ($i=0; $i<$max; $i++) {
    if (!($i % 100)) echo $i . "\n";
    $b = http_build_query($a);
}
echo microtime() . "\n\n";

実行結果

serialize 0.13482000 1561615171
0
100
200
300
400
500
600
700
800
900
0.38630600 1561615221

var_export 0.38633600 1561615221
0
100
200
300
400
500
600
700
800
900
0.19411200 1561615319

json_encode 0.19412900 1561615319
0
100
200
300
400
500
600
700
800
900
0.97560400 1561615333

http_build_query 0.97562600 1561615333
0
100
200
300
400
500
600
700
800
900
0.72081200 1561615394
  • serialize 約50秒
  • var_export 約98秒
  • json_encode 約24秒
  • http_build_query 約61秒

結論:json_encodeが早い。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?