Dear all,
Recently I discovered, thanks to people of this forum the use of Benchmark. A great powerful tool comparing speeds, processes and times.
Well since I love playing around with details I could not resist to make this make not useful test and compare the Config::IniFiles Vs Config::Simple.I decided to run an experiment and observe the output.
Update:Thanks to Anonymous Monk and davido for their contributions and suggestions, I have modified my experiment and came up with new data.
The experimental code:
#!/usr/bin/perl use strict; use warnings; use List::Compare; use Config::Simple; use Config::IniFiles; use Fcntl qw(:flock); use Benchmark qw(:all) ; use Data::Dumper qw(Dumper); $|=1; #flush every time the program =flock sub LOCK_SH { 1 } ## shared lock sub LOCK_EX { 2 } ## exclusive lock sub LOCK_NB { 4 } ## non-blocking sub LOCK_UN { 8 } ## unlock =cut my %information; my $path = 'conf.ini'; my $count = -5 || die "Need a count!\n"; sub complex { open my $fh , '<' , "".$path."" or die "Could not open file: ".$path." - $!\n"; flock($fh, LOCK_SH) or die "Could not lock '".$fh."' - $!\n"; tie my %ini, 'Config::IniFiles', ( -file => "".$path."" ) or die "Error: IniFiles->new: @Config::IniFiles::errors"; close ($fh) or die "Could not close '".$path."' - $!\n"; print Dumper(\%ini); return %ini; } # end sub complex sub val { open my $fr , '<' , "".$path."" or die "Could not open file: ".$path." - $!\n"; flock($fr, LOCK_SH) or die "Could not lock '".$fr."' - $!\n"; my $cfg = Config::IniFiles->new( -file => "".$path."" ) or die "error: IniFiles->new: @Config::IniFiles::errors"; close ($fr) or die "Could not close '".$path."' - $!\n"; my $values = $cfg->val( 'Perl', 'test' ); my @array = split(',', $values); print Dumper(\@array); return @array; } # End of sub val sub simple { open my $fr , '<' , "".$path."" or die "Could not open file: ".$path." - $!\n"; flock($fr, LOCK_SH) or die "Could not lock '".$fr."' - $!\n"; Config::Simple->import_from( "".$path."", \%information) or die Config::Simple->error(); close ($fr) or die "Could not close '".$path."' - $!\n"; print Dumper(\%information); return %information; } # End of sub simple my $r = timethese ( $count , { 'complex' => '&complex', 'val' => '&val', 'simple' => '&simple' } ); cmpthese $r;
I am using a common conf.ini folder that both scripts are reading from with flock process applied since I am planning to use in combination with other scripts.
Sample of the conf.ini folder:
[Perl] test= bar,baz,foo,quux
The results after the test are the following:
val: 6 wallclock secs ( 5.69 usr + 0.30 sys = 5.99 CPU) @ 989.98/s +(n=5930) Rate complex val simple complex 700/s -- -29% -46% val 990/s 41% -- -24% simple 1297/s 85% 31% --
Well to be honest I was expecting the complex version of Config::IniFiles to be faster in comparison to Config::Simple due to simplicity of the code. But the results have proved my assumption wrong. Thanks to Anonymous Monk that he elaborate regarding the tie and OOP process I decided to add also the val process to make it more fair. Also davido point out that the unlock process is a trap and possibly the data can still remain within the process, so it is better to close the file in order to flush the output.
Again thank you all for your contribution, to beginners like my self this is a big boost on the learning curve.
Well I do not know if this comparison makes any sense to anyone. But since I am beginner and all of this stuff make a huge impression, I felt it would be nice to mention this. Just in case that someone needs to use one of these two solutions to get also an idea about speed.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: config::simple vs config::ini
by davido (Cardinal) on May 21, 2014 at 02:43 UTC | |
by thanos1983 (Parson) on May 21, 2014 at 07:25 UTC | |
|
Re: RFC: config::simple vs config::ini
by Jenda (Abbot) on May 21, 2014 at 09:34 UTC | |
by thanos1983 (Parson) on May 21, 2014 at 10:44 UTC | |
|
Re: RFC: config::simple vs config::ini
by Anonymous Monk on May 21, 2014 at 00:03 UTC | |
by thanos1983 (Parson) on May 21, 2014 at 07:20 UTC |