fhg
Volume 7, Number 24 -- June 27, 2007

Performance of Function Subprocedures

Published: June 27, 2007

by Ted Holt


Note: The code accompanying this article is available for download here.


Last week, I wrote about performance issues when parameters are passed to subprocedures. This week, I write about a similar matter. If a subprocedure returns a value to the caller (i.e., the subprocedure serves as a function), the computer has to store the returned value somewhere in memory. The fact that the system must allocate memory for returned values means that performance may be affected.

To test the performance of function subprocedures, I wrote two substring functions. The first one, SubString, works like the %SUBST built-in function that IBM gives us as part of the RPG compiler, but it differs in two ways:

  • If the length of the substring from the beginning position exceeds the end of the string, the remainder of the string is returned. This is the same behavior %SUBST exhibits when it receives no third parameter. Fewer characters may be returned to the caller than the number requested in the third parameter.
  • If the beginning position and/or length parameter is invalid, the function returns an empty string.

Here's a version of SubString that works with 64-byte strings:

P SubString       b                                  
D                 pi            64a   varying        
D  inString                     64a   varying const  
D  inPosition                   10i 0         const  
D  inLength                     10i 0         const  
D                                                    
D EmptyString     c                   const('')      
                                                     
 /free                                               
     if inPosition <= *zero                          
     or inLength <= *zero                            
     or inPosition > %len(inString);                 
        return EmptyString;                          
     endif;                                          
     if (inPosition + inLength - 1) > %len(inString);
        return %subst(inString: inPosition);         
     endif;                                          
     return %subst(inString: inPosition: inLength);  
 /end-free         
P                 e

Notice that all three parameters are passed by read-only reference, so there is no parameter overhead of any significance. For those of you who are still fuzzy about subprocedures, the second line is the one that defines the return value.

D                 pi            64a   varying

The second subprocedure, SubStringThru, differs from %SUBST in the definition of the third parameter. In both functions, the first parameter is the string from which the substring is to be derived, and the second parameter is the beginning position. However, SubStringThru's third parameter is the ending position of the substring, not the length of the returned string. For example, SubStringThru (SomeString: 4: 10) means "the characters in positions four through ten of SomeString", which is equivalent to %SUBST(SomeString: 4: 7).

I wrote two versions of SubStringThru. Version 1 of SubStringThru uses a modular programming technique my professors pounded into my head when I was getting my computer science degree--build new routines from existing routines, otherwise known as "don't reinvent the wheel." Version 1 uses the SubString subprocedure to do its grunt work. Here is the subprocedure defined to return a 64-byte value.

P SubStringThru   b                                  
D                 pi            64a   varying        
D  inString                     64a   varying const  
D  inBeginPos                   10i 0         const  
D  inEndPos                     10i 0         const  
D                                                    
D Length          s             10i 0                
D EmptyString     c                   const('')      
 /free                                               
     Length = inEndPos - inBeginPos + 1;             
     return SubString(inString: inBeginPos: Length); 
 /end-free                                           
P                 e                                  

Version 2 does its own grunt work.

P SubStringThru   b                                
D                 pi            64a   varying      
D  inString                     64a   varying const
D  inBeginPos                   10i 0         const
D  inEndPos                     10i 0         const
D                                                  
D Length          s             10i 0              
D EmptyString     c                   const('')    
 /free                                             
     Length = inEndPos - inBeginPos + 1;           
     if inBeginPos <= *zero                        
     or Length <= *zero                            
     or inBeginPos > %len(inString);               
        return EmptyString;                        
     endif;                                        
     if (inBeginPos + Length - 1) > %len(inString);
        return %subst(inString: inBeginPos);       
     endif;                                        
     return %subst(inString: inBeginPos: Length);  
 /end-free                                         
P                 e                                

Now for the tests. I used the same method I used in the previous article. That is, rather than use some mechanism that measures in fractions of seconds (e.g., PEX), I chose to measure performance in CPU seconds. I chose this method because I believe it gives a truer picture of how subprocedures perform in production. I've included the code in a zip file, in case you'd like to do your own testing.

