Perl中while(每个)和foreach之间的性能差异

维诺德

我测试的哈希包含大约70000所大学,每所大学包含大约20名学生。我尝试了5次,以下是结果。foreach性能和while(每个)性能存在很大差异。为什么呢?

带while循环的代码:

while ( my ($college_code, $college_info_hr) = each (%{$college_data_hr}) ) {
    while ( my ($student_num, $student_info_hr) = each (%{$college_info_hr->{'students'}}) ) {
        if($student_num < 104000) { ## Delete the info of students before 2004.
            delete $college_info_hr->{'students'}{$student_num};
        }
    }
}

带foreach循环的代码:

foreach my $college_code (keys %{$college_data_hr}) {
    foreach my $student_num (keys %{$college_data_hr->{$college_code}{'students'}}) {
        if($student_num < 104000) { ## Delete the info of students before 2004.
            delete $college_data_hr->{$college_code}{'students'}{$student_num};
        }
    }
}

当大学数量为70,000时,执行时间为:

对于while循环的代码(间隔时间以秒为单位):

间隔时间:2.186621

间隔时间:2.058644

间隔时间:2.055645

间隔时间:2.101637

间隔时间:2.124632

对于带有foreach循环的代码:(间隔时间以秒为单位)

间隔时间:1.341768

间隔时间:1.436751

间隔时间:1.346529

间隔时间:1.302775

间隔时间:1.356765

当大学数量是248,000时,执行时间是:

(while循环的执行时间)

间隔时间:9.084427

间隔时间:8.438684

间隔时间:9.329338

间隔时间:9.169687

(foreach循环的执行时间)

间隔时间:5.502048

间隔时间:6.386692

间隔时间:5.596032

间隔时间:5.620144

Hynek -Pichi-他走了出去

问题是Perl无法进行一些优化,这在许多编译的编程语言中都是很常见的。正如ikegami指出的那样,在每个循环中,您都从哈希中复制数据,并且您还会执行许多不必要的哈希查找。您可以使用一些基准代码。

#!/usr/bin/env perl

use 5.10.0;
use strict;
use warnings;
use Benchmark qw(:hireswallclock :all);
use Clone qw(clone);

my $data = {
    map +( $_ => { students => { map +( $_ => undef ), 103991 .. 104010 } } ),
    1 .. 70000
};
my $college_data_hr;

sub sum_time {
    my $t = shift;
    $t = timesum( $t, $_ ) for @_;
    return $t;
}

sub my_cmp_these {
    my %bench = @_;
    my %times;
    for ( 1 .. 10 ) {
        push @{ $times{$_} }, do {
            $college_data_hr = clone($data);
            timeit( 1, $bench{$_} );
            }
            for keys %bench;
    }
    $_ = sum_time(@$_) for values %times;
    cmpthese( \%times );
}

my_cmp_these(
    orig_while => sub {
        while ( my ( $college_code, $college_info_hr )
            = each( %{$college_data_hr} ) )
        {
            while ( my ( $student_num, $student_info_hr )
                = each( %{ $college_info_hr->{'students'} } ) )
            {
                if ( $student_num < 104000 )
                {    ## Delete the info of students before 2004.
                    delete $college_info_hr->{'students'}{$student_num};
                }
            }
        }
    },
    new_while => sub {
        while ( my ( undef, $college_info_hr ) = each( %{$college_data_hr} ) )
        {
            my $s = $college_info_hr->{'students'};
            while ( my ( $student_num, undef ) = each(%$s) ) {
                if ( $student_num < 104000 )
                {    ## Delete the info of students before 2004.
                    delete $s->{$student_num};
                }
            }
        }
    },
    orig_foreach => sub {
        foreach my $college_code ( keys %$college_data_hr ) {
            foreach my $student_num (
                keys %{ $college_data_hr->{$college_code}{'students'} } )
            {
                if ( $student_num < 104000 )
                {    ## Delete the info of students before 2004.
                    delete $college_data_hr->{$college_code}{'students'}
                        {$student_num};
                }
            }
        }
    },
    new_foreach => sub {
        foreach my $college_info ( values %$college_data_hr ) {
            my $students = $college_info->{'students'};
            delete @$students{ grep $_ < 104000, keys %$students };
        }
    },
    ikegami_foreach => sub {
        for my $college_code ( keys %$college_data_hr ) {
            my $students = $college_data_hr->{$college_code}{students};
            delete @$students{ grep $_ < 104000, keys %$students };
        }
    },
);

我的笔记本上的结果:

                s/iter orig_while new_while orig_foreach ikegami_foreach new_foreach
orig_while        1.56         --      -25%         -31%            -35%        -40%
new_while         1.17        33%        --          -8%            -14%        -21%
orig_foreach      1.08        44%        8%           --             -6%        -14%
ikegami_foreach   1.01        54%       16%           7%              --         -8%
new_foreach      0.927        68%       26%          16%              9%          --

