本帖最后由 523066680 于 2021-12-19 19:24 编辑
回复 13# netbenton
把你的方案翻译成其他语言,加载改用JSON解析器一次性加载(逐行解析相对耗时)
处理过程是复刻你的逻辑,实测70W行数据只需要0.9秒完成输出(到文件)
15楼代码 1.3秒
16楼代码 2.2秒- use Modern::Perl;
- use File::Slurp;
- use Time::HiRes qw/time/;
- use JSON qw/from_json/;
- use feature 'signatures';
- no warnings 'experimental::signatures';
- STDOUT->autoflush(1);
-
- my $ta = time();
- my $n = 0;
- open my $FH, ">:raw", "Category_Tree_benton.json";
- say $FH "{";
-
- my %hash;
- my $prev;
- my $raw = read_file( "gener_big.json" );
- my $data = from_json( $raw, {relaxed => 1} );
-
- for my $e ( @{$data->{data}} )
- {
- my ( $parent, $child, $label, $lv ) = @$e;
- next unless defined $lv;
-
- if ( $n < $lv )
- {
- if ( not defined $hash{$parent} )
- {
- say $FH "\t"x$lv, qq("$parent"), ":{";
- $hash{$parent} = 1;
- }
- else
- {
- say $FH "\t"x($lv+1), $child;
- }
- $prev = $child;
- $n = $lv;
- }
- elsif ( $n > $lv )
- {
- my $m = $lv + 1;
- for my $e ( reverse ($m .. $n) )
- {
- if ( defined $prev ) {
- say $FH "\t"x($e+1), qq("$prev"), ":{}" ;
- undef $prev;
- }
- say $FH "\t"x$e, "},";
- $n = $lv;
- }
- $prev = $child;
- }
- else
- {
- if ( defined $prev ) {
- say $FH "\t"x($lv+1), qq("$prev"), ":{},";
- }
- $prev = $child;
- }
- }
-
- if ( defined $prev )
- {
- say $FH "\t"x$n, qq("$prev"), ":{}";
- undef $prev;
- }
-
- # 收尾
- grep { say $FH "\t"x$_, "}"; } reverse ( 0 .. $n );
- close $FH;
-
- time_delta(\$ta);
-
- sub time_delta ($t)
- {
- printf "Time Delta: %.2f\n", time() - $$t;
- $$t = time();
- }
复制代码
|