The first performance test consisted of 500,000 calls to the various substring techniques. I used two lengths of return values--64 bytes and 64-kilobytes. Take a look at this table.


Technique

64-byte return values

64-kilobyte return values

SubString

1

9

SubStringThru version 1

1

18

SubStringThru version 2

1

9


The numbers indicate elapsed CPU seconds. Here are the same figures for 2,500,000 calls.


Technique

64-byte return values

64-kilobyte return values

SubString

1

45

SubStringThru version 1

2

89

SubStringThru version 2

1

45


The last two lines of the table tell why I wrote the second version of SubStringThru. The first version performed terribly because each invocation generated a second subprocedure invocation, an invocation of SubString.

The lessons I learned from these tests are similar to those I shared with you in the previous article.

  1. The size of the return value is important. The larger the return value, the longer the run-time. Returning a value from a function subprocedure is similar to passing a parameter by value-the system has to allocate memory. The more memory, the longer the allocation and deallocation time.
  2. Second, consider the frequency of the call. If a subprocedure is likely to be invoked frequently during a program, keep the return value as short as possible.
  3. Third, as wonderful as modular programming is, building routines from other routines can have a terrible effect on performance.

I still like subprocedures and recommend that developers use them. But I have decided that I will try to be more judicious about the way I implement them.

I have yet more to cover concerning performance and subprocedures. Maybe next week or the week after.


RELATED STORY

Parameter Passing and Performance



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

COMMON:  Join us at the Annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
Seagull Software:  Web-enable your System i apps with LegaSuite GUI
VAULT400:  Securely archive data with Instant Back-Up & 24x7 Recovery


IT Jungle Store Top Book Picks

The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95

 

The Four Hundred
The AS/400 at 19: Predicting the Future--Or Not

IBM Kills Off System i ServerProven, Standard Edition Rebates

VoIP and the Search for Single Points of Failure

As I See It: Dare to Be Rich

The Linux Beacon
Mandriva, Ubuntu Not Interested in Microsoft Deals

SGI Launches Blade-Style Altix Linux Supers

Fujitsu Adds New Blade Chassis, Quad-Core Server

The CIO Is the Hammer, and Everything IT Vendors See Are Nails

Four Hundred Stuff
MPG Helps to Size Boxes in a User-Based Pricing World

Vision's Product Plans Change Little Post Lakeview

Don't Overlook Hardware-Based High Availability Alternatives

Halcyon Boosts Spool File Manager, Company

Big Iron
For Some Users, Multiprise and VSE May Have a Bright Past Ahead

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
June 23, 2007: Volume 9, Number 25

June 16, 2007: Volume 9, Number 24

June 9, 2007: Volume 9, Number 23

June 2, 2007: Volume 9, Number 22

May 26, 2007: Volume 9, Number 21

May 19, 2007: Volume 9, Number 20

The Windows Observer
MPack Hacker Tool Claims 10,000 Compromised Web Sites

Microsoft Ships Updated Dynamics ERP Products

Intel Bangs the Itanium Drum, Draws Out Roadmap

Linspire Hooks Up with Microsoft, Too

The Unix Guardian
Intel Bangs the Itanium Drum, Draws Out Roadmap

Sun Revs Solaris Express Developer Edition, Adds Non-Sun Iron Support

Disk Array Sales Still Humming Along, Says IDC

Vision Solutions Acquires HA Rival Lakeview Technology

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

ProData Computer Services
WorksRight Software
Bug Busters Software Engineering



TABLE OF CONTENTS
PHP on i5/OS: A Whole New Stack

Performance of Function Subprocedures

Admin Alert: Meditations on Full System Backups

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
FNDSTRPDM Output Member Name to *OUTFILE

Stuck in the Dark Ages

Referencing the key of current record

Brother HL 6050 printing '$' instead of '£' symbol

XML





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement