Training, Open Source Programming Languages

This is page http://www.wellho.net/mouth/2069_Eff ... speed.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Efficient calls to subs in Perl - avoid duplication, gain speed

When you call a sub in Perl (a subroutine - a named block of code), you pass in a list of parameters in the list called @_. It's easy and straightforward ... but as the length of your list increases, it starts to become less that efficient.

Say - for example - I had a 35 Mbyte log file (and I really did yesterday, when I was looking at our web server log) ... then passing it across to a sub would duplicate the list into @_, and if I copied it into a named list in the sub that would be a third copy - over 100 Mbytes of memory swallowed up. Here's how that code might look:

sub mygrep {
  ($look4,@within) = @_;
  @send_back = reverse(grep(/$look4/i,@within));
  return @send_back;
  }
open (FH,"shortie");
@stuff = <FH>;
@interesting = mygrep("horse",@stuff);
print @interesting;


But if you pass a REFERENCE to the list across to your sub, you can save yourself all the duplication - change @stuff to \@stuff in the call, collect the parameter in $within rather than @within inside the sub, then reference it via @$within rather than @within when you search through it. The code does not look much different:

sub mygrep {
  ($look4,$within) = @_;
  @send_back = reverse(grep(/$look4/i,@$within));
  return @send_back;
  }
open (FH,"shortie");
@stuff = <FH>;
@interesting = mygrep("horse",\@stuff);
print @interesting;


but the performance certain does change dramatically!

If you're a newcomer to Perl you may find this sort of thing perplexing .. but help is to hand! Subjects such as this are covered on our Perl Programming course. And if you're already somewhat into Perl, but want to go a little further, we run a Perl for larger projects course which covers handling of large data sets, object orientation, databases and more.


"Let me show you how that works ...."


Copying the list - lots of duplication
Passing a reference - only the one list

(written 2009-03-07)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P209 - Subroutines in Perl
  [96] Variable Scope - (2004-10-22)
  [308] Call by name v call by value - (2005-05-11)
  [357] Where do Perl modules load from - (2005-06-24)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [775] Do not duplicate your code - (2006-06-23)
  [969] Perl - $_ and @_ - (2006-12-07)
  [1163] A better alternative to cutting and pasting code - (2007-04-26)
  [1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
  [1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
  [1784] Global - Tcl, PHP, Python - (2008-09-03)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1860] Seven new intermediate Perl examples - (2008-10-30)
  [1921] Romeo and Julie - (2008-12-04)
  [2550] Do not copy and paste code - there are much better ways - (2009-12-26)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
  [3066] Separating groups of variables into namespaces - (2010-11-24)
  [3574] Perl functions such as chop change their input parameters - (2012-01-10)
  [3833] Learning to use existing classes in Perl - (2012-08-10)


Back to
Playing Catchup
Previous and next
or
Horse's mouth home
Forward to
Converting to Perl - the sort of programs you will write
Some other Articles
Extra PHP Examples
Copyright, Portability and other nontechnical web site issues
Setting up a MySQL database from PHP
Converting to Perl - the sort of programs you will write
Efficient calls to subs in Perl - avoid duplication, gain speed
Playing Catchup
Perl - lists do so much more than arrays
Melksham Industrial
Static mirroring through HTTrack, wget and others
East of Melksham Countryside
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2069_Eff ... speed.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb