On May 25, 10:05 pm, jason.li...@gmail.com (Xi Liu) wrote: > 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 guess > was true. > for($i = 0; $i < @lines; $i++) > { > $sum += $lines[$_]->{foo_value} for ($i - $n + 1 .. $i);} > > sorry I can't post the whole code, but the lines above is the big framework, > I also omit all the boundary check. "$sum += $lines[$_]->{foo_value} for ($i > - $n2 + 1 .. $i)" this line consumes all the time. > I got a array, for each element of the array I have to sum up the $n > elements prior to the current one. I know I should use pop push shift > unshift all the perl list operators instead of subscripting, but I just > don't know how, would somebody tell me what is the perl way of doing this?
Another option, if Perl performance remains too slow after your improvements, would be an XS extension interface between Perl and C code. See: perldoc perlxs or, for a different approach and a much easier learning curve, See: Inline::C and/or Inline::C-Cookbook. -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/