json_vs_storable.pl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/perl
  2. # Speed comparison of JSON::XS vs Storable.
  3. # Result:
  4. # Storable is significantly faster for both encoding and decoding of data.
  5. use 5.014;
  6. use strict;
  7. use warnings;
  8. use Storable qw(freeze thaw);
  9. use JSON::XS qw(encode_json decode_json);
  10. use LWP::Simple qw(get);
  11. use Benchmark qw(cmpthese);
  12. my $info = {
  13. content => get("https://github.com/"),
  14. description => "GitHub is where people build software. More than 73 million people use GitHub to discover, fork, and contribute to over 200 million projects.",
  15. id => "2df61d3f",
  16. keywords => undef,
  17. score => 2,
  18. title => "This is a test",
  19. url => "https://github.com/",
  20. };
  21. my $storable = freeze($info);
  22. my $json = encode_json($info);
  23. say "# Decoding speed:\n";
  24. cmpthese(
  25. -1,
  26. {
  27. json => sub {
  28. my $data = decode_json($json);
  29. },
  30. storable => sub {
  31. my $data = thaw($storable);
  32. },
  33. }
  34. );
  35. say "\n# Encoding speed:\n";
  36. cmpthese(
  37. -1,
  38. {
  39. json => sub {
  40. my $data = encode_json($info);
  41. },
  42. storable => sub {
  43. my $data = freeze($info);
  44. },
  45. }
  46. );
  47. __END__
  48. # Decoding speed:
  49. Rate json storable
  50. json 2327/s -- -94%
  51. storable 41533/s 1685% --
  52. # Encoding speed:
  53. Rate json storable
  54. json 1541/s -- -93%
  55. storable 21721/s 1309% --