1

在我的 Web 应用程序中,我使用下面的代码在运行时动态加载模块,但看起来它需要花费大量时间,这使得应用程序有点慢。

eval {
    eval "require $package_name";
}
if ($@) {
    die"Error while loading module: $@\n";
}

我做了一个小程序来检查eval "require $package_name";这段代码平均需要多少时间,然后我发现它需要将近 10 秒,这对于 Web 应用程序来说是巨大的。

use strict;
use Time::HiRes qw( gettimeofday tv_interval );

my $startTime = [ gettimeofday() ];
print "Time:@{$startTime}->[0]\n";

eval "require Foo::Bar"; # giving example

my $timeSpent = tv_interval( $startTime, [ gettimeofday() ] );
print "Time Spent:$timeSpent\n";

exit 1;

输出:

Time:1378897304
Time Spent:10.627147

所以我的问题是,为什么这需要这么多时间,有没有其他方法可以解决这个问题?

4

1 回答 1

1

是的。

使用 apache 和 mod_perl 并在启动时加载模块。

http://www.conceptsolutionsbc.com/perl-articles-mainmenu-41/29-perl-and-apache/55-modperl-part-2-pre-loading-perl-modules

我认为最好的方法是使用一个非常简单的脚本,它只需要你所有使用过的模块:

PerlRequire "/usr/local/apache2/conf/startup.pl" 
于 2013-09-11T11:18:21.570 回答