的结果 248,000

                s/iter orig_while new_while orig_foreach ikegami_foreach new_foreach
orig_while        6.19         --      -27%         -30%            -33%        -38%
new_while         4.54        36%        --          -5%             -8%        -16%
orig_foreach      4.31        44%        5%           --             -4%        -11%
ikegami_foreach   4.16        49%        9%           4%              --         -8%
new_foreach       3.83        62%       19%          13%              9%          --

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

?? 之间的性能差异 和 ==

来自分类Dev

for,while和do while循环之间的性能差异?

来自分类Dev

angular.foreach和native for循环之间的性能差异

来自分类Dev

angular.foreach和native for循环之间的性能差异

来自分类Dev

Android中query()和rawQuery()方法之间的性能差异

来自分类Dev

JavaScript中的“ let”和“ var”之间是否存在性能差异

来自分类Dev

在foreach循环中使用parallel.foreach和task之间的性能差异是什么?

来自分类Dev

perl中m / PATTERN /和/ PATTERN /之间的差异

来自分类Dev

perl中m / PATTERN /和/ PATTERN /之间的差异

来自分类Dev

Perl中打印之间的差异

来自分类Dev

Perl中打印之间的差异

来自分类Dev

管道和流程替代之间的性能差异

来自分类Dev

Linux和Windows之间的numpy性能差异

来自分类Dev

阵列,堆栈和队列之间的性能差异

来自分类Dev

Firefox和Chrome之间的webgl性能差异

来自分类Dev

管道和流程替代之间的性能差异

来自分类Dev

drawBitmap和createScaledBitmap之间的性能差异

来自分类Dev

并行和嵌套IF条件之间的性能差异

来自分类Dev

x86和x86_64中的float和double之间的性能差异

来自分类Dev

<FH>和<$ FH>之间的Perl差异

来自分类Dev

C中while循环和for循环之间的差异导致输出差异

来自分类Dev

平方函数和直接在Rust中调用powi(2)之间是否存在性能差异?

来自分类Dev

SQL Server中不存在和左联接之间的性能差异

来自分类Dev

python中keras和tensorflow.keras之间的意外性能差异

来自分类Dev

MongoDb中的MongoServer.Create()和mongoClient.GetServer()之间的性能差异

来自分类Dev

平方函数和直接在Rust中调用powi(2)之间是否存在性能差异?

来自分类Dev

swift中nil和空字符串之间基于性能的差异

来自分类Dev

在 Common Lisp 中,函数和宏之间是否存在性能差异?

来自分类Dev

Bash中$ *和$ @之间的差异

Related 相关文章

  1. 1

    ?? 之间的性能差异 和 ==

  2. 2

    for,while和do while循环之间的性能差异?

  3. 3

    angular.foreach和native for循环之间的性能差异

  4. 4

    angular.foreach和native for循环之间的性能差异

  5. 5

    Android中query()和rawQuery()方法之间的性能差异

  6. 6

    JavaScript中的“ let”和“ var”之间是否存在性能差异

  7. 7

    在foreach循环中使用parallel.foreach和task之间的性能差异是什么?

  8. 8

    perl中m / PATTERN /和/ PATTERN /之间的差异

  9. 9

    perl中m / PATTERN /和/ PATTERN /之间的差异

  10. 10

    Perl中打印之间的差异

  11. 11

    Perl中打印之间的差异

  12. 12

    管道和流程替代之间的性能差异

  13. 13

    Linux和Windows之间的numpy性能差异

  14. 14

    阵列,堆栈和队列之间的性能差异

  15. 15

    Firefox和Chrome之间的webgl性能差异

  16. 16

    管道和流程替代之间的性能差异

  17. 17

    drawBitmap和createScaledBitmap之间的性能差异

  18. 18

    并行和嵌套IF条件之间的性能差异

  19. 19

    x86和x86_64中的float和double之间的性能差异

  20. 20

    <FH>和<$ FH>之间的Perl差异

  21. 21

    C中while循环和for循环之间的差异导致输出差异

  22. 22

    平方函数和直接在Rust中调用powi(2)之间是否存在性能差异?

  23. 23

    SQL Server中不存在和左联接之间的性能差异

  24. 24

    python中keras和tensorflow.keras之间的意外性能差异

  25. 25

    MongoDb中的MongoServer.Create()和mongoClient.GetServer()之间的性能差异

  26. 26

    平方函数和直接在Rust中调用powi(2)之间是否存在性能差异?

  27. 27

    swift中nil和空字符串之间基于性能的差异

  28. 28

    在 Common Lisp 中,函数和宏之间是否存在性能差异?

  29. 29

    Bash中$ *和$ @之间的差异

热门标签

归档