0
0

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.

Perl 6Advent Calendar 2015

Day 12

Perl6とJSON

Posted at

Perl 6 Advent Calendar 2015の12日目です(遅刻)

JSON

Perl6 で JSON を扱うには to-json()from-json() を使います。

use v6;

my $file = '/usr/local/lib/node_modules/npm/package.json';
my $json = $file.IO.slurp;

# JSONから
my $a = from-json( $json );
$a.perl.say;

# JSONへ
my $b = to-json( $a );
$b.say;

Benchmark

ただ、built-in の JSON::Fast は遅いという噂があるので、今でもそうなのかベンチマークを取ってみよう。

# JSON::Faster のインストール
$ panda install git://github.com/tony-o/perl6-json-faster.git

# JSON::Tiny のインストール
$ panda install JSON::Tiny

サンプルのjsonファイルは JSON::Faster 付録のベンチマークのものを使います。
perl6-json-faster/benchmark/

use v6;
use Bench;

my $json = 'projects.json'.IO.slurp;
my $nosj = from-json( $json );

my $繰り返し回数 = 3 #`(回) ;
my $bench = Bench.new;

$bench.cmpthese(
  $繰り返し回数,
  {
    'JSON::Fast.to-json'   => sub {                   to-json( $nosj ); },
    'JSON::Faster.to-json' => sub { use JSON::Faster; to-json( $nosj ); },
    'JSON::Tiny.to-json'   => sub { use JSON::Tiny;   to-json( $nosj ); },
  });

$bench.cmpthese(
  $繰り返し回数,
  {
    'JSON::Fast.from-json'   => sub {                   from-json( $json ); },
    'JSON::Faster.from-json' => sub { use JSON::Faster; from-json( $json ); },
    'JSON::Tiny.from-json'   => sub { use JSON::Tiny;   from-json( $json ); },
  });
出力
Benchmark:
Timing 3 iterations of JSON::Fast.to-json, JSON::Faster.to-json, JSON::Tiny.to-json...
JSON::Fast.to-json: 322.3456 wallclock secs @ 0.0093/s (n=3)
		(warning: too few iterations for a reliable count)
JSON::Faster.to-json: 7.2296 wallclock secs @ 0.4150/s (n=3)
		(warning: too few iterations for a reliable count)
JSON::Tiny.to-json: 259.3022 wallclock secs @ 0.0116/s (n=3)
		(warning: too few iterations for a reliable count)
O----------------------O--------O----------------------O--------------------O--------------------O
|                      | s/iter | JSON::Faster.to-json | JSON::Tiny.to-json | JSON::Fast.to-json |
O======================O========O======================O====================O====================O
| JSON::Faster.to-json | 2.41   | --                   | 3487%              | 4359%              |
| JSON::Tiny.to-json   | 86.4   | -97%                 | --                 | 24%                |
| JSON::Fast.to-json   | 107    | -98%                 | -20%               | --                 |
--------------------------------------------------------------------------------------------------
Benchmark:
Timing 3 iterations of JSON::Fast.from-json, JSON::Faster.from-json, JSON::Tiny.from-json...
JSON::Fast.from-json: 11.2349 wallclock secs @ 0.2670/s (n=3)
		(warning: too few iterations for a reliable count)
JSON::Faster.from-json: 12.5122 wallclock secs @ 0.2398/s (n=3)
		(warning: too few iterations for a reliable count)
JSON::Tiny.from-json: 10.4782 wallclock secs @ 0.2863/s (n=3)
		(warning: too few iterations for a reliable count)
O------------------------O--------O----------------------O------------------------O----------------------O
|                        | s/iter | JSON::Fast.from-json | JSON::Faster.from-json | JSON::Tiny.from-json |
O========================O========O======================O========================O======================O
| JSON::Fast.from-json   | 3.74   | --                   | 11%                    | -7%                  |
| JSON::Faster.from-json | 4.17   | -10%                 | --                     | -16%                 |
| JSON::Tiny.from-json   | 3.49   | 7%                   | 19%                    | --                   |
----------------------------------------------------------------------------------------------------------

to-jsonJSON::Fasterがダントツで超速く、from-jsonJSON::Tinyが若干速い感じでしょうか。

おわりです。

参考と注釈

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?