|
|
| Perl Monk, Perl Meditation | |
| PerlMonks |
Re: When do you function?by Falkkin (Chaplain) |
| on Dec 27, 2000 at 10:42 UTC ( #48393=note: print w/ replies, xml ) | Need Help?? |
|
My rule of thumb: if a function doesn't make code any easier to read, it's not worth writing. At one point in my life (not very long ago ;)), I had the temptation to create lots of little 2-line and 3-line functions (probably coming from an OO background in school, where I was taught to make even simple variable accesses into object methods...) But, consider the following code:
It's clear in this case that the overhead involved in calling a function (mostly involving pushing variables to perl's stack and popping them back off) makes the code run roughly 3 times more slowly. Generally, I try to avoid calls to "small" functions in inner loops whenever performance is anything of an issue; if I'm only reusing those few lines of code once or twice in a program, it's just not worth it to create a function for it, in my opinion.
In Section
Meditations
|
|
||||||||||||||||||||||||||||||