17.04.09, 20:16, "Jim Gibson" <jimsgib...@gmail.com>: > On 4/16/09 Thu Apr 16, 2009 1:39 PM, "edw...@yandex.ru" <edw...@yandex.ru> > scribbled: > > Hello, > > > > I would like to know, how to force perl unfold "foreach" expression during > > the > > compilation, i.e. I want next code: > > > > > > foreach (1..100) { > > block > > } > > > > to be compiled like this: > > > > block[$_ = 1] > > > > block[$_ = 2] > > > > block[$_ = 3] > > > > . > > . > > . > > > > block[$_ = 100] > > > > Where block[$_ = n] means "block" with all occurences of $_ substituted with > > n. > > Of course, we presume that array like (1..100) is known at compile time. > Can you explain why you want to do this? > Compiler and run-time options for Perl are given in 'perldoc perlrun'. I > don't see anything there that will affect how the Perl compiler optimizes > the code it generates or specifically to unfold loops. > I suggest you write a program that measures the execution times for the > iterative-loop case and the unfolded case and compare them to see if you > will gain enough execution time (if that is indeed what you are seeking) to > justify the additional memory needed to contain the unwrapped block > statements. (Were I to do this, I would write a Perl program to generate the > Perl program!) See the Benchmark module.
The problem is that I did this benchmarking before. Actually, the code snippets above are run in a very long cycle, and if we have "foreach" statement, then we need to store some value for loop iteration and even one assignment of this variable consumes time significantly in my case (around several seconds in the whole). So, I ended up with solution with unfolding, but as I have mentioned I do not want to do it manually. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/