Re: How to avoid using the slow array subscripting operator?

2011-05-29 Thread Xi Liu
I guess I should provide some real code. below is a tiny module I grabbed from my project which is not that privacy but sufficient for discussion. #/usr/bin/perl use warnings; use 5.010; use Carp; use DB_File; { my %sum_cache; my %deviation_cache; tie %sum_cache => 'DB_File', "sum_cac

Re: How to avoid using the slow array subscripting operator?

2011-05-26 Thread Xi Liu
OK, I think I got the idea. I am going to change the useless code caching the result I have already summed or inline c code to achieve a better performance. Really learned something. Thank you!

Re: How to avoid using the slow array subscripting operator?

2011-05-26 Thread Xi Liu
I know I am doing the repetitive and useless summing again and again. What confuses me is that using the same algorithm, I mean for my $i (99 .. 6) { my $sum; map {$sum += $_->{foo_value}} @lines[$i - $n + 1 .. $i]; push @res, $sum; } in perl and for(int i =99; i <= 6;

Re: How to avoid using the slow array subscripting operator?

2011-05-26 Thread Xi Liu
Thanks for your help! I am sorry I missed something important in the code snippet. for($i = 0; $i < @lines; $i++) { $sum = 0; $sum += $lines[$_]->{foo_value} for ($i - $n + 1 .. $i); push @res, $sum; } this is what I intend.I try to make it clear and removed something so that change

How to avoid using the slow array subscripting operator?

2011-05-25 Thread Xi Liu
Hi all: I translated a program from c to perl.but the perl program cost 15 seconds compare to the original c one cost only less than 1 second, I guess this might be the result of I literally translated the program, using a lot of array subscripts. After I profile the perl program, it turned